#include <socket.hh>
Public Member Functions | |
Socket () | |
Constructor. | |
Socket (int fd) | |
Constructor. | |
virtual | ~Socket () |
Destructor. | |
int | Connect (const char *host, int port, unsigned long timeoutMs) |
Requests a new connection to be created to remote endpoint. | |
void | Send (const char *data, size_t len, DeltaMsecTime timeoutMs=1000) |
Requests data to be sent. | |
void | Recv (unsigned long timeoutMs) |
Requests data to be received. | |
void | WaitDataAvailable (unsigned long timeoutMs) |
Request waiting for data to be availabe. | |
int | Close () |
Closes the socket immediately. | |
int | Listen (const char *host, int port) |
Listens on the port. | |
int | Listen (const char *unixSocketPath) |
Bind local socket and listen on it. | |
int | Accept (unsigned long timeoutMs) |
Requests accepting of connection. | |
void | Sleep (DeltaMsecTime timeoutMs) |
Request sleep. | |
void | SleepAbs (AbsMsecTime timeWakeMs) |
Request sleep. | |
void | SleepAbs0 () |
Optimized SleepAbs() for immediate wakeup. | |
int | Fd (void) const |
Get the file descriptor. | |
int | Port (void) const |
Get the port. | |
void | Event (short events, short revents, AbsMsecTime currentTime) |
Signalize an event on the socket. | |
void | SendCallBack () |
Callback after ALL data is sent or error during sending. | |
void | RecvCallBack () |
Callback after data is received. | |
void | SleepCallBack () |
Callback after sleep is over. | |
void | Terminate () |
Request termination. | |
void | InitNonsocket (int fd) |
Perform initialization of non-socket fd. | |
Protected Types | |
S_Ok | |
OK. | |
S_Error | |
Error. | |
S_Timeout | |
Timeout. | |
enum | Status { S_Ok, S_Error, S_Timeout } |
Operation status. More... | |
Protected Member Functions | |
const std::string & | Error (void) const |
Get error. | |
size_t | LenNotSent () const |
Return number of bytes not sent in last Send() operation. | |
bool | IsConnected () const |
Return connected status. | |
bool | CanTerminate () |
Call whenever termination is possible. | |
Private Member Functions | |
void | Constructor () |
Common constructor body. | |
virtual void | ConnectCompleteImpl (int result) |
Callback when connect is complete. | |
virtual void | SendCompleteImpl (Status status) |
Callback when send is complete. | |
virtual void | RecvCompleteImpl (int result, const char *buf, size_t lenRecv) |
Callback when data is received. | |
virtual void | AcceptCompleteImpl (int result, int sock, std::string remote) |
Callback when a new connection is accepted. | |
virtual void | SleepCompleteImpl () |
Callback after sleep is over. | |
virtual void | TerminateImpl () |
Termination implementation. | |
virtual void | DataAvailableImpl (Status status) |
Callback after data is available. | |
Private Attributes | |
SocketFnPtr | mHelperFn [6] |
Dispatch functions. | |
Impl *const | mpImpl |
implementation | |
int & | mrFd |
reference to fd so Fd() op can be inlined | |
bool & | mrIsConnected |
reference to connected flag so IsConnected() op can be inlined | |
Classes | |
struct | Impl |
Socket private implementation. More... |
Implements C++ socket - a connection endpoint.
Client should implement the virtual operations:
Server should implement the virtual operations: See also samples directory.
enum nnl::Socket::Status [protected] |
nnl::Socket::Socket | ( | int | fd | ) |
Constructor.
Used for connected socket (after accept())
fd | file descriptor |
int nnl::Socket::Accept | ( | unsigned long | timeoutMs | ) |
Requests accepting of connection.
timeoutMs | timeout of operation in ms |
virtual void nnl::Socket::AcceptCompleteImpl | ( | int | result, | |
int | sock, | |||
std::string | remote | |||
) | [private, virtual] |
Callback when a new connection is accepted.
result | 0 if success, other value if error | |
sock | new socket (connection) file descriptor | |
remote | remote address |
bool nnl::Socket::CanTerminate | ( | ) | [protected] |
Call whenever termination is possible.
If termination is requested, TerminateImpl() is called. Consider also using TERMINATION_POINT macro below.
int nnl::Socket::Close | ( | ) |
Closes the socket immediately.
In case the socket is already closed, nothing happens.
int nnl::Socket::Connect | ( | const char * | host, | |
int | port, | |||
unsigned long | timeoutMs | |||
) |
Requests a new connection to be created to remote endpoint.
If immediate connect is not possible (would block), a callback is called upon successfull connection.
host | remote host | |
port | remote port | |
timeoutMs | connect timeout in ms |
virtual void nnl::Socket::ConnectCompleteImpl | ( | int | result | ) | [private, virtual] |
Callback when connect is complete.
result | 0 if success, other value if error |
virtual void nnl::Socket::DataAvailableImpl | ( | Status | status | ) | [private, virtual] |
Callback after data is available.
Callback for WaitDataAvailable(). It signalizes whether data can be read without blocking.
status | operation status |
const std::string& nnl::Socket::Error | ( | void | ) | const [protected] |
Get error.
Get error text.
void nnl::Socket::Event | ( | short | events, | |
short | revents, | |||
AbsMsecTime | currentTime | |||
) |
Signalize an event on the socket.
Used by netman, should not be used by user.
events | events requested | |
revents | events got | |
currentTime | current time |
int nnl::Socket::Fd | ( | void | ) | const [inline] |
Get the file descriptor.
void nnl::Socket::InitNonsocket | ( | int | fd | ) |
Perform initialization of non-socket fd.
This op is to be used in case you need to use a file descriptor with NNL which is not a socket, but e.g. an ordinary file.
bool nnl::Socket::IsConnected | ( | ) | const [inline, protected] |
Return connected status.
Note the fd can be 'open' (created) even if not connected, so it's safe to call Close() even if not connected.
int nnl::Socket::Listen | ( | const char * | unixSocketPath | ) |
Bind local socket and listen on it.
unixSocketPath | socket name with full path |
int nnl::Socket::Listen | ( | const char * | host, | |
int | port | |||
) |
Listens on the port.
host | host address (ip or name) to listen on.. Will use INADDR_ANY if equals to 0. | |
port | port to listen on |
int nnl::Socket::Port | ( | void | ) | const |
Get the port.
void nnl::Socket::Recv | ( | unsigned long | timeoutMs | ) |
Requests data to be received.
Tries to receive data. If it can't be done immediately (operation would block) the request is forwarded to netman which will do poll() first and then notify Socket.
timeoutMs | timeout of operation in ms |
CanRecv
void nnl::Socket::RecvCallBack | ( | ) |
Callback after data is received.
Called by netman in case the data was already received in Recv() function. Just to separate the Recv() and RecvCompleteImpl().
virtual void nnl::Socket::RecvCompleteImpl | ( | int | result, | |
const char * | buf, | |||
size_t | lenRecv | |||
) | [private, virtual] |
Callback when data is received.
result | 0 if success, other value if error | |
buf | buffer where data is stored | |
lenRecv | number of bytes in buffer (received) |
void nnl::Socket::Send | ( | const char * | data, | |
size_t | len, | |||
DeltaMsecTime | timeoutMs = 1000 | |||
) |
Requests data to be sent.
Tries to send data. If not all the data can be sent immediately (operation would block), the request is forwarded to netman which will do poll() first and then notify Socket via CanSend(). If all the data is sent immediately, or error happened, netman calls SendCallBack(). SendCompleteImpl() is called from both callbacks which is implemented by user.
data | data to be sent | |
len | length of data | |
timeoutMs | timeout of operation in ms |
CanSend
void nnl::Socket::SendCallBack | ( | ) |
Callback after ALL data is sent or error during sending.
Called by netman in case ALL the data was already sent in Send() function or error happened in Send(). Just to separate the Send() and SendCompleteImpl().
virtual void nnl::Socket::SendCompleteImpl | ( | Status | status | ) | [private, virtual] |
Callback when send is complete.
status | S_Ok if all data was sent, S_Error if error on connection, S_Timeout if timeout while sending |
void nnl::Socket::Sleep | ( | DeltaMsecTime | timeoutMs | ) |
Request sleep.
Don't request any operation, just blocking for certain abount of time.
timeoutMs | timeout of operation in ms |
void nnl::Socket::SleepAbs | ( | AbsMsecTime | timeWakeMs | ) |
Request sleep.
Same as Sleep() but requests to be awaken at
timeWakeMs. | ||
timeWakeMs | absolute time in ms when awaking is requested |
void nnl::Socket::SleepCallBack | ( | ) |
Callback after sleep is over.
Called by netman in case sleep is over. Just to separate the Sleep() and SleepCompleteImpl().
virtual void nnl::Socket::TerminateImpl | ( | ) | [private, virtual] |
Termination implementation.
Client will implement termination here, e.g. delete this if allocated by new.
void nnl::Socket::WaitDataAvailable | ( | unsigned long | timeoutMs | ) |
Request waiting for data to be availabe.
Polls for data to read. Kind of same as Recv() but does not read the data.
SocketFnPtr nnl::Socket::mHelperFn[6] [private] |