index previous next




file name: sockbase.hpp, sockbase.cpp
classification: simulation
contents: class CSocket
use: a basic interface to stream and datagram sockets


global items used:


local functions defined in the implementation file:

 ESocketMode GetSocketMode (double timeout): Returns the timeout mode of the socket for s_receive() calls. The mode depends on the value of timeout.


member variables:

 ACSocketAddress *addr_: the socket address.

 INT32 sid_: the ID of the socket.

 EBool stream_: True if the object represents a stream socket, False if it represents a datagram socket.

 double timeout_: the timeout used for message reception (in seconds).
default and reset value: SocketTimeout


member functions:

 CSocket (ACSocketAddress* addr, INT32 sid, EBool stream): Stores the socket ID and address (assume that a socket with the ID sid exists and is open). If stream is True, it is assumed that the socket is a stream socket, if it is False, a datagram socket is assumed. If addr is NULL, an assert fails.

 CSocket (ACSocketAddress *addr, EBool stream): stores the address and type of the socket. If addr is NULL, an assert fails.

 virtual ~CSocket (): Closes the socket (does not unlink it).

 ACSocketAddress *GetSocketAddress(): Returns the socket address.

 INT32 GetSocketID(): Returns the file descriptor of the socket.

 double GetTimeout (): returns the currently used timeout (in seconds).

 virtual void Init (): Set the timeout to its default value SocketTimeout.

 EBool IsStream (): Returns True if the socket is a stream socket, False if it is a datagram socket.

 EBool Receive (void *data, INT32 *size): Reads the data from the socket (assumes that data points to a buffer of size *size). Blocks according to the internal timeout. Modifies *size to reflect the number of bytes read. Returns True if the receive operation was successful, else False. If data or size is NULL, an assert fails.

 EBool Receive (void *data, INT32 *size, double timeout): Has the same behaviour as the Receive() function above, but the timeout is controlled through timeout. Please see SetTimeout() for a description of timeout.

 virtual void Reset (): Calls Init() to set all members to default values.

 EBool Send (void *data, INT32 size): Sends the data using the appropriate s_send() function of the socket type. Returns True if the send operation was successful, else False. If data is NULL, an assert fails.

 void SetSocketID (INT32 sid): Sets the socket ID.

 void SetTimeout (double timeout): Sets the timeout (in seconds) for message reception. Use a negative value to block indefinitely.
Note: blocking indefinitly does not imply that a call to the socket only returns if a message has been received. An error (pe. socket deletion) may also result in a return.



last modified: Fri Feb 5 18:56:33 1999