45 #ifndef CCXX_SOCKET_H_
46 #define CCXX_SOCKET_H_
48 #ifndef CCXX_ADDRESS_H_
52 #if defined(WIN32) && !defined(__CYGWIN32__)
54 #define _IOLEN64 (unsigned)
55 #define _IORET64 (int)
56 #define TIMEOUT_INF ~((timeout_t) 0)
57 typedef int socklen_t;
59 #define INVALID_SOCKET -1
72 #define MSG_DONTWAIT 0
76 #define MSG_NOSIGNAL 0
83 #define IPPROTO_DCCP 33
88 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
89 #define DCCP_SOCKOPT_CCID 13
90 #define DCCP_SOCKOPT_TX_CCID 14
91 #define DCCP_SOCKOPT_RX_CCID 15
93 #ifdef CCXX_NAMESPACES
195 mutable const char *errstr;
198 void setSocket(
void);
232 Error
error(Error
error,
const char *err = NULL,
long systemError = 0)
const;
240 inline void error(
const char *err)
const
241 {
error(errExtended, err);};
250 {flags.thrown = !enable;};
257 void endSocket(
void);
264 Error connectError(
void);
269 Error sendLimit(
int limit = 2048);
274 Error receiveLimit(
int limit = 1);
299 Error sendBuffer(
unsigned size);
308 Error receiveBuffer(
unsigned size);
317 Error bufferSize(
unsigned size);
327 Error setBroadcast(
bool enable);
340 Error setMulticastByFamily(
bool enable, Family family = IPV4);
350 Error setLoopbackByFamily(
bool enable, Family family = IPV4);
359 Error setTimeToLiveByFamily(
unsigned char ttl, Family fam = IPV4);
390 Error setRouting(
bool enable);
399 Error setNoDelay(
bool enable);
412 Socket(
int domain,
int type,
int protocol = 0);
446 ssize_t readLine(
char *buf,
size_t len,
timeout_t timeout = 0);
459 virtual ssize_t readData(
void * buf,
size_t len,
char separator=0,
timeout_t t=0);
469 virtual ssize_t writeData(
const void* buf,
size_t len,
timeout_t t=0);
487 static bool check(Family fam);
503 virtual IPV4Host getIPV4Sender(tpport_t *port = NULL)
const;
506 {
return getIPV4Sender(port);}
509 virtual IPV6Host getIPV6Sender(tpport_t *port = NULL)
const;
521 IPV4Host getIPV4Peer(tpport_t *port = NULL)
const;
524 {
return getIPV4Peer(port);}
527 IPV6Host getIPV6Peer(tpport_t *port = NULL)
const;
537 IPV4Host getIPV4Local(tpport_t *port = NULL)
const;
540 {
return getIPV4Local(port);}
543 IPV6Host getIPV6Local(tpport_t *port = NULL)
const;
573 IPV4Host getIPV4NAT(tpport_t *port = NULL)
const;
576 {
return getIPV4NAT(port);}
579 IPV6Host getIPV6NAT(tpport_t *port = NULL)
const;
592 void setCompletion(
bool immediate);
599 Error setLinger(
bool linger);
608 Error setKeepAlive(
bool enable);
618 Error setTypeOfService(Tos service);
628 bool isConnected(
void)
const;
637 bool isActive(
void)
const;
643 bool operator!()
const;
652 {
return flags.broadcast;};
660 {
return flags.route;};
680 const char *getSystemErrorString(
void)
const;
723 struct sockaddr_in ipv4;
725 struct sockaddr_in6 ipv6;
743 virtual bool onAccept(
const IPV4Host &ia, tpport_t port);
745 virtual bool onAccept(
const IPV6Host &ia, tpport_t port);
748 virtual IPV4Host getIPV4Sender(tpport_t *port = NULL)
const;
751 virtual IPV6Host getIPV6Sender(tpport_t *port = NULL)
const;
779 DCCPSocket(
const char *name, Family family = IPV4,
unsigned backlog = 5);
799 void disconnect(
void);
804 bool setCCID(
uint8 ccid);
836 void connect(
const char *name);
887 inline Error setKeepAlive(
bool enable)
893 struct sockaddr_in6 ipv6;
894 struct sockaddr_in ipv4;
898 struct sockaddr_in ipv4;
913 UDPSocket(
const char *name, Family family = IPV4);
959 void setPeer(
const IPV4Host &host, tpport_t port);
960 void connect(
const IPV4Host &host, tpport_t port);
962 void setPeer(
const IPV6Host &host, tpport_t port);
963 void connect(
const IPV6Host &host, tpport_t port);
973 Socket::Error getInterfaceIndex(
const char *ethX,
int& InterfaceIndex);
993 ssize_t send(
const void *buf,
size_t len);
1003 ssize_t receive(
void *buf,
size_t len,
bool reply =
false);
1013 IPV4Host getIPV4Peer(tpport_t *port = NULL)
const;
1015 {
return getIPV4Peer(port);}
1018 IPV6Host getIPV6Peer(tpport_t *port = NULL)
const;
1028 inline ssize_t
peek(
void *buf,
size_t len)
1029 {
return _IORET64 ::recv(so, (
char *)buf,
_IOLEN64 len, MSG_PEEK);};
1034 void setPeer(
const char *service);
1035 void connect(
const char *service);
1041 Error disconnect(
void);
1056 void setPeer(
const IPV4Host &ia, tpport_t port);
1058 Error setBroadcast(
bool enable)
1165 inline ssize_t
send(
const void *buf,
size_t len)
1198 inline ssize_t
transmit(
const char *buffer,
size_t len)
1310 {
return _IORET64 ::recv(so, (
char *)buf,
_IOLEN64 len, 0);};
1367 Error disconnect(
void);
1399 void setSegmentSize(
unsigned mss);
1413 virtual bool onAccept(
const IPV4Host &ia, tpport_t port);
1451 TCPSocket(
const char *name,
unsigned backlog = 5,
unsigned mss = 536);
1518 void setSegmentSize(
unsigned mss);
1532 virtual bool onAccept(
const IPV6Host &ia, tpport_t port);
1567 TCPV6Socket(
const char *name,
unsigned backlog = 5,
unsigned mss = 536);
1615 #pragma warning(disable:4275) // disable C4275 warning
1636 void segmentBuffering(
unsigned mss);
1657 void disconnect(
void);
1662 int getSegmentSize(
void);
1671 void allocate(
size_t size);
1677 void endStream(
void);
1704 int overflow(
int ch);
1714 void connect(
const IPV4Host &host, tpport_t port,
unsigned mss = 536);
1716 void connect(
const IPV6Host &host, tpport_t port,
unsigned mss = 536);
1726 void connect(
const char *name,
unsigned mss = 536);
1736 {
return ((std::iostream *)
this);};
1787 TCPStream(
const char *name, Family family = IPV4,
unsigned mss = 536,
bool throwflag =
false,
timeout_t timer = 0);
1819 #ifdef HAVE_SNPRINTF
1826 size_t printf(
const char *format, ...);
1845 inline ssize_t
peek(
void *buf,
size_t len)
1846 {
return _IORET64 ::recv(so, (
char *)buf,
_IOLEN64 len, MSG_PEEK);};
1906 tpport_t port,
size_t size = 536,
int pri = 0,
size_t stack = 0);
1909 tpport_t port,
size_t size = 536,
int pri = 0,
size_t stack = 0);
1969 IPV4Host getSender(tpport_t *port)
const;
1982 void endStream(
void);
1992 void Connect(
const IPV4Host &host, tpport_t port,
size_t size);
2055 ssize_t read(
char *bytes,
size_t length,
timeout_t timeout = 0);
2068 ssize_t write(
const char *bytes,
size_t length,
timeout_t timeout = 0);
2083 ssize_t peek(
char *bytes,
size_t length,
timeout_t timeout = 0);
2087 #ifdef COMMON_STD_EXCEPTION
2088 class __EXPORT SockException :
public IOException
2094 SockException(
const String &str,
Socket::Error socketError,
long systemError = 0) :
2095 IOException(str, systemError), _socketError(socketError) {};
2098 {
return _socketError; }
2102 #ifdef CCXX_NAMESPACES
SOCKET getSocket(void)
Fetch out the socket.
Definition: socket.h:1537
Representing a UDP socket used for subnet broadcasts, this class provides an alternate binding and se...
Definition: socket.h:1053
virtual IPV4Host getIPV4Sender(tpport_t *port=NULL) const
May be used to examine the origin of data waiting in the socket receive queue.
bool isPendingConnection(timeout_t timeout=TIMEOUT_INF)
Used to wait for pending connection requests.
Definition: socket.h:1596
std::iostream * tcp(void)
Used in derived classes to refer to the current object via it's iostream.
Definition: socket.h:1735
#define MSG_NOSIGNAL
Definition: socket.h:76
IPV6Host getRequest(tpport_t *port=NULL) const
Return address and port of next connection request.
Definition: socket.h:1577
bool isPendingConnection(timeout_t timeout=TIMEOUT_INF)
Used to wait for pending connection requests.
Definition: socket.h:843
Error getErrorNumber(void) const
Often used by a "catch" to fetch the last error of a thrown socket.
Definition: socket.h:668
State
Definition: socket.h:182
void setTimeout(timeout_t timer)
Set the I/O operation timeout for socket I/O operations.
Definition: socket.h:1794
timeout_t timeout
Definition: socket.h:1642
IPV4Host getRequest(tpport_t *port=NULL) const
Return address and port of next connection request.
Definition: socket.h:1461
bool isPendingReceive(timeout_t timeout)
Check for pending data.
Definition: socket.h:1267
int getSegmentSize(void)
Definition: socket.h:1540
Error setTypeOfService(Tos tos)
Definition: socket.h:1214
SOCKET getTransmitter(void)
Definition: socket.h:1179
ssize_t transmit(const char *buffer, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: socket.h:1198
virtual bool isPending(Pending pend, timeout_t timeout=TIMEOUT_INF)
Get the status of pending operations.
#define __EXPORT
Definition: config.h:980
char * pbuf
Definition: socket.h:1645
Error setRouting(bool enable)
Set the socket routing to indicate if outgoing messages should bypass normal routing (set false)...
SOCKET getReceiver(void) const
Definition: socket.h:1276
Error setMulticastByFamily(bool enable, Family family=IPV4)
Setting multicast binds the multicast interface used for the socket to the interface the socket itsel...
#define TIMEOUT_INF
Definition: thread.h:115
UDP duplex connections impliment a bi-directional point-to-point UDP session between two peer hosts...
Definition: socket.h:1332
Error join(const IPV4Multicast &ia)
Join a multicast group.
Error setBroadcast(bool enable)
Definition: socket.h:1217
Family
Definition: socket.h:122
bool isOutputReady(unsigned long timeout=0l)
See if output queue is empty for sending more packets.
Definition: socket.h:1207
Error setRouting(bool enable)
Definition: socket.h:1211
u_int8_t uint8
Definition: config.h:143
static Mutex mutex
Definition: socket.h:202
size_t getBufferSize(void) const
Return the size of the current stream buffering used.
Definition: socket.h:1853
Family family
Definition: socket.h:902
Representing half of a two-way UDP connection, the UDP transmitter can broadcast data to another sele...
Definition: socket.h:1087
unsigned long timeout_t
Definition: thread.h:74
Family family
Definition: socket.h:1644
Error setLoopbackByFamily(bool enable, Family family=IPV4)
Set the multicast loopback flag for the socket.
The TCP session is used to primarily to represent a client connection that can be managed on a separa...
Definition: socket.h:1867
void setError(bool enable)
This service is used to turn the error handler on or off for "throwing" exceptions by manipulating th...
Definition: socket.h:249
Representing half of a two-way UDP connection, the UDP receiver can receive data from another peer ho...
Definition: socket.h:1229
Error setRouting(bool enable)
Definition: socket.h:1279
IPV4Host getSender(tpport_t *port=NULL) const
Definition: socket.h:505
IPV6Host getLocal(tpport_t *port=NULL) const
Used to get local bound address.
Definition: socket.h:1588
bool isBroadcast(void) const
Return if broadcast has been enabled for the specified socket.
Definition: socket.h:651
Error setMulticast(bool enable)
Definition: socket.h:1282
IPV4Host getNAT(tpport_t *port) const
Definition: socket.h:575
SOCKET volatile so
the actual socket descriptor, in Windows, unlike posix it cannot be used as an file descriptor that w...
Definition: socket.h:221
virtual IPV6Host getIPV6Sender(tpport_t *port=NULL) const
The network name and address objects are all derived from a common IPV6Address base class...
Definition: address.h:753
The network name and address objects are all derived from a common IPV4Address base class...
Definition: address.h:350
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: socket.h:1845
Error setTimeToLiveByFamily(unsigned char ttl, Family fam=IPV4)
Set the multicast time to live for a multicast socket.
SOCKET getSocket(void)
Fetch out the socket.
Definition: socket.h:1418
State volatile state
Definition: socket.h:222
Error setKeepAlive(bool enable)
Set the keep-alive status of this socket and if keep-alive messages will be sent. ...
IPV6Host getIPV6Local(tpport_t *port=NULL) const
Error setLoopback(bool enable)
Set the loopback.
Definition: socket.h:937
IPV4Host getPeer(tpport_t *port=NULL) const
Definition: socket.h:1014
#define MSG_DONTWAIT
Definition: socket.h:72
Network addresses and sockets related classes.
Error drop(const IPV6Multicast &ia)
Definition: socket.h:1297
ssize_t receive(void *buf, size_t len)
Receive a data packet from the connected peer host.
Definition: socket.h:1309
int getSegmentSize(void)
Get the buffer size for servers.
Definition: socket.h:1424
void endTransmitter(void)
Stop transmitter.
Definition: socket.h:1171
Error setMulticast(bool enable)
Set the multicast.
Definition: socket.h:943
Tos
Definition: socket.h:165
Error drop(const IPV4Multicast &ia)
Drop membership from a multicast group.
bool isPendingConnection(timeout_t timeout=TIMEOUT_INF)
Used to wait for pending connection requests.
Definition: socket.h:1480
TCPV6 sockets are used for stream based connected sessions between two ipv6 sockets.
Definition: socket.h:1514
Error setTimeToLive(unsigned char ttl)
Definition: socket.h:1185
The Socket is used as the base for all Internet protocol services under Common C++.
Definition: socket.h:119
size_t bufsize
Definition: socket.h:1643
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: socket.h:1028
Error setTypeOfService(Tos service)
Set packet scheduling on platforms which support ip quality of service conventions.
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition: thread.h:186
TCP streams are used to represent TCP client connections to a server by TCP protocol servers for acce...
Definition: socket.h:1631
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition: address.h:1010
IPV4Host getLocal(tpport_t *port=NULL) const
Definition: socket.h:539
Every thread of execution in an application is created by instantiating an object of a class derived ...
Definition: thread.h:1093
Error setTimeToLive(char ttl)
Set time to live.
Definition: socket.h:949
Pending
Definition: socket.h:174
ssize_t send(const void *buf, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Definition: socket.h:1165
Error join(const IPV4Multicast &ia)
Definition: socket.h:1285
void endSocket(void)
Used as the default destructor for ending a socket.
IPV4Host getIPV4Local(tpport_t *port=NULL) const
Get the local address and port number this socket is currently bound to.
The broadcast address object is used to store the broadcast address for a specific subnet...
Definition: address.h:612
bool isInputReady(timeout_t timeout=TIMEOUT_INF)
See if input queue has data packets available.
Definition: socket.h:1318
int SOCKET
Definition: socket.h:60
class __EXPORT SimpleTCPStream
Definition: socket.h:1952
Simple TCP Stream, to be used with Common C++ Library.
Definition: socket.h:1965
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:561
Error
Definition: socket.h:131
Error drop(const IPV4Multicast &ia)
Definition: socket.h:1293
void flush()
Definition: socket.h:2042
UDP sockets implement the TCP SOCK_DGRAM UDP protocol.
Definition: socket.h:884
Error setBroadcast(bool enable)
Set the subnet broadcast flag for the socket.
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:634
IPV4Host getLocal(tpport_t *port=NULL) const
Used to get local bound address.
Definition: socket.h:1472
Error join(const IPV6Multicast &ia)
Definition: socket.h:1289
int segsize
Definition: socket.h:1398
Error setMulticast(bool enable)
Definition: socket.h:1182
unsigned short tpport_t
Transport Protocol Ports.
Definition: address.h:86
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:948
bool isRouted(void) const
Return if socket routing is enabled.
Definition: socket.h:659
IPV4Host getPeer(tpport_t *port=NULL) const
Definition: socket.h:523
DCCP sockets are used for stream based connected sessions between two sockets.
Definition: socket.h:720
const char * getErrorString(void) const
Often used by a "catch" to fetch the user set error string of a thrown socket, but only if EXTENDED e...
Definition: socket.h:676
void error(const char *err) const
This service is used to throw application defined socket errors where the application specific error ...
Definition: socket.h:240
TCP sockets are used for stream based connected sessions between two sockets.
Definition: socket.h:1395
#define _IOLEN64
Definition: socket.h:64
long getSystemError(void) const
Definition: socket.h:678
__EXPORT AppLog & error(AppLog &sl)
Manipulator for error level.
Definition: applog.h:541
void endReceiver(void)
End receiver.
Definition: socket.h:1273