#include <Flek/FSocket.H>
FSocket is an abstract socket interface. FSocket was adapted from K.A. Knizhnik's very nice SAL library. It provides a socket implementation based on the socket library provided by the operating system. As local sockets are not supported under Win32, shared memory and semaphore objects are used to provide high speed communications on the same computer.
virtual FSocket* FSocket::accept();
Accept new socket. This method is called by server to establish a connection with the new client. When the client executes a connect method to the access server's accept port, accept method will create new socket, which can be used for communication with the client. The accept method will block the current task until some connection is established.Return Value
Pointer to new socket or NULL if operation failed.
virtual int FSocket::cancel_accept();
Cancel accept operation. Task blocked in accept call is woken and execution continues.Return Value
1 if socket was successfully closed, 0 otherwise.
virtual int FSocket::close();
Close socket connection.Return Value
1 if operation successfully completed, 0 otherwise.
static FSocket* FSocket::connect(char const* address, socket_domain domain = sock_any_domain, int max_attempts = DEFAULT_CONNECT_MAX_ATTEMPTS, time_t timeout = DEFAULT_RECONNECT_TIMEOUT);
Establish connection with server. This method will do at most max_attempts attempts to connect server, with timeout interval between attempts.Parameters
address Address of server socket in format "hostname:port". domain Type of connection. The following values of this parameter are recognized: If sock_any_domain is specified, local connection is chosen when either port was omitted in specification of the address or hostname is "localhost", and global connection is used in all other cases.
- sock_any_domain = domain is chosen automatically.
- sock_local_domain = local domain (connection with one host).
- sock_global_domain = internet domain.
max_attempts Maximal number of attempts to connect to server. timeout Timeout in seconds between attempts to connect the server. Return Value
This method always create new socket object and returns a pointer to it. If connection with server was not established, this socket contains error code describing reason of failure. So returned socket should be first checked by the valid() method.
static FSocket* FSocket::create_global(char const* address, int listen_queue_size = DEFAULT_LISTEN_QUEUE_SIZE);
Create and open socket in global (internet) domain at the server site.Parameters
address Address to be assigned to the socket. listen_queue_size Size of listen queue. Return Value
This method always create new socket object and returns pointer to it. If socket can not be opened, error code field of returned socket describes the reason of failure. So returned socket should be first checked by the valid() method.
static FSocket* FSocket::create_local(char const* address, int listen_queue_size = DEFAULT_LISTEN_QUEUE_SIZE);
Create and open socket in local domain at the server site.Parameters
address Address to be assigned to the socket. listen_queue_size Size of listen queue. Return Value
This method always create new socket object and returns pointer to it. If socket can not be opened, error code field of returned socket describes the reason of failure. So returned socket should be first checked by the valid() method.
virtual void FSocket::get_error_text(char* buf, size_t buf_size);
Get error message text for the last operation.Parameters
buf Buffer to receive text of the error message. buf_size Size of buffer, no more than buf_size bytes will be placed in the buffer.
virtual int FSocket::read(void* buf, size_t size);
Read data from socket.Parameters
buf Buffer to hold fetched data. buf_size Number of bytes to fetch. Return Value
1 if operation successfully completed, 0 otherwise.
virtual int FSocket::shutdown();
Shutdown the socket. This function prohibits write and read operation on the socket. All further attempts to read or write data from/to the socket will be denied. But all previously initiated operations are guaranteed to be completed.Return Value
1 if operation successfully completed, 0 otherwise.
virtual int FSocket::valid();
Check the status of the last operation with socket.Return Value
1 if the last operation completed successfully, 0 otherwise.
virtual int FSocket::write(void const* buf, size_t size);
Write data to socket.Parameters
buf Buffer that contains the data to be sent. buf_size Number of bytes to send. Return Value
1 if operation successfully completed, 0 otherwise.