6 #ifndef _INC_cNetSocket_H
7 #define _INC_cNetSocket_H
13 #include "../WinAPI/WinTypes.h"
22 #define INVALID_SOCKET (SOCKET)(~0)
23 #define SOCKET_ERROR (-1)
55 #ifndef USE_MFC_SOCKET
70 #elif defined(__linux__)
83 #elif defined(__linux__)
84 return m_hSocket.get_Handle();
88 bool Connect(
const struct sockaddr* pAddr, socklen_t nAddrLen)
91 return(::connect(get_HSocket(), pAddr, nAddrLen) == 0);
99 if (::shutdown(get_HSocket(), (
int)nHow) == SOCKET_ERROR)
108 virtual bool isValidCheck() const noexcept;
109 bool isValidSocket() const noexcept
114 if (m_hSocket == INVALID_SOCKET || m_hSocket == (SOCKET)
HANDLE_NULL)
116 #elif defined(__linux__)
117 if (!m_hSocket.isValidHandle())
122 operator SOCKET() const noexcept
124 return get_HSocket();
127 bool AttachSocket(SOCKET hSocket);
128 SOCKET DetachSocket();
130 virtual void Close();
132 bool Listen(
int iMaxBacklogConnections = SOMAXCONN)
137 return ::listen(get_HSocket(), iMaxBacklogConnections) == 0;
140 int Send(
const void* pData,
int iLen,
int nFlags = 0)
154 nFlags |= MSG_NOSIGNAL;
156 int iRet = ::send(get_HSocket(), (
char*)pData, iLen, nFlags);
159 int Receive(
void* pBuffer,
int nBufLen,
int nFlags = 0)
169 int iRet = ::recv(get_HSocket(), (
char*)pBuffer, nBufLen, nFlags);
173 bool IOCtl(
long lCmd,
unsigned long* pnArgs)
const;
174 bool SetSockOpt(
int nOptionName,
const void* optval, socklen_t optlen,
int nLevel = SOL_SOCKET)
const;
175 bool GetSockOpt(
int nOptionName,
void* optval, socklen_t* pOptLen,
int nLevel = SOL_SOCKET)
const;
197 bool m_bBlockingMode;
198 #if ! defined(UNDER_CE)
216 virtual void Close()
override;
217 bool AttachSocket(SOCKET hSocket,
long lEvent = 0);
219 #ifdef USE_MFC_SOCKET
220 SOCKET DetachSocket()
222 return SUPER_t::Detach();
236 #ifdef USE_MFC_SOCKET
244 return GetLastErrorDef();
250 bool Listen(
int iMaxBacklogConnections = SOMAXCONN)
255 if (!SUPER_t::Listen(iMaxBacklogConnections))
288 #ifdef USE_MFC_SOCKET
289 bool isValidSocket() const noexcept
294 if (m_hSocket == INVALID_SOCKET)
358 HRESULT SendTo(
const void* pData,
size_t iLen,
const cNetAddress& SockAddr,
int nFlags = 0);
359 HRESULT ReceiveFrom(
void* pBuffer,
size_t nBufLen,
cNetAddress& SockAddr,
int nFlags = 0);
364 bool isBlockingMode()
const;
365 bool put_BlockingMode(
bool bBlock);
369 bool put_Nagle(
bool bDelay);
370 bool put_KeepAlive(
bool bKeepAlive);
371 bool put_Async(
bool bAsync);
372 bool put_ReuseAddr(
bool bReuse);
374 int get_BufferSizeRx()
const;
375 bool put_BufferSizeRx(
int iSize);
376 int get_BufferSizeTx()
const;
377 bool put_BufferSizeTx(
int iSize);
378 int get_ReadReady()
const;
381 virtual HRESULT WriteX(
const void* pData,
size_t nDataSize)
override;
382 virtual HRESULT ReadX(
void* pData,
size_t nDataSize)
override;
383 virtual HRESULT ReadPeek(
void* pData =
nullptr,
size_t nDataSize = 1)
override;
385 #if defined(_WIN32) && ! defined(UNDER_CE)
386 bool AsyncAccept(
cNetSocket& socknew,
cNetAddress& rAddr, LPCONDITIONPROC pfnCondition, DWORD_PTR dwCallbackData = 0);
387 bool AsyncSelect(HWND hWnd,
WINMSG_t uMsg, UINT dwEventMask = FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE);
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
UINT WINMSG_t
Supplement _WIN32 "windows.h".
Definition: WinTypes.h:111
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
short sa_family_t
enum of network family types. e.g. AF_UNSPEC=0, AF_INET = 2, AF_INET6 = 23, AF_NETBIOS = 17,...
Definition: cNetHost.h:19
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
Definition: cNetAddr.h:31
const sockaddr & ref_Addr() const noexcept
Definition: cNetAddr.h:140
socklen_t get_AddrLen() const noexcept
Definition: cNetAddr.h:124
const sockaddr & get_Addr() const noexcept
Definition: cNetAddr.h:135
Definition: cNetSocket.h:59
bool Listen(int iMaxBacklogConnections=SOMAXCONN)
Definition: cNetSocket.h:132
bool ShutDown(SOCKET_ShutDown_TYPE nHow=SOCKET_ShutDown_sends)
Definition: cNetSocket.h:94
int Receive(void *pBuffer, int nBufLen, int nFlags=0)
Definition: cNetSocket.h:159
bool Connect(const struct sockaddr *pAddr, socklen_t nAddrLen)
Definition: cNetSocket.h:88
static ITERATE_t sm_iAllocSockets
Count all open sockets. Watch out for WSADATA.iMaxSockets.
Definition: cNetSocket.h:65
int Send(const void *pData, int iLen, int nFlags=0)
Definition: cNetSocket.h:140
SOCKET get_HSocket() const noexcept
Definition: cNetSocket.h:78
Definition: cNetSocket.h:185
bool isConnecting() const noexcept
Definition: cNetSocket.h:302
static HRESULT GetLastError()
Definition: cNetSocket.h:278
static HRESULT GetLastErrorDef(HRESULT hResDef=E_FAIL)
Definition: cNetSocket.h:282
bool isConnected2() const noexcept
Definition: cNetSocket.h:320
HASHCODE_t get_HashCode() const noexcept
Definition: cNetSocket.h:345
bool isConnected() const noexcept
Definition: cNetSocket.h:311
SOCKETSTATE_TYPE m_eState
What is the state of the socket. e.g. SOCKETSTATE_Disconnected.
Definition: cNetSocket.h:195
SOCKET get_HSocket() const noexcept
Definition: cNetSocket.h:340
void SetStateEstablished() noexcept
Definition: cNetSocket.h:331
HRESULT BindAddr(const cNetAddress &SockAddr)
Definition: cNetSocket.h:226
bool Listen(int iMaxBacklogConnections=SOMAXCONN)
Definition: cNetSocket.h:250
UNITTEST_FRIEND(cNetSocket)
static HRESULT GetLastErrorDef(HRESULT hResDef=E_FAIL)
Definition: cNetSystem.h:105
static HRESULT GetLastError()
Definition: cNetSystem.h:90
Definition: cNonCopyable.h:17
Definition: cOSHandle.h:59
Definition: cStream.h:456
SOCKETSTATE_TYPE
Definition: cNetSocket.h:37
@ SOCKETSTATE_Connecting
connect() called but not fully connected.
Definition: cNetSocket.h:46
@ SOCKETSTATE_Created
socket() created (isOpen() socket created) but nothing else.
Definition: cNetSocket.h:43
@ SOCKETSTATE_Listening
bound and listening.
Definition: cNetSocket.h:45
@ SOCKETSTATE_Error
similar to above but some other error. dead socket.
Definition: cNetSocket.h:50
@ SOCKETSTATE_Undefined
socket is not created or closed (by calling close()). NULL handle. !isOpen()
Definition: cNetSocket.h:42
@ SOCKETSTATE_Bound
bound to an address:port.
Definition: cNetSocket.h:44
@ SOCKETSTATE_Established
The port is ready to receive/send data from/to the remote peer. maybe accept() was called.
Definition: cNetSocket.h:47
@ SOCKETSTATE_Disconnected
error indicates a remote disconnect. dead socket. not functional socket.
Definition: cNetSocket.h:49
@ SOCKETSTATE_Closing
we are closing this locally. flushing. and going to call close().
Definition: cNetSocket.h:48
UNITTEST2_PREDEF(cQuadtree)
int IPPROTO_TYPE
enum of IPPROTO_TCP,IPPROTO_UDP, etc. getprotobyname() and getprotobynumber()
Definition: cNetSocket.h:34
SOCKET_ShutDown_TYPE
Definition: cNetSocket.h:53
@ SOCKET_ShutDown_receives
Definition: cNetSocket.h:53
@ SOCKET_ShutDown_sends
Definition: cNetSocket.h:53
@ SOCKET_ShutDown_both
Definition: cNetSocket.h:53
int ITERATE_t
like size_t but signed
Definition: Index.h:28
int TIMESECD_t
signed delta seconds. like TIMESEC_t. redefined in TimeUnits.h.
Definition: cTimeSys.h:19
UINT_PTR HASHCODE_t
could hold a pointer converted to a number? maybe 64 or 32 bit ? same as size_t.
Definition: GrayCore.h:116
Definition: cDebugAssert.h:29