simple-socket library 1.1.5
|
Unix datagram socket class. More...
#include <UnixDatagramSocket.h>
Public Types | |
enum | ShutdownDirection { STOP_SEND = SHUT_WR, STOP_RECEIVE = SHUT_RD, STOP_BOTH = SHUT_RDWR } |
Public Member Functions | |
void | bind (const std::string &localPath) |
void | connect (const std::string &foreignPath) |
void | disconnect () |
std::string | getForeignPath () const |
std::string | getLocalPath () const |
bool | peerDisconnected () const |
returns whether a peer disconnected | |
int | receive (void *buffer, size_t len) |
receive data from a bound socket | |
int | receiveFrom (void *buffer, size_t len, std::string &sourcePath) |
int | send (const void *buffer, size_t len) |
send data through a connected socket | |
void | sendTo (const void *buffer, size_t len, const std::string &foreignPath) |
void | shutdown (ShutdownDirection type) |
shutdown the connection in the specified direction | |
int | timedReceive (void *buffer, size_t len, int timeout) |
receive data from a bound socket, return after the given timespan | |
int | timedReceiveFrom (void *buffer, size_t len, std::string &sourcePath, int timeout) |
UnixDatagramSocket () | |
Protected Types | |
enum | SocketDomain { INTERNET = PF_INET, UNIX = PF_LOCAL } |
enum | SocketType { STREAM = SOCK_STREAM, DATAGRAM = SOCK_DGRAM } |
Static Protected Member Functions | |
static std::string | extractPath (const sockaddr_un &addr, socklen_t len) |
extracts a path string from the socket address structure | |
static void | fillAddress (const std::string &path, sockaddr_un &addr) |
Protected Attributes | |
bool | m_peerDisconnected |
int | m_socket |
Unix datagram socket class.
Definition at line 9 of file UnixDatagramSocket.h.
enum NET::SimpleSocket::ShutdownDirection [inherited] |
enum NET::SimpleSocket::SocketDomain [protected, inherited] |
Definition at line 166 of file SimpleSocket.h.
enum NET::SimpleSocket::SocketType [protected, inherited] |
Definition at line 172 of file SimpleSocket.h.
UnixDatagramSocket::UnixDatagramSocket | ( | ) |
Construct a Unix datagram socket
SocketException | thrown if unable to create the socket |
Definition at line 10 of file UnixDatagramSocket.cpp.
void UnixDatagramSocket::sendTo | ( | const void * | buffer, |
size_t | len, | ||
const std::string & | foreignPath | ||
) |
Send the given buffer as a datagram to the specified address / port.
buffer | data to be send |
len | number of bytes to write |
foreignPath | filename of the datagram socket the data should be sent to |
SocketException | thrown if unable to send datagram |
Definition at line 14 of file UnixDatagramSocket.cpp.
int UnixDatagramSocket::receiveFrom | ( | void * | buffer, |
size_t | len, | ||
std::string & | sourcePath | ||
) |
Read read up to bufferLen bytes data from this socket. The given buffer is where the data will be placed.
buffer | buffer to receive data |
len | maximum number of bytes to receive |
sourcePath | path where the data originated |
SocketException | thrown if unable to receive datagram |
Definition at line 26 of file UnixDatagramSocket.cpp.
int UnixDatagramSocket::timedReceiveFrom | ( | void * | buffer, |
size_t | len, | ||
std::string & | sourcePath, | ||
int | timeout | ||
) |
Read read up to bufferLen bytes data from this socket. The given buffer is where the data will be placed. If no host has sent a datagram before the timeout runs out, the function will return without changing the buffer.
buffer | buffer to receive data |
len | maximum number of bytes to receive |
sourcePath | path where the data originated |
timeout | timeout in milliseconds |
SocketException | thrown if unable to receive datagram |
Definition at line 39 of file UnixDatagramSocket.cpp.
void UnixSocket::connect | ( | const std::string & | foreignPath | ) | [inherited] |
Establish a socket connection with the given socket
foreignPath | the path of the remote socket |
SocketException | thrown if unable to establish connection |
Definition at line 13 of file UnixSocket.cpp.
void UnixSocket::bind | ( | const std::string & | localPath | ) | [inherited] |
Set the local path to the specified path
localPath | specifies where the socket should be bound |
SocketException | thrown if setting local path fails |
Definition at line 22 of file UnixSocket.cpp.
std::string UnixSocket::getLocalPath | ( | ) | const [inherited] |
Get the local path (after binding the socket)
SocketException | thrown if fetch fails |
Definition at line 32 of file UnixSocket.cpp.
std::string UnixSocket::getForeignPath | ( | ) | const [inherited] |
Get the foreign path. Call connect() before using this function.
SocketException | thrown if fetch fails |
Definition at line 43 of file UnixSocket.cpp.
void UnixSocket::fillAddress | ( | const std::string & | path, |
sockaddr_un & | addr | ||
) | [static, protected, inherited] |
Fill an address structure with the given path. If the given path is not valid addr will be unchanged.
path | path to unix domain socket |
addr | address structure to fill |
SocketException | thrown if path is not valid |
Definition at line 54 of file UnixSocket.cpp.
std::string UnixSocket::extractPath | ( | const sockaddr_un & | addr, |
socklen_t | len | ||
) | [static, protected, inherited] |
extracts a path string from the socket address structure
Definition at line 64 of file UnixSocket.cpp.
int SimpleSocket::send | ( | const void * | buffer, |
size_t | len | ||
) | [inherited] |
send data through a connected socket
send() can only be used on a socket that called connect() before. If you try to use send() on a not connected socket, SocketException will be thrown.
If you are using a stream oriented socket (like TCPSocket), the operating system is allowed to send only a part of the packet you told it to send, so send() will return the number of bytes actually sent. It is your responsibility to resend the data not sent by send()
If you are using a datagram oriented socket (like UDPSocket or SCTPSocket) the operating system will only send and receive complete datagrams, but send() will fail if you are trying to send too much data. In that case SocketException will be thrown.
buffer | data to be send |
len | length of the data to be sent |
SocketException | if sending went wrong |
Definition at line 43 of file SimpleSocket.cpp.
int SimpleSocket::receive | ( | void * | buffer, |
size_t | len | ||
) | [inherited] |
receive data from a bound socket
receive() can only be used on a socket that called bind() or connect() before. If you try to use receive() on a not bound socket, SocketException will be thrown.
If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.
If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.
If the remote host has closed the connection (on a connection based socket like TCP or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
buffer | the buffer the received data will be written to |
len | length of the provided buffer, receive will not read more than that |
SocketException | in case an error occured |
Definition at line 61 of file SimpleSocket.cpp.
int SimpleSocket::timedReceive | ( | void * | buffer, |
size_t | len, | ||
int | timeout | ||
) | [inherited] |
receive data from a bound socket, return after the given timespan
timedReceive() can only be used on a socket that called bind() or connect before. If you try to use receive() on a not bound socket, SocketException will be thrown.
If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.
If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.
If the remote host has closed the connection (on a connection based socket like TCP or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
buffer | the buffer the received data will be written to |
len | length of the provided buffer, receive will not read more than that |
timeout | the timeout in ms after which receive will give up and return |
SocketException | in case an error occured |
Definition at line 68 of file SimpleSocket.cpp.
void SimpleSocket::disconnect | ( | ) | [inherited] |
Disconnect and unset any foreign addresses
SocketException | thrown if unable to disconnect |
Definition at line 88 of file SimpleSocket.cpp.
void SimpleSocket::shutdown | ( | ShutdownDirection | type | ) | [inherited] |
shutdown the connection in the specified direction
Depending on the specified ShutdownDirection, calls for that direction will stop working. Use this function if you want to have more control than just destroing the socket. It allows you to cut the connection in one direction, or both.
If you use shutdown() on an unconnected socket, the corresponding calls will simply stop working.
type | the ShutdownDirection that be used |
SocketException | in case an error occured |
Definition at line 103 of file SimpleSocket.cpp.
bool SimpleSocket::peerDisconnected | ( | ) | const [inherited] |
returns whether a peer disconnected
Will only work if you use a connection oriented, connected socket. Returns true if the peer disconnected. Use this function after a call to receive, returned 0 received bytes.
Definition at line 109 of file SimpleSocket.cpp.
int NET::SimpleSocket::m_socket [protected, inherited] |
Definition at line 185 of file SimpleSocket.h.
bool NET::SimpleSocket::m_peerDisconnected [protected, inherited] |
Definition at line 186 of file SimpleSocket.h.