56 #if defined(__linux__) || defined (_DARWIN) 57 #include <sys/socket.h> 58 #include <netinet/in.h> 59 #include <arpa/inet.h> 60 #include <netinet/tcp.h> 61 #include <netinet/ip.h> 65 #include <linux/if_packet.h> 66 #include <linux/if_ether.h> 68 #include <sys/sendfile.h> 73 #if defined(__linux__) || defined (_DARWIN) 78 #include <sys/ioctl.h> 86 # pragma warning(disable: 4786) 87 # pragma comment(lib, "ws2_32.lib") 89 #define IPTOS_LOWDELAY 0x10 93 #include "StatTimer.h" 94 #include "ChannelDevice.h" 99 #ifndef INVALID_SOCKET 100 #define INVALID_SOCKET ~(0) 103 #define SOCKET_SENDFILE_BLOCKSIZE 8192 180 virtual bool Close(
void);
188 virtual bool Shutdown(CShutdownMode nShutdown);
207 virtual bool Select(int32_t nTimeoutSec, int32_t nTimeoutUSec);
210 virtual int WaitForData(
size_t data_count, uint32_t timeout,
size_t * returned_size);
227 inline const char *DescribeError() {
239 virtual int32_t
Receive(int32_t nMaxBytes = 1, uint8_t * pBuffer = 0);
247 virtual int32_t
Send(
const uint8_t *pBuf,
size_t bytesToSend);
257 virtual int32_t
Send(
const struct iovec *sendVector, int32_t nNumItems);
271 virtual int32_t
SendFile(int32_t nOutFd, int32_t nInFd, off_t *pOffset, int32_t nCount);
393 bool SetMulticast(
bool bEnable, uint8_t multicastTTL = 1);
422 bool SetSendTimeout(int32_t nSendTimeoutSec, int32_t nSendTimeoutUsec = 0);
435 return m_timer.GetMilliSeconds();
441 return m_timer.GetMicroSeconds();
500 return GetWindowSize(SO_RCVBUF);
507 return GetWindowSize(SO_SNDBUF);
514 return SetWindowSize(SO_RCVBUF, nWindowSize);
521 return SetWindowSize(SO_SNDBUF, nWindowSize);
532 virtual bool Open(
const char *pAddr, uint16_t nPort){
return true;}
534 virtual bool bindport(
const char*, uint32_t );
538 virtual bool isOpen();
540 virtual void closePort();
542 virtual void flush();
544 virtual int waitfordata(
size_t data_count,uint32_t timeout = -1,
size_t * returned_size = NULL);
546 virtual size_t writeData(
const uint8_t * data,
size_t size);
548 virtual size_t readData(uint8_t * data,
size_t size);
571 uint32_t GetWindowSize(uint32_t nOptionName);
575 uint32_t SetWindowSize(uint32_t nOptionName, uint32_t nWindowSize);
587 int32_t Writev(
const struct iovec *pVector,
size_t nCount);
virtual int32_t SendFile(int32_t nOutFd, int32_t nInFd, off_t *pOffset, int32_t nCount)
Definition: SimpleSocket.cpp:931
Defines socket as IPv6 TCP socket.
Definition: SimpleSocket.h:130
Definition: ActiveSocket.h:48
Shutdown active socket.
Definition: SimpleSocket.h:120
int GetSocketDscp(void)
Definition: SimpleSocket.cpp:294
The connection has been aborted.
Definition: SimpleSocket.h:149
int32_t GetSendTimeoutUSec(void)
Definition: SimpleSocket.h:415
virtual bool Select(void)
Definition: SimpleSocket.h:196
int32_t m_nBufferSize
internal send/receive buffer
Definition: SimpleSocket.h:597
fd_set m_readFds
write file descriptor set
Definition: SimpleSocket.h:617
Defines socket as IPv6 UDP socket.
Definition: SimpleSocket.h:131
int32_t GetBytesReceived(void)
Definition: SimpleSocket.h:298
CStatTimer m_timer
linger flag
Definition: SimpleSocket.h:612
Connection was forcibly closed by the remote host.
Definition: SimpleSocket.h:153
uint16_t GetClientPort()
Definition: SimpleSocket.h:480
int32_t GetBytesSent(void)
Definition: SimpleSocket.h:305
uint8_t * GetData(void)
Definition: SimpleSocket.h:291
CSocketType
Defines the socket types defined by CSimpleSocket class.
Definition: SimpleSocket.h:125
Socket is non-blocking and the connection cannot be completed immediately.
Definition: SimpleSocket.h:147
Operation would block if socket were blocking.
Definition: SimpleSocket.h:145
CSocketError GetSocketError(void)
Definition: SimpleSocket.h:428
struct sockaddr_in m_stMulticastGroup
client address
Definition: SimpleSocket.h:610
virtual bool IsSocketValid(void)
Definition: SimpleSocket.h:215
bool SetMulticast(bool bEnable, uint8_t multicastTTL=1)
Definition: SimpleSocket.cpp:245
Definition: ChannelDevice.h:4
uint32_t GetReceiveWindowSize()
Definition: SimpleSocket.h:499
bool EnableNagleAlgoritm()
Definition: SimpleSocket.cpp:391
Definition: SimpleSocket.h:114
Invalid socket handle.
Definition: SimpleSocket.h:140
void SetSocketError(CSimpleSocket::CSocketError error)
Definition: SimpleSocket.h:554
virtual bool Shutdown(CShutdownMode nShutdown)
Definition: SimpleSocket.cpp:523
Firewall rules forbid connection.
Definition: SimpleSocket.h:151
Generic socket error translates to error below.
Definition: SimpleSocket.h:138
uint8_t * m_pBuffer
number of last error
Definition: SimpleSocket.h:596
int32_t GetConnectTimeoutUSec(void)
Definition: SimpleSocket.h:342
int32_t m_nBytesReceived
socket type - UDP, TCP or RAW
Definition: SimpleSocket.h:600
std::string m_addr
error file descriptor set
Definition: SimpleSocket.h:620
int32_t m_nSocketDomain
size of internal send/receive buffer
Definition: SimpleSocket.h:598
struct sockaddr_in m_stClientSockaddr
server address
Definition: SimpleSocket.h:609
CShutdownMode
Defines the three possible states for shuting down a socket.
Definition: SimpleSocket.h:117
int32_t m_nBytesSent
number of bytes received
Definition: SimpleSocket.h:601
const char * GetServerAddr()
Definition: SimpleSocket.h:486
Unknown error please report to mark@carrierlabs.com.
Definition: SimpleSocket.h:156
struct sockaddr_in m_stServerSockaddr
send timeout
Definition: SimpleSocket.h:608
bool SetSocketDscp(int nDscp)
Definition: SimpleSocket.cpp:270
Shutdown both active and passive sockets.
Definition: SimpleSocket.h:121
The receive buffer point outside the process's address space.
Definition: SimpleSocket.h:152
Invalid protocol for operation.
Definition: SimpleSocket.h:150
uint32_t m_nFlags
number of bytes sent
Definition: SimpleSocket.h:602
bool SetNonblocking(void)
Definition: SimpleSocket.cpp:855
int32_t GetReceiveTimeoutUSec(void)
Definition: SimpleSocket.h:372
CSocketType GetSocketType()
Definition: SimpleSocket.h:463
struct linger m_stLinger
multicast group to bind to
Definition: SimpleSocket.h:611
bool BindInterface(const char *pInterface)
Definition: SimpleSocket.cpp:220
bool Flush()
Definition: SimpleSocket.cpp:539
virtual int32_t Send(const uint8_t *pBuf, size_t bytesToSend)
Definition: SimpleSocket.cpp:414
bool SetOptionLinger(bool bEnable, uint16_t nTime)
Definition: SimpleSocket.cpp:705
int32_t GetReceiveTimeoutSec(void)
Definition: SimpleSocket.h:365
Invalid socket type.
Definition: SimpleSocket.h:127
Timed out while attempting operation.
Definition: SimpleSocket.h:144
bool GetMulticast()
Definition: SimpleSocket.h:397
void SetSocketHandle(SOCKET socket)
Definition: SimpleSocket.h:560
SOCKET GetSocketDescriptor()
Definition: SimpleSocket.h:457
CSocketType m_nSocketType
socket type PF_INET, PF_INET6
Definition: SimpleSocket.h:599
virtual bool Close(void)
Definition: SimpleSocket.cpp:490
Defines socket as TCP socket.
Definition: SimpleSocket.h:128
bool SetOptionReuseAddr()
Definition: SimpleSocket.cpp:684
void SetSocketType(const CSocketType &type)
set socket descriptor
Definition: SimpleSocket.h:468
uint32_t SetSendWindowSize(uint32_t nWindowSize)
Definition: SimpleSocket.h:520
Shutdown passive socket.
Definition: SimpleSocket.h:119
Pointer type supplied as argument is invalid.
Definition: SimpleSocket.h:155
Invalid destination address specified.
Definition: SimpleSocket.h:141
Provides raw network protocol access.
Definition: SimpleSocket.h:132
No server is listening at remote address.
Definition: SimpleSocket.h:143
Address already in use.
Definition: SimpleSocket.h:154
static const char * DescribeError(CSocketError err)
Definition: SimpleSocket.cpp:1099
fd_set m_errorFds
read file descriptor set
Definition: SimpleSocket.h:618
const char * GetClientAddr()
Definition: SimpleSocket.h:474
Invalid destination port specified.
Definition: SimpleSocket.h:142
bool m_bIsMulticast
is socket blocking
Definition: SimpleSocket.h:604
virtual bool Initialize(void)
Definition: SimpleSocket.cpp:189
Call was interrupted by a signal that was caught before a valid connection arrived.
Definition: SimpleSocket.h:148
uint32_t GetSendWindowSize()
Definition: SimpleSocket.h:506
uint32_t SetReceiveWindowSize(uint32_t nWindowSize)
Definition: SimpleSocket.h:513
void TranslateSocketError(void)
Definition: SimpleSocket.cpp:973
bool DisableNagleAlgoritm()
Definition: SimpleSocket.cpp:368
struct timeval m_stConnectTimeout
is the UDP socket multicast;
Definition: SimpleSocket.h:605
bool SetBlocking(void)
Definition: SimpleSocket.cpp:894
CSocketError m_socketErrno
socket handle
Definition: SimpleSocket.h:595
int32_t GetConnectTimeoutSec(void)
Definition: SimpleSocket.h:335
void SetConnectTimeout(int32_t nConnectTimeoutSec, int32_t nConnectTimeoutUsec=0)
Definition: SimpleSocket.h:356
bool SetSendTimeout(int32_t nSendTimeoutSec, int32_t nSendTimeoutUsec=0)
Definition: SimpleSocket.cpp:657
bool m_bIsBlocking
socket flags
Definition: SimpleSocket.h:603
Definition: StatTimer.h:70
int32_t GetSendTimeoutSec(void)
Definition: SimpleSocket.h:408
Currently not connected.
Definition: SimpleSocket.h:146
virtual int32_t Receive(int32_t nMaxBytes=1, uint8_t *pBuffer=0)
Definition: SimpleSocket.cpp:732
struct timeval m_stSendTimeout
receive timeout
Definition: SimpleSocket.h:607
uint32_t GetTotalTimeMs()
Definition: SimpleSocket.h:434
bool IsNonblocking(void)
Definition: SimpleSocket.h:275
No socket error.
Definition: SimpleSocket.h:139
bool SetReceiveTimeout(int32_t nRecvTimeoutSec, int32_t nRecvTimeoutUsec=0)
Definition: SimpleSocket.cpp:629
uint32_t GetTotalTimeUsec()
Definition: SimpleSocket.h:440
CSocketError
Defines all error codes handled by the CSimpleSocket class.
Definition: SimpleSocket.h:136
struct timeval m_stRecvTimeout
connection timeout
Definition: SimpleSocket.h:606
Defines socket as UDP socket.
Definition: SimpleSocket.h:129
fd_set m_writeFds
internal statistics.
Definition: SimpleSocket.h:616
uint16_t GetServerPort()
Definition: SimpleSocket.h:492