Nebula
Loading...
Searching...
No Matches
win32socket.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
15#include "core/refcounted.h"
17
18namespace Net
19{
20class Socket;
21}
22
23//------------------------------------------------------------------------------
24namespace Win32
25{
27{
29public:
32 {
33 TCP, // a reliable TCP connection
34 UDP, // unreliable UDP connection
35 };
36
37 // return values
38 enum Result
39 {
40 Error = 0, // an error occured, call GetErrorCode() or GetErrorString() for more info
41 Success, // everything ok
42 WouldBlock, // operation on non-blocking socket is not complete
43 Closed, // connection has been gracefully closed
44 };
45
48 {
49 ErrorNone, // no error occured
50 ErrorUnknown, // an "unknown" error occured
51 ErrorInterrupted, // WSAEINTR
52 ErrorPermissionDenied, // WSAEACCESS
53 ErrorBadAddress, // WSAEFAULT
56 ErrorWouldBlock, // WSAEWOULDBLOCK
57 ErrorInProgress, // WSAEINPROGRESS
58 ErrorAlreadyInProgress, // WSAEALREADY
59 ErrorNotASocket, // WSAENOTSOCK
60 ErrorDestAddrRequired, // WSAEDESTADDRREQ
61 ErrorMsgTooLong, // WSAEMSGSIZE
62 ErrorInvalidProtocol, // WSAEPROTOTYPE
63 ErrorBadProtocolOption, // WSAENOPROTOOPT
64 ErrorProtocolNotSupported, // WSEAPROTONOSUPPORT
65 ErrorSocketTypeNotSupported, // WSAESOCKTNOSUPPORT
67 ErrorProtFamilyNotSupported, // WSAEPFNOSUPPORT
68 ErrorAddrFamilyNotSupported, // WSAEAFNOSUPPORT
69 ErrorAddrInUse, // WSAEADDRINUSE
70 ErrorAddrNotAvailable, // WSAEADDRNOTAVAIL
71 ErrorNetDown, // WSAENETDOWN
72 ErrorNetUnreachable, // WSAENETUNREACH
73 ErrorNetReset, // WSAENETRESET
74 ErrorConnectionAborted, // WSAECONNABORTED
75 ErrorConnectionReset, // WSAECONNRESET
76 ErrorNoBufferSpace, // WSAENOBUFS
77 ErrorIsConnected, // WSAEISCONN
78 ErrorNotConnected, // WSAENOTCONNECTED
79 ErrorIsShutdown, // WSAESHUTDOWN
80 ErrorIsTimedOut, // WSAETIMEDOUT
81 ErrorConnectionRefused, // WSAECONNREFUSED
82 ErrorHostDown, // WSAEHOSTDOWN
83 ErrorHostUnreachable, // WSAEHOSTUNREACH
84 ErrorTooManyProcesses, // WSAEPROCLIM
85 ErrorSystemNotReady, // WSAESYSNOTREADY
86 ErrorVersionNotSupported, // WSAEVERNOTSUPPORTED
87 ErrorNotInitialized, // WSAENOTINITIALISED
88 ErrorDisconnecting, // WSAEDISCONN
89 ErrorTypeNotFound, // WSAETYPE_NOT_FOUND
90 ErrorHostNotFound, // WSAEHOST_NOT_FOUND
91 ErrorTryAgain, // WSAETRY_AGAIN
92 ErrorNoRecovery, // WSAENO_RECOVERY
93 ErrorNoData, // WSAENO_DATA
94 };
95
99 virtual ~Win32Socket();
101 static void InitNetwork();
103 static bool IsNetworkInitialized();
104
106 bool Open(Protocol p);
108 void Close();
110 bool IsOpen() const;
112 ErrorCode GetErrorCode() const;
115
117 void SetAddress(const Net::IpAddress& a);
119 const Net::IpAddress& GetAddress() const;
120
122 void SetBroadcast(bool b);
124 bool GetBroadcast();
126 void SetKeepAlive(bool b);
128 bool GetKeepAlive();
130 void SetReUseAddr(bool b);
132 bool GetReUseAddr();
134 void SetNoDelay(bool b);
136 bool GetNoDelay();
138 void SetRecvBufSize(SizeT s);
142 void SetSendBufSize(SizeT s);
146 void SetBlocking(bool b);
148 bool GetBlocking() const;
151
153 bool Bind();
155 bool IsBound() const;
157 bool Listen();
159 bool Accept(Ptr<Net::Socket>& outSocket);
161 Result Connect();
163 bool IsConnected();
165 Result Send(const void* buf, SizeT numBytes, SizeT& bytesSent);
167 bool HasRecvData();
169 Result Recv(void* buf, SizeT bufSize, SizeT& bytesReceived);
171 Result SendTo(const void* buf, SizeT numBytes, uint addr, ushort port, SizeT& bytesSent);
173 Result RecvFrom(void* buf, SizeT bufSize, uint addr, ushort port, SizeT& bytesReceived);
174
175private:
176 friend class Win32IpAddress;
177
179 void OpenWithExistingSocket(SOCKET s);
181 void ClearError();
183 void SetToLastWSAError();
185 void SetWSAError(int wsaError);
187 static ErrorCode WSAErrorToErrorCode(int wsaError);
191 static Util::String WSAErrorToString(int wsaError);
193 void SetBoolOption(int optName, bool val);
195 bool GetBoolOption(int optName);
197 void SetIntOption(int optName, int val);
199 int GetIntOption(int optName);
200
203 SOCKET sock;
207};
208
209//------------------------------------------------------------------------------
212inline bool
214{
215 return (0 != this->sock);
216}
217
218//------------------------------------------------------------------------------
221inline bool
223{
224 return this->isBound;
225}
226
227//------------------------------------------------------------------------------
231inline void
233{
234 this->addr = a;
235}
236
237//------------------------------------------------------------------------------
241inline const Net::IpAddress&
243{
244 return this->addr;
245}
246
247//------------------------------------------------------------------------------
252{
253 return this->error;
254}
255
256//------------------------------------------------------------------------------
259inline void
261{
262 this->SetBoolOption(SO_BROADCAST, b);
263}
264
265//------------------------------------------------------------------------------
268inline bool
270{
271 return this->GetBoolOption(SO_BROADCAST);
272}
273
274//------------------------------------------------------------------------------
277inline void
279{
280 this->SetBoolOption(SO_KEEPALIVE, b);
281}
282
283//------------------------------------------------------------------------------
286inline bool
288{
289 return this->GetBoolOption(SO_KEEPALIVE);
290}
291
292//------------------------------------------------------------------------------
295inline void
297{
298 this->SetBoolOption(SO_REUSEADDR, b);
299}
300
301//------------------------------------------------------------------------------
304inline bool
306{
307 return this->GetBoolOption(SO_REUSEADDR);
308}
309
310//------------------------------------------------------------------------------
313inline void
315{
316 this->SetBoolOption(TCP_NODELAY, b);
317}
318
319//------------------------------------------------------------------------------
322inline bool
324{
325 return this->GetBoolOption(TCP_NODELAY);
326}
327
328//------------------------------------------------------------------------------
331inline void
333{
334 this->SetIntOption(SO_RCVBUF, s);
335}
336
337//------------------------------------------------------------------------------
340inline SizeT
342{
343 return this->GetIntOption(SO_RCVBUF);
344}
345
346//------------------------------------------------------------------------------
349inline void
351{
352 this->SetIntOption(SO_SNDBUF, s);
353}
354
355//------------------------------------------------------------------------------
358inline SizeT
360{
361 return this->GetIntOption(SO_SNDBUF);
362}
363
364//------------------------------------------------------------------------------
367inline SizeT
369{
370 return 8192;
371}
372
373//------------------------------------------------------------------------------
376inline bool
378{
379 return this->isBlocking;
380}
381
382} // namespace Win32
383//------------------------------------------------------------------------------
The common base class of Nebula.
Definition refcounted.h:38
Represents an IP address, consisting of a IPv4 host address and a port number.
Platform independent wrapper class for the Sockets API.
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Represents an IP address, consisting of a IPv4 host address and a port number.
Definition win32ipaddress.h:32
A lowlevel socket wrapper class.
Definition win32socket.h:27
__DeclareClass(Win32Socket)
bool GetBroadcast()
get the broadcast flag
Definition win32socket.h:269
SizeT GetMaxMsgSize()
get the maximum message size that can be sent atomically
Definition win32socket.h:368
Net::IpAddress addr
Definition win32socket.h:204
bool GetReUseAddr()
get reuseaddr flag
Definition win32socket.h:305
Win32Socket()
constructor
Definition win32socket.cc:19
Result
Definition win32socket.h:39
@ Success
Definition win32socket.h:41
@ Closed
Definition win32socket.h:43
@ Error
Definition win32socket.h:40
@ WouldBlock
Definition win32socket.h:42
bool GetBlocking() const
get blocking mode
Definition win32socket.h:377
bool IsOpen() const
return true if the socket is open
Definition win32socket.h:213
static Util::String ErrorAsString(ErrorCode err)
convert error code to human readable string
Definition win32socket.cc:664
void Close()
close the socket
Definition win32socket.cc:117
ErrorCode
error codes
Definition win32socket.h:48
@ ErrorDestAddrRequired
Definition win32socket.h:60
@ ErrorIsShutdown
Definition win32socket.h:79
@ ErrorAddrFamilyNotSupported
Definition win32socket.h:68
@ ErrorProtFamilyNotSupported
Definition win32socket.h:67
@ ErrorVersionNotSupported
Definition win32socket.h:86
@ ErrorBadProtocolOption
Definition win32socket.h:63
@ ErrorHostDown
Definition win32socket.h:82
@ ErrorSystemNotReady
Definition win32socket.h:85
@ ErrorMsgTooLong
Definition win32socket.h:61
@ ErrorAddrInUse
Definition win32socket.h:69
@ ErrorIsConnected
Definition win32socket.h:77
@ ErrorNetDown
Definition win32socket.h:71
@ ErrorInProgress
Definition win32socket.h:57
@ ErrorHostNotFound
Definition win32socket.h:90
@ ErrorPermissionDenied
Definition win32socket.h:52
@ ErrorWouldBlock
Definition win32socket.h:56
@ ErrorIsTimedOut
Definition win32socket.h:80
@ ErrorTooManyProcesses
Definition win32socket.h:84
@ ErrorNoData
Definition win32socket.h:93
@ ErrorSocketTypeNotSupported
Definition win32socket.h:65
@ ErrorInterrupted
Definition win32socket.h:51
@ ErrorTooManyOpenFiles
Definition win32socket.h:55
@ ErrorNetUnreachable
Definition win32socket.h:72
@ ErrorHostUnreachable
Definition win32socket.h:83
@ ErrorNotConnected
Definition win32socket.h:78
@ ErrorDisconnecting
Definition win32socket.h:88
@ ErrorConnectionReset
Definition win32socket.h:75
@ ErrorConnectionRefused
Definition win32socket.h:81
@ ErrorTryAgain
Definition win32socket.h:91
@ ErrorNotASocket
Definition win32socket.h:59
@ ErrorNetReset
Definition win32socket.h:73
@ ErrorNoBufferSpace
Definition win32socket.h:76
@ ErrorNotInitialized
Definition win32socket.h:87
@ ErrorAlreadyInProgress
Definition win32socket.h:58
@ ErrorProtocolNotSupported
Definition win32socket.h:64
@ ErrorNone
Definition win32socket.h:49
@ ErrorOperationNotSupported
Definition win32socket.h:66
@ ErrorConnectionAborted
Definition win32socket.h:74
@ ErrorBadAddress
Definition win32socket.h:53
@ ErrorInvalidArgument
Definition win32socket.h:54
@ ErrorTypeNotFound
Definition win32socket.h:89
@ ErrorInvalidProtocol
Definition win32socket.h:62
@ ErrorUnknown
Definition win32socket.h:50
@ ErrorNoRecovery
Definition win32socket.h:92
@ ErrorAddrNotAvailable
Definition win32socket.h:70
bool isBlocking
Definition win32socket.h:205
ErrorCode GetErrorCode() const
get the last error code
Definition win32socket.h:251
int GetIntOption(int optName)
get an integer socket option
Definition win32socket.cc:219
void SetNoDelay(bool b)
set nodelay flag (TCP_NODELAY)
Definition win32socket.h:314
void SetKeepAlive(bool b)
set the keepalive flag (SO_KEEPALIVE)
Definition win32socket.h:278
bool Accept(Ptr< Net::Socket > &outSocket)
accept incoming connection, return a new socket (for server sockets)
Definition win32socket.cc:308
static ErrorCode WSAErrorToErrorCode(int wsaError)
convert a WSA error code to internal error code
Definition win32socket.cc:606
Result RecvFrom(void *buf, SizeT bufSize, uint addr, ushort port, SizeT &bytesReceived)
receive raw data from address for connectionless sockets
Definition win32socket.cc:564
void SetBroadcast(bool b)
set the broadcast flag (SO_BROADCAST)
Definition win32socket.h:260
Result Send(const void *buf, SizeT numBytes, SizeT &bytesSent)
send raw data into the socket
Definition win32socket.cc:431
bool GetBoolOption(int optName)
get a bool socket option
Definition win32socket.cc:176
SOCKET sock
Definition win32socket.h:203
void SetToLastWSAError()
set error code to WSAGetLastError();
Definition win32socket.cc:585
bool IsBound() const
return true if the socket is bound to an address
Definition win32socket.h:222
void SetReUseAddr(bool b)
set reuseaddr flag (SO_REUSEADDR)
Definition win32socket.h:296
Protocol
protocol types
Definition win32socket.h:32
@ UDP
Definition win32socket.h:34
@ TCP
Definition win32socket.h:33
void ClearError()
clear the last error code
Definition win32socket.cc:575
Util::String GetErrorString() const
get the last error string
Definition win32socket.cc:732
void SetAddress(const Net::IpAddress &a)
set internet address of socket
Definition win32socket.h:232
static Util::String WSAErrorToString(int wsaError)
convert WSA error code directly to string
Definition win32socket.cc:723
SizeT GetRecvBufSize()
get receive buffer size
Definition win32socket.h:341
void OpenWithExistingSocket(SOCKET s)
open with an existing socket (called by Accept())
Definition win32socket.cc:106
void SetRecvBufSize(SizeT s)
set receive buffer size
Definition win32socket.h:332
static bool IsNetworkInitialized()
is network initialized
Definition win32socket.cc:741
bool Open(Protocol p)
open the socket
Definition win32socket.cc:64
void SetBlocking(bool b)
set blocking mode (FIONBIO)
Definition win32socket.cc:240
static bool NetworkInitialized
Definition win32socket.h:201
static void InitNetwork()
static initializer method (called by SysFunc::Setup())
Definition win32socket.cc:46
Result SendTo(const void *buf, SizeT numBytes, uint addr, ushort port, SizeT &bytesSent)
send raw data to address for connectionless sockets
Definition win32socket.cc:552
bool HasRecvData()
return true if recv data is available at the socket
Definition win32socket.cc:465
Result Recv(void *buf, SizeT bufSize, SizeT &bytesReceived)
receive raw data from the socket
Definition win32socket.cc:503
bool GetKeepAlive()
get the keepalive flag
Definition win32socket.h:287
SizeT GetSendBufSize()
get send buffer size
Definition win32socket.h:359
bool Bind()
bind socket to ip address
Definition win32socket.cc:262
void SetWSAError(int wsaError)
set WSA error code
Definition win32socket.cc:595
void SetIntOption(int optName, int val)
set an integer socket option
Definition win32socket.cc:202
Result Connect()
connect to the sockets address (for client sockets)
Definition win32socket.cc:355
const Net::IpAddress & GetAddress() const
get internet address of socket
Definition win32socket.h:242
bool Listen()
listen for incoming connections (for server sockets)
Definition win32socket.cc:285
bool isBound
Definition win32socket.h:206
virtual ~Win32Socket()
destructor
Definition win32socket.cc:31
bool GetNoDelay()
get nodelay flag
Definition win32socket.h:323
ErrorCode error
Definition win32socket.h:202
void SetSendBufSize(SizeT s)
set send buffer size
Definition win32socket.h:350
void SetBoolOption(int optName, bool val)
set a bool socket option
Definition win32socket.cc:152
bool IsConnected()
test if the socket is currently connected
Definition win32socket.cc:399
Definition debugmessage.h:20
[TODO: Describe Win32 subsystem]
Nebula's universal string class.
Definition string.h:50
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31
unsigned short ushort
Definition types.h:32