Nebula
Loading...
Searching...
No Matches
posixsocket.h
Go to the documentation of this file.
1#pragma once
2#ifndef POSIX_POSIXSOCKET_H
3#define POSIX_POSIXSOCKET_H
4//------------------------------------------------------------------------------
16#include "core/refcounted.h"
18#include <sys/socket.h>
19#include <netinet/tcp.h>
20
21namespace Net
22{
23class Socket;
24}
25
26//------------------------------------------------------------------------------
27namespace Posix
28{
29
30typedef int SOCKET;
31
33{
35public:
38 {
39 TCP, // a reliable TCP connection
40 UDP, // unreliable UDP connection
41 };
42
43 // return values
44 enum Result
45 {
46 Error = 0, // an error occured, call GetErrorCode() or GetErrorString() for more info
47 Success, // everything ok
48 WouldBlock, // operation on non-blocking socket is not complete
49 Closed, // connection has been gracefully closed
50 };
51
54 {
55 ErrorNone, // no error occured
56 ErrorUnknown, // an "unknown" error occured
60 ErrorBadAddress, // EFAULT
63 ErrorWouldBlock, // EWOULDBLOCK
64 ErrorInProgress, // EINPROGRESS
66 ErrorNotASocket, // ENOTSOCK
67 ErrorDestAddrRequired, // EDESTADDRREQ
68 ErrorMsgTooLong, // EMSGSIZE
69 ErrorInvalidProtocol, // EPROTOTYPE
70 ErrorBadProtocolOption, // ENOPROTOOPT
71 ErrorProtocolNotSupported, // WSEAPROTONOSUPPORT
72 ErrorSocketTypeNotSupported, // ESOCKTNOSUPPORT
76 ErrorAddrInUse, // EADDRINUSE
77 ErrorAddrNotAvailable, // EADDRNOTAVAIL
78 ErrorNetDown, // ENETDOWN
79 ErrorNetUnreachable, // ENETUNREACH
80 ErrorNetReset, // ENETRESET
81 ErrorConnectionAborted, // ECONNABORTED
82 ErrorConnectionReset, // ECONNRESET
84 ErrorIsConnected, // EISCONN
85 ErrorNotConnected, // ENOTCONNECTED
86 ErrorIsShutdown, // ESHUTDOWN
87 ErrorIsTimedOut, // ETIMEDOUT
88 ErrorConnectionRefused, // ECONNREFUSED
89 ErrorHostDown, // EHOSTDOWN
90 ErrorHostUnreachable, // EHOSTUNREACH
91 ErrorSystemNotReady, // ESYSNOTREADY
92 ErrorVersionNotSupported, // EVERNOTSUPPORTED
93 ErrorNotInitialized, // ENOTINITIALISED
94 ErrorDisconnecting, // EDISCONN
95 ErrorTypeNotFound, // ETYPE_NOT_FOUND
96 ErrorHostNotFound, // EHOST_NOT_FOUND
97 ErrorTryAgain, // ETRY_AGAIN
98 ErrorNoRecovery, // ENO_RECOVERY
99 ErrorNoData, // ENO_DATA
100 };
101
103 PosixSocket();
105 virtual ~PosixSocket();
106
108 bool Open(Protocol p);
110 void Close();
112 bool IsOpen() const;
114 ErrorCode GetErrorCode() const;
117
119 void SetAddress(const Net::IpAddress& a);
121 const Net::IpAddress& GetAddress() const;
122
124 void SetBroadcast(bool b);
126 bool GetBroadcast();
128 void SetDontLinger(bool b);
130 bool GetDontLinger();
132 void SetKeepAlive(bool b);
134 bool GetKeepAlive();
136 void SetReUseAddr(bool b);
138 bool GetReUseAddr();
140 void SetNoDelay(bool b);
142 bool GetNoDelay();
144 void SetRecvBufSize(SizeT s);
148 void SetSendBufSize(SizeT s);
152 void SetBlocking(bool b);
154 bool GetBlocking() const;
157
159 bool Bind();
161 bool IsBound() const;
163 bool Listen();
165 bool Accept(Ptr<Net::Socket>& outSocket);
167 Result Connect();
169 bool IsConnected();
171 Result Send(const void* buf, SizeT numBytes, SizeT& bytesSent);
173 bool HasRecvData();
175 Result Recv(void* buf, SizeT bufSize, SizeT& bytesReceived);
177 Result SendTo(const void* buf, SizeT numBytes, uint addr, ushort port, SizeT& bytesSent);
179 Result RecvFrom(void* buf, SizeT bufSize, uint addr, ushort port, SizeT& bytesReceived);
180
181private:
182 friend class PosixIpAddress;
183 friend class SysFunc;
184
186 static void InitNetwork();
188 void OpenWithExistingSocket(int s);
190 void ClearError();
194 void SetSocketError(int wsaError);
196 static ErrorCode SocketErrorToErrorCode(int wsaError);
198 static ErrorCode HostErrorToErrorCode(int wsaError);
202 static Util::String SocketErrorToString(int wsaError);
204 static Util::String HostErrorToString(int wsaError);
206 void SetBoolOption(int optName, bool val);
208 bool GetBoolOption(int optName);
210 void SetIntOption(int optName, int val);
212 int GetIntOption(int optName);
213
220};
221
222//------------------------------------------------------------------------------
225inline bool
227{
228 return (0 != this->sock);
229}
230
231//------------------------------------------------------------------------------
234inline bool
236{
237 return this->isBound;
238}
239
240//------------------------------------------------------------------------------
244inline void
246{
247 this->addr = a;
248}
249
250//------------------------------------------------------------------------------
254inline const Net::IpAddress&
256{
257 return this->addr;
258}
259
260//------------------------------------------------------------------------------
265{
266 return this->error;
267}
268
269//------------------------------------------------------------------------------
272inline void
274{
275 this->SetBoolOption(SO_BROADCAST, b);
276}
277
278//------------------------------------------------------------------------------
281inline bool
283{
284 return this->GetBoolOption(SO_BROADCAST);
285}
286
287//------------------------------------------------------------------------------
290inline void
292{
293 this->SetBoolOption(SO_LINGER, !b);
294}
295
296//------------------------------------------------------------------------------
299inline bool
301{
302 return !this->GetBoolOption(SO_LINGER);
303}
304
305//------------------------------------------------------------------------------
308inline void
310{
311 this->SetBoolOption(SO_KEEPALIVE, b);
312}
313
314//------------------------------------------------------------------------------
317inline bool
319{
320 return this->GetBoolOption(SO_KEEPALIVE);
321}
322
323//------------------------------------------------------------------------------
326inline void
328{
329 this->SetBoolOption(SO_REUSEADDR, b);
330}
331
332//------------------------------------------------------------------------------
335inline bool
337{
338 return this->GetBoolOption(SO_REUSEADDR);
339}
340
341//------------------------------------------------------------------------------
344inline void
346{
347 this->SetBoolOption(TCP_NODELAY, b);
348}
349
350//------------------------------------------------------------------------------
353inline bool
355{
356 return this->GetBoolOption(TCP_NODELAY);
357}
358
359//------------------------------------------------------------------------------
362inline void
364{
365 this->SetIntOption(SO_RCVBUF, s);
366}
367
368//------------------------------------------------------------------------------
371inline SizeT
373{
374 return this->GetIntOption(SO_RCVBUF);
375}
376
377//------------------------------------------------------------------------------
380inline void
382{
383 this->SetIntOption(SO_SNDBUF, s);
384}
385
386//------------------------------------------------------------------------------
389inline SizeT
391{
392 return this->GetIntOption(SO_SNDBUF);
393}
394
395//------------------------------------------------------------------------------
398inline SizeT
400{
401 return 1500; // XXX: Revisit this.
402 // return this->GetIntOption(SO_MAX_MSG_SIZE);
403}
404
405//------------------------------------------------------------------------------
408inline bool
410{
411 return this->isBlocking;
412}
413
414} // namespace Posix
415//------------------------------------------------------------------------------
416#endif
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.
Represents an IP address, consisting of a IPv4 host address and a port number.
Definition posixipaddress.h:35
A lowlevel socket wrapper class.
Definition posixsocket.h:33
static Util::String ErrorAsString(ErrorCode err)
convert error code to human readable string
Definition posixsocket.cc:678
bool GetNoDelay()
get nodelay flag
Definition posixsocket.h:354
SizeT GetSendBufSize()
get send buffer size
Definition posixsocket.h:390
bool GetKeepAlive()
get the keepalive flag
Definition posixsocket.h:318
ErrorCode GetErrorCode() const
get the last error code
Definition posixsocket.h:264
bool Accept(Ptr< Net::Socket > &outSocket)
accept incoming connection, return a new socket (for server sockets)
Definition posixsocket.cc:322
bool IsBound() const
return true if the socket is bound to an address
Definition posixsocket.h:235
bool Bind()
bind socket to ip address
Definition posixsocket.cc:276
void SetRecvBufSize(SizeT s)
set receive buffer size
Definition posixsocket.h:363
Result Recv(void *buf, SizeT bufSize, SizeT &bytesReceived)
receive raw data from the socket
Definition posixsocket.cc:507
bool Open(Protocol p)
open the socket
Definition posixsocket.cc:65
ErrorCode error
Definition posixsocket.h:215
int GetIntOption(int optName)
get an integer socket option
Definition posixsocket.cc:220
static void InitNetwork()
static initializer method (called only once)
Definition posixsocket.cc:55
Protocol
protocol types
Definition posixsocket.h:38
@ UDP
Definition posixsocket.h:40
@ TCP
Definition posixsocket.h:39
void SetNoDelay(bool b)
set nodelay flag (TCP_NODELAY)
Definition posixsocket.h:345
void SetSendBufSize(SizeT s)
set send buffer size
Definition posixsocket.h:381
void SetKeepAlive(bool b)
set the keepalive flag (SO_KEEPALIVE)
Definition posixsocket.h:309
void SetBroadcast(bool b)
set the broadcast flag (SO_BROADCAST)
Definition posixsocket.h:273
__DeclareClass(PosixSocket)
void SetSocketError(int wsaError)
set socket error code
Definition posixsocket.cc:598
bool isBlocking
Definition posixsocket.h:218
static ErrorCode SocketErrorToErrorCode(int wsaError)
convert a socket error code to internal error code
Definition posixsocket.cc:609
static Util::String HostErrorToString(int wsaError)
convert host error code directly to string
Definition posixsocket.cc:746
Result Send(const void *buf, SizeT numBytes, SizeT &bytesSent)
send raw data into the socket
Definition posixsocket.cc:437
void SetBlocking(bool b)
set blocking mode (FIONBIO)
Definition posixsocket.cc:241
Result Connect()
connect to the sockets address (for client sockets)
Definition posixsocket.cc:359
PosixSocket()
constructor
Definition posixsocket.cc:26
void SetReUseAddr(bool b)
set reuseaddr flag (SO_REUSEADDR)
Definition posixsocket.h:327
Result SendTo(const void *buf, SizeT numBytes, uint addr, ushort port, SizeT &bytesSent)
send raw data to address for connectionless sockets
Definition posixsocket.cc:555
void SetToLastSocketError()
set error code to errno;
Definition posixsocket.cc:588
const Net::IpAddress & GetAddress() const
get internet address of socket
Definition posixsocket.h:255
bool IsOpen() const
return true if the socket is open
Definition posixsocket.h:226
static ErrorCode HostErrorToErrorCode(int wsaError)
convert a host (gethostbyname) error code to internal error code
Definition posixsocket.cc:659
void OpenWithExistingSocket(int s)
open with an existing socket (called by Accept())
Definition posixsocket.cc:107
static Util::String SocketErrorToString(int wsaError)
convert socket error code directly to string
Definition posixsocket.cc:737
SOCKET sock
Definition posixsocket.h:216
bool GetBlocking() const
get blocking mode
Definition posixsocket.h:409
bool HasRecvData()
return true if recv data is available at the socket
Definition posixsocket.cc:467
bool isBound
Definition posixsocket.h:219
void SetBoolOption(int optName, bool val)
set a bool socket option
Definition posixsocket.cc:153
bool GetBroadcast()
get the broadcast flag
Definition posixsocket.h:282
virtual ~PosixSocket()
destructor
Definition posixsocket.cc:42
void Close()
close the socket
Definition posixsocket.cc:118
void SetDontLinger(bool b)
set the don't linger flag (SO_DONTLINGER)
Definition posixsocket.h:291
ErrorCode
error codes
Definition posixsocket.h:54
@ ErrorHostUnreachable
Definition posixsocket.h:90
@ ErrorConnectionAborted
Definition posixsocket.h:81
@ ErrorUnknown
Definition posixsocket.h:56
@ ErrorNetUnreachable
Definition posixsocket.h:79
@ ErrorNetDown
Definition posixsocket.h:78
@ ErrorVersionNotSupported
Definition posixsocket.h:92
@ ErrorNotInitialized
Definition posixsocket.h:93
@ ErrorOperationNotSupported
Definition posixsocket.h:73
@ ErrorAddrFamilyNotSupported
Definition posixsocket.h:75
@ ErrorInProgress
Definition posixsocket.h:64
@ ErrorConnectionRefused
Definition posixsocket.h:88
@ ErrorProtFamilyNotSupported
Definition posixsocket.h:74
@ ErrorInterrupted
Definition posixsocket.h:57
@ ErrorIsConnected
Definition posixsocket.h:84
@ ErrorPermissionDenied
Definition posixsocket.h:59
@ ErrorNoRecovery
Definition posixsocket.h:98
@ ErrorIsShutdown
Definition posixsocket.h:86
@ ErrorHostNotFound
Definition posixsocket.h:96
@ ErrorSocketTypeNotSupported
Definition posixsocket.h:72
@ ErrorTypeNotFound
Definition posixsocket.h:95
@ ErrorInvalidArgument
Definition posixsocket.h:61
@ ErrorAddrInUse
Definition posixsocket.h:76
@ ErrorAlreadyInProgress
Definition posixsocket.h:65
@ ErrorNone
Definition posixsocket.h:55
@ ErrorNetReset
Definition posixsocket.h:80
@ ErrorWouldBlock
Definition posixsocket.h:63
@ ErrorAddrNotAvailable
Definition posixsocket.h:77
@ ErrorNotConnected
Definition posixsocket.h:85
@ ErrorIsTimedOut
Definition posixsocket.h:87
@ ErrorDestAddrRequired
Definition posixsocket.h:67
@ ErrorMsgTooLong
Definition posixsocket.h:68
@ ErrorBadAddress
Definition posixsocket.h:60
@ ErrorHostDown
Definition posixsocket.h:89
@ ErrorNotASocket
Definition posixsocket.h:66
@ ErrorNoBufferSpace
Definition posixsocket.h:83
@ ErrorTooManyOpenFiles
Definition posixsocket.h:62
@ ErrorConnectionReset
Definition posixsocket.h:82
@ ErrorBadProtocolOption
Definition posixsocket.h:70
@ ErrorSystemNotReady
Definition posixsocket.h:91
@ ErrorInvalidProtocol
Definition posixsocket.h:69
@ ErrorDisconnecting
Definition posixsocket.h:94
@ ErrorNoData
Definition posixsocket.h:99
@ ErrorTryAgain
Definition posixsocket.h:97
@ ErrorProtocolNotSupported
Definition posixsocket.h:71
@ ErrorBrokenPipe
Definition posixsocket.h:58
Result
Definition posixsocket.h:45
@ Success
Definition posixsocket.h:47
@ WouldBlock
Definition posixsocket.h:48
@ Closed
Definition posixsocket.h:49
@ Error
Definition posixsocket.h:46
Result RecvFrom(void *buf, SizeT bufSize, uint addr, ushort port, SizeT &bytesReceived)
receive raw data from address for connectionless sockets
Definition posixsocket.cc:567
bool GetBoolOption(int optName)
get a bool socket option
Definition posixsocket.cc:177
SizeT GetRecvBufSize()
get receive buffer size
Definition posixsocket.h:372
bool GetReUseAddr()
get reuseaddr flag
Definition posixsocket.h:336
void SetIntOption(int optName, int val)
set an integer socket option
Definition posixsocket.cc:203
bool IsConnected()
test if the socket is currently connected
Definition posixsocket.cc:403
void ClearError()
clear the last error code
Definition posixsocket.cc:578
Util::String GetErrorString() const
get the last error string
Definition posixsocket.cc:755
void SetAddress(const Net::IpAddress &a)
set internet address of socket
Definition posixsocket.h:245
static bool NetworkInitialized
Definition posixsocket.h:214
bool GetDontLinger()
get the don't linger flag
Definition posixsocket.h:300
SizeT GetMaxMsgSize()
get the maximum message size that can be sent atomically
Definition posixsocket.h:399
Net::IpAddress addr
Definition posixsocket.h:217
bool Listen()
listen for incoming connections (for server sockets)
Definition posixsocket.cc:299
Provides Posix specific helper functions.
Definition posixsysfunc.h:25
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Definition debugmessage.h:20
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21
int SOCKET
Definition posixsocket.h:30
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