![]() |
Gray C++ Libraries
0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
|
#include <cNetSocket.h>
Public Member Functions | |
cNetSocket () | |
cNetSocket (SOCKET hSocket) | |
cNetSocket (const cNetSocket &src) | |
virtual | ~cNetSocket () |
cNetSocket & | operator= (const cNetSocket &src) |
virtual void | Close () override |
bool | AttachSocket (SOCKET hSocket, long lEvent=0) |
HRESULT | BindAddr (const cNetAddress &SockAddr) |
bool | Listen (int iMaxBacklogConnections=SOMAXCONN) |
HRESULT | ConnectAddr (const cNetAddress &rSockAddr) |
HRESULT | Accept (OUT cNetSocket &rConnectedSocket, OUT cNetAddress *pConnectedSocketAddr=nullptr) |
HRESULT | CreateSocket (sa_family_t nAddressFamily=AF_INET, int nSocketType=SOCK_STREAM, IPPROTO_TYPE nProtocolType=IPPROTO_TCP) |
HRESULT | CreateConnect (const cNetAddress &SockAddr, int nSocketType=SOCK_STREAM, IPPROTO_TYPE nProtocolType=IPPROTO_TCP) |
HRESULT | CreateConnect (const cNetAddressArray &rSockAddrs, int nSocketType=SOCK_STREAM, IPPROTO_TYPE nProtocolType=IPPROTO_TCP) |
HRESULT | CreateBind (const cNetAddress &SockAddr, int nSocketType=SOCK_STREAM, IPPROTO_TYPE nProtocolType=IPPROTO_TCP) |
HRESULT | CreateBindFirst (const cNetAddressArray &rSockAddrs, int nSocketType=SOCK_STREAM, IPPROTO_TYPE nProtocolType=IPPROTO_TCP) |
HRESULT | CloseFlush (TIMESECD_t tWait=1) |
bool | isConnecting () const noexcept |
bool | isConnected () const noexcept |
bool | isConnected2 () const noexcept |
void | SetStateEstablished () noexcept |
SOCKET | get_HSocket () const noexcept |
HASHCODE_t | get_HashCode () const noexcept |
bool | GetAddrSock (OUT cNetAddress &SockAddr) const |
bool | GetAddrPeer (OUT cNetAddress &SockAddr) const |
HRESULT | SendTo (const void *pData, size_t iLen, const cNetAddress &SockAddr, int nFlags=0) |
HRESULT | ReceiveFrom (void *pBuffer, size_t nBufLen, cNetAddress &SockAddr, int nFlags=0) |
cNetAddress | get_AddrSock () const |
cNetAddress | get_AddrPeer () const |
bool | isBlockingMode () const |
bool | put_BlockingMode (bool bBlock) |
bool | put_BlockingTimeoutRx (TIMESECD_t tSec) |
bool | put_BlockingTimeoutTx (TIMESECD_t tSec) |
bool | put_Nagle (bool bDelay) |
bool | put_KeepAlive (bool bKeepAlive) |
bool | put_Async (bool bAsync) |
bool | put_ReuseAddr (bool bReuse) |
int | get_BufferSizeRx () const |
bool | put_BufferSizeRx (int iSize) |
int | get_BufferSizeTx () const |
bool | put_BufferSizeTx (int iSize) |
int | get_ReadReady () const |
virtual HRESULT | WriteX (const void *pData, size_t nDataSize) override |
virtual HRESULT | ReadX (void *pData, size_t nDataSize) override |
virtual HRESULT | ReadPeek (void *pData=nullptr, size_t nDataSize=1) override |
UNITTEST_FRIEND (cNetSocket) | |
![]() | |
cNetSocketBase () | |
virtual | ~cNetSocketBase () |
virtual bool | isValidCheck () const noexcept |
< memory allocation and structure definitions are valid. More... | |
bool | isValidSocket () const noexcept |
operator SOCKET () const noexcept | |
bool | AttachSocket (SOCKET hSocket) |
SOCKET | DetachSocket () |
bool | Listen (int iMaxBacklogConnections=SOMAXCONN) |
int | Send (const void *pData, int iLen, int nFlags=0) |
int | Receive (void *pBuffer, int nBufLen, int nFlags=0) |
bool | IOCtl (long lCmd, unsigned long *pnArgs) const |
bool | SetSockOpt (int nOptionName, const void *optval, socklen_t optlen, int nLevel=SOL_SOCKET) const |
bool | GetSockOpt (int nOptionName, void *optval, socklen_t *pOptLen, int nLevel=SOL_SOCKET) const |
![]() | |
virtual | ~CObject () |
virtual void | AssertValid () const |
< memory allocation and structure definitions are valid. More... | |
virtual void | Serialize (cArchive &a) |
![]() | |
virtual STREAM_SEEKRET_t | Seek (STREAM_OFFSET_t iOffset, SEEK_ORIGIN_TYPE eSeekOrigin=SEEK_Set) override |
Disambiguate Seek for cStreamBase to cStreamInput for stupid compiler. More... | |
virtual STREAM_POS_t | GetPosition () const override |
virtual STREAM_POS_t | GetLength () const override |
void | SeekToBegin () |
STREAM_POS_t | SeekToEnd () |
![]() | |
cStreamInput () noexcept | |
virtual | ~cStreamInput () |
virtual size_t | SetSeekSizeMin (size_t nSizeMin=k_FILE_BLOCK_SIZE) |
virtual HRESULT | ReadX (OUT void *pData, size_t nDataSize) |
HRESULT | ReadAll (OUT cHeapBlock &block, size_t nSizeExtra=0) |
virtual HRESULT | ReadStringLine (OUT char *pszBuffer, StrLen_t iSizeMax) |
virtual HRESULT | ReadStringLine (OUT wchar_t *pszBuffer, StrLen_t iSizeMax) |
HRESULT | ReadT (OUT void *pVal, size_t nSize) |
template<typename TYPE > | |
HRESULT | ReadT (OUT TYPE &val) |
template<typename TYPE > | |
HRESULT | ReadTN (OUT TYPE &val) |
HRESULT | ReadSize (OUT size_t &nSize) |
template<typename TYPE > | |
HRESULT | ReadSizeT (OUT TYPE &n) |
HRESULT | ReadHashCode (OUT UINT32 &nHashCode) |
HRESULT | ReadHashCode (OUT UINT64 &nHashCode) |
HRESULT | ReadN (OUT BYTE *pBuffer, size_t nSizeMax) |
template<typename _CH > | |
HRESULT | ReadStringN (OUT _CH *pszStr, StrLen_t iSizeMax) |
![]() | |
virtual | ~cStreamBase () |
void | SeekToBegin () |
STREAM_POS_t | SeekToEnd () |
![]() | |
cStreamOutput () noexcept | |
virtual | ~cStreamOutput () |
HRESULT | WriteT (const void *pVal, size_t nDataSize) |
template<typename TYPE > | |
HRESULT | WriteT (TYPE val) |
HRESULT | WriteSize (size_t nSize) |
HRESULT | WriteHashCode (HASHCODE_t nHashCode) |
HRESULT | WriteN (const void *pBuffer, size_t nSize) |
template<typename _CH > | |
HRESULT | WriteStringN (const _CH *pszStr) |
template<typename _CH > | |
HRESULT | WriteCharRepeat (_CH nChar, int nCount=1) |
virtual HRESULT | WriteString (const char *pszStr) |
virtual HRESULT | WriteString (const wchar_t *pszStr) |
StrLen_t | VPrintf (const char *pszFormat, va_list args) |
StrLen_t | VPrintf (const wchar_t *pszFormat, va_list args) |
StrLen_t _cdecl | Printf (const char *pszFormat,...) |
StrLen_t _cdecl | Printf (const wchar_t *pszFormat,...) |
HRESULT | WriteStream (cStreamInput &sInp, STREAM_POS_t nSizeMax=k_FILE_BLOCK_SIZE, IStreamProgressCallback *pProgress=nullptr, TIMESYSD_t nTimeout=0) |
Copy cStreamInput to this stream. More... | |
virtual HRESULT | FlushX () |
Static Public Member Functions | |
static HRESULT | GetLastError () |
static HRESULT | GetLastErrorDef (HRESULT hResDef=E_FAIL) |
Public Attributes | |
SOCKETSTATE_TYPE | m_eState |
What is the state of the socket. e.g. SOCKETSTATE_Disconnected. More... | |
Additional Inherited Members | |
![]() | |
static ITERATE_t | sm_iAllocSockets = 0 |
Count all open sockets. Watch out for WSADATA.iMaxSockets. More... | |
![]() | |
static const BYTE | k_SIZE_MASK = 0x80 |
Used for WriteSize() More... | |
static const size_t | k_FILE_BLOCK_SIZE = (32 * 1024) |
default arbitrary transfer block size. more than this is NOT more efficient. More... | |
![]() | |
SOCKET | get_HSocket () const noexcept |
bool | Connect (const struct sockaddr *pAddr, socklen_t nAddrLen) |
bool | ShutDown (SOCKET_ShutDown_TYPE nHow=SOCKET_ShutDown_sends) |
![]() | |
cNonCopyable () noexcept | |
Force the use of Factory creation via protected constructor. More... | |
~cNonCopyable () noexcept | |
Based on the MFC CSocket "#include <afxsock.h>", Similar to MFC CAsyncSocket,CSocket.
GrayLib::cNetSocket::cNetSocket | ( | ) |
GrayLib::cNetSocket::cNetSocket | ( | SOCKET | hSocket | ) |
GrayLib::cNetSocket::cNetSocket | ( | const cNetSocket & | src | ) |
|
virtual |
HRESULT GrayLib::cNetSocket::Accept | ( | OUT cNetSocket & | rConnectedSocket, |
OUT cNetAddress * | pConnectedSocketAddr = nullptr |
||
) |
We are a listener and find an incoming connection.
bool GrayLib::cNetSocket::AttachSocket | ( | SOCKET | hSocket, |
long | lEvent = 0 |
||
) |
|
inline |
Bind outgoing or incoming (listen) socket to a particular address:port Prepare to listen on a port. (as a server)
|
overridevirtual |
support cStream Maybe we should call CloseFlush() instead.
Reimplemented from GrayLib::cNetSocketBase.
HRESULT GrayLib::cNetSocket::CloseFlush | ( | TIMESECD_t | tWait = 1 | ) |
flush/purge/SO_LINGER before closing. blocks only if in blocking mode.
HRESULT GrayLib::cNetSocket::ConnectAddr | ( | const cNetAddress & | rSockAddr | ) |
request a TCP connection to a server. (listener) if ! isBlocking() we get a FD_CONNECT back when complete. OnEventConnect() then call SetStateEstablished() ASSUME rAddr.MakeValidHostAddr() was already called. // This can block!! ASSUME CreateSocket() was called
HRESULT GrayLib::cNetSocket::CreateBind | ( | const cNetAddress & | SockAddr, |
int | nSocketType = SOCK_STREAM , |
||
IPPROTO_TYPE | nProtocolType = IPPROTO_TCP |
||
) |
Create the socket and Prepare to listen on a port. (as a server)
HRESULT GrayLib::cNetSocket::CreateBindFirst | ( | const cNetAddressArray & | rSockAddrs, |
int | nSocketType = SOCK_STREAM , |
||
IPPROTO_TYPE | nProtocolType = IPPROTO_TCP |
||
) |
Bind to the first address from a list that succeeds. (from cNetDNS) Given a list of addresses, pick the first that works. Maybe AF_INET6 (ip6) or AF_INET (ip4) ?
HRESULT GrayLib::cNetSocket::CreateConnect | ( | const cNetAddress & | SockAddr, |
int | nSocketType = SOCK_STREAM , |
||
IPPROTO_TYPE | nProtocolType = IPPROTO_TCP |
||
) |
Create the outgoing socket and connect. blocking. (as a client)
HRESULT GrayLib::cNetSocket::CreateConnect | ( | const cNetAddressArray & | rSockAddrs, |
int | nSocketType = SOCK_STREAM , |
||
IPPROTO_TYPE | nProtocolType = IPPROTO_TCP |
||
) |
Given a list of addresses, pick the first that works. (from cNetDNS) ASSUME default is a blocking call if IPPROTO_TCP
HRESULT GrayLib::cNetSocket::CreateSocket | ( | sa_family_t | nAddressFamily = AF_INET , |
int | nSocketType = SOCK_STREAM , |
||
IPPROTO_TYPE | nProtocolType = IPPROTO_TCP |
||
) |
create a SOCKET like the M$ WSASocket()
cNetAddress GrayLib::cNetSocket::get_AddrPeer | ( | ) | const |
get the far side address of the connection. this socket is connected to a remote port?
cNetAddress GrayLib::cNetSocket::get_AddrSock | ( | ) | const |
get the local side of the connection. this socket is bound to a port?
int GrayLib::cNetSocket::get_BufferSizeRx | ( | ) | const |
get size of low level read buffer. SO_RCVBUF
int GrayLib::cNetSocket::get_BufferSizeTx | ( | ) | const |
how big is the low level write buffer? SO_SNDBUF
|
inlinenoexcept |
A unique hashcode for the socket.
|
inlinenoexcept |
Get the low level socket handle.
int GrayLib::cNetSocket::get_ReadReady | ( | ) | const |
is there data to be read ? Non blocking.
bool GrayLib::cNetSocket::GetAddrPeer | ( | OUT cNetAddress & | SockAddr | ) | const |
Get the address of the far end. SOCKETSTATE_Connecting to SOCKETSTATE_Established
bool GrayLib::cNetSocket::GetAddrSock | ( | OUT cNetAddress & | SockAddr | ) | const |
Get the address of the near end. (us) SOCKETSTATE_Bound
|
inlinestatic |
An error occurred. try to find out why.
bool GrayLib::cNetSocket::isBlockingMode | ( | ) | const |
is FIONBIO or O_NONBLOCK set ? NOT the same as MFC CSocket.IsBlocking() ?? (currently blocking vs would/might block)
|
inlinenoexcept |
|
inlinenoexcept |
Is isConnecting() or isConnected()? May be connecting but not fully complete yet.
|
inlinenoexcept |
|
inline |
The listen function places a SOCKETSTATE_Bound socket in a state in which it is listening for an incoming connection. NON blocking.
|
inline |
bool GrayLib::cNetSocket::put_Async | ( | bool | bAsync | ) |
FIOASYNC allow async notify. send SIGIO signal to a process when ready. (linux)
bool GrayLib::cNetSocket::put_BlockingMode | ( | bool | bBlock | ) |
Will read,write,accept,connect block when called ? default is to block. O_NONBLOCK or F_SETFL on other systems ?
bool GrayLib::cNetSocket::put_BlockingTimeoutRx | ( | TIMESECD_t | tSec | ) |
assume isBlockingMode
bool GrayLib::cNetSocket::put_BlockingTimeoutTx | ( | TIMESECD_t | tSec | ) |
assume isBlockingMode
bool GrayLib::cNetSocket::put_BufferSizeRx | ( | int | iSize | ) |
set size of low level read buffer. SO_RCVBUF
bool GrayLib::cNetSocket::put_BufferSizeTx | ( | int | iSize | ) |
set size of low level write buffer. SO_SNDBUF
bool GrayLib::cNetSocket::put_KeepAlive | ( | bool | bKeepAlive | ) |
Send keep alive packets or not. http://msdn.microsoft.com/en-us/library/ee470551%28VS.85%29.aspx
bool GrayLib::cNetSocket::put_Nagle | ( | bool | bDelay | ) |
TCP_NODELAY Enable or disable Nagle algorithm for the socket. Default = delay is true/active. (for _WIN32) doesn't send bytes until it has full packet or time has passed.
bool GrayLib::cNetSocket::put_ReuseAddr | ( | bool | bReuse | ) |
SO_REUSEADDR is opposite of SO_EXCLUSIVEADDRUSE
|
overridevirtual |
Test if there is data to read. Non blocking. support cStream
Reimplemented from Gray::cStreamInput.
|
overridevirtual |
Read data. May block if blocking mode. support cStream
HRESULT GrayLib::cNetSocket::ReceiveFrom | ( | void * | pBuffer, |
size_t | nBufLen, | ||
cNetAddress & | SockAddr, | ||
int | nFlags = 0 |
||
) |
Receive UDP (if any)
HRESULT GrayLib::cNetSocket::SendTo | ( | const void * | pData, |
size_t | iLen, | ||
const cNetAddress & | SockAddr, | ||
int | nFlags = 0 |
||
) |
Send UDP to a specific address. don't bother checking isValidSocket() as the function may fail for us. m_hSocket not set is OK.
|
inlinenoexcept |
Only externally called if ! isBlocking() We know we are fully connected if blocking outgoing connect returns.
GrayLib::cNetSocket::UNITTEST_FRIEND | ( | cNetSocket | ) |
|
overridevirtual |
Write data to the (usually small) low level output buffers. May block if blocking mode. support cStream
Reimplemented from Gray::cStreamOutput.
SOCKETSTATE_TYPE GrayLib::cNetSocket::m_eState |
What is the state of the socket. e.g. SOCKETSTATE_Disconnected.