|
class TcpSocket
This class represents an TCP/IP socket. It extendes the basic semantics of sockets to allow non-blocking or time constrained operations.
Callback WhenWait
If this callback is defined, it is invoked periodically while TcpSocket performs any operations, with the frequency 50Hz. This is intended to give user feedback in interactive applications.
static String GetHostName()
Returns the name of computer.
int GetDone() const
Returns number of bytes processed during current operation; intended to be called from WhenWait routine
bool IsOpen() const
Returns true if socket is open.
bool IsEof() const
Returns true if connection was closed and all peer data were processed.
bool IsError() const
Returns true if some previous operations reported error. In that case, all subsequent request are ignored.
void ClearError()
Clears the error state.
int GetError() const
Returns errorcode. Errorcodes are either defined by SOCKET API or it can be -1 for other errors.
String GetErrorDesc() const
Returns description of error.
void Abort()
Sets TcpSocket to aborted state. In aborted state, all subsequent request are ignored. Intended to be called from WhenWait routine.
bool IsAbort() const
Returns true is TcpSocket is in aborted state.
void ClearAbort()
Clears the aborted state.
SOCKET GetSOCKET() const
Returns socket handle. Note that all TcpSocket sockets are non-blocking from host OS perspective.
String GetPeerAddr() const
Returns the peer address.
void Attach(SOCKET socket)
Attaches socket to TcpSocket. socket must be in non-blocking state.
bool Connect(const char *host, int port)
Connects socket to server at host:port. This operation is blocking.
bool Connect(IpAddrInfo& info)
Connects socket to server found at info. Non-blocking.
bool Listen(int port, int listen_count = 5, bool ipv6 = false, bool reuse = true)
Starts a listening server socket at port with input queue listen_count. ipv6 sets the socket to IPv6 mode, reuse sets SO_REUSEADDR socket option.
bool Accept(TcpSocket& listen_socket)
Accepts a connection from listen_socket.
void Close()
Closes the socket.
void Shutdown()
Performs shutdown for write operations. Normally not needed.
void NoDelay()
Sets TCP_NODELAY option.
void Linger(int msecs)
Sets SO_LINGER option to msecs. If msecs is Null, switches SO_LINGER off.
void NoLinger()
Same as Linger(Null).
bool Wait(dword events)
Waits for at most timeout for events, which can be a combination of WAIT_READ (wait for more input bytes available), WAIT_WRITE (wait till it is possible to write something to socket). Wait also always returns when socket exception happens.
bool WaitRead()
Same as Wait(WAIT_READ).
bool WaitWrite()
Same as Wait(WAIT_WRITE).
int Peek()
int Term()
Returns the next input byte without actually removing it from input queue. It at most waits for specified timeout for it, if there is still none, returns -1.
int Get()
Reads the next input byte. It at most waits for specified timeout for it, if there is still none, returns -1.
int Get(void *buffer, int len)
Reads at most len bytes into buffer, trying to do so at most for specified timeout. Returns the number of bytes actually read.
String Get(int len)
Reads at most len bytes, trying to do so at most for specified timeout. Returns a String with read data.
int Put(const char *s, int len)
Writes at most len bytes from buffer, trying to do so at most for specified timeout. Returns the number of bytes actually written.
int Put(const String& s)
Writes s, trying to do so at most for specified timeout. Returns the number of bytes actually written.
bool GetAll(void *buffer, int len)
Reads exactly len bytes into buffer. If such number of bytes cannot be read until timeout, returns false and sets timeout error for TcpSocket.
String GetAll(int len)
Reads exactly len bytes. If such number of bytes cannot be read until timeout, returns String::GetVoid() and sets timeout error for TcpSocket.
String GetLine(int maxlen = 65536)
Reads single line (ended with '\n', '\r' is ignored). If the whole line cannot be read within timeout or line length is longer than maxlen sets error and returns String::GetVoid().
bool PutAll(const char *s, int len)
Outputs exactly len bytes. If such number of bytes cannot be written in time specified by timeout, sets error and returns false.
bool PutAll(const String& s)
Outputs the whole String. If such number of bytes cannot be written in time specified by timeout, sets error and returns false.
bool StartSSL()
Sets TcpSocket to SSL mode and starts SSL handshake. Core/SSL must be present in project. Returns true if SSL could have been started. Handshake is not finished until SSLHandshake returns false.
bool IsSSL() const
Returns true if TcpSocket is in SSL mode.
bool SSLHandshake()
Attempts the progress on SSL handshake for at most timeout period. Returns true if SSL handshake is (still) in progress.
void SSLCertificate(const String& cert, const String& pkey, bool asn1)
Sets the SSL certificate. Must be called before StartSSL. (Note that clients usually do not need certificates, this is usually used on accepting sockets.)
const SSLInfo *GetSSLInfo() const
Returns information about established (after handshake) SSL connection or NULL if such information is not available.
TcpSocket& Timeout(int ms)
Sets timeout for all operations. Zero means that all operations return immediatelly (in that case it is usually a good idea to perform some sort of external blocking on socket or socket group using e.g. SocketWaitEvent). Null means operations are blocking (but they still can invoke WhenProgress periodically if defined). Other values specify a number of milliseconds. Note: It is possible to adjust timeout before any single TcpSocket operation. Returns *this.
int GetTimeout() const
Returns current timeout.
TcpSocket& GlobalTimeout(int ms)
Sets the "global timeout". This timeout is in effect over a whole range of operations, until it is canceled by calling this method with Null parameter. If global timeout is exceeded, operation during which it happened fails and socket error code is set to ERROR_GLOBAL_TIMEOUT.
TcpSocket& NoGlobalTimeout()
Same as GlobalTimeout(Null).
TcpSocket& Blocking()
Same as Timeout(Null). Returns *this.
TcpSocket& WaitStep(int ms)
Sets the periodicity of calling WhenWait in millisecond between calls. Default is 10ms (100hz).
int GetWaitStep() const
Retruns current periodicity of calling WhenWait.
TcpSocket()
~TcpSocket()
Constructor, destructor.
struct SSLInfo
This structure is used to pass information about established SSL connection.
String cipher
Cipher used.
bool cert_avail
Certificate is available.
String cert_subject
Subject name.
String cert_issuer
Issuer name.
Date cert_notbefore
Certificate is not valid before this date.
Date cert_notafter
Certificate is not valid after this date.
int cert_version
Version of certificate.
String cert_serial
Serial number of certificate.
|