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
198 void setSocket(
void);
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);
369 Error join(
const IPV6Multicast &ia);
380 Error drop(
const IPV6Multicast &ia);
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);
506 {
return getIPV4Sender(port);}
509 virtual IPV6Host getIPV6Sender(
tpport_t *port = NULL)
const;
524 {
return getIPV4Peer(port);}
527 IPV6Host getIPV6Peer(
tpport_t *port = NULL)
const;
540 {
return getIPV4Local(port);}
543 IPV6Host getIPV6Local(
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;
745 virtual bool onAccept(
const IPV6Host &ia,
tpport_t port);
751 virtual IPV6Host getIPV6Sender(
tpport_t *port = NULL)
const;
799 void disconnect(
void);
804 bool setCCID(uint8 ccid);
836 void connect(
const char *name);
893 struct sockaddr_in6 ipv6;
894 struct sockaddr_in ipv4;
898 struct sockaddr_in ipv4;
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);
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);
1165 inline ssize_t
send(
const void *buf,
size_t len)
1198 inline ssize_t
transmit(
const char *buffer,
size_t len)
1289 inline Error join(
const IPV6Multicast &ia)
1297 inline Error drop(
const IPV6Multicast &ia)
1310 {
return _IORET64 ::recv(so, (
char *)buf,
_IOLEN64 len, 0);};
1367 Error disconnect(
void);
1399 void setSegmentSize(
unsigned mss);
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);
1537 inline SOCKET getSocket(
void)
1540 inline int getSegmentSize(
void)
1555 TCPV6Socket(
const IPV6Address &bind,
tpport_t port,
unsigned backlog = 5,
unsigned mss = 536);
1567 TCPV6Socket(
const char *name,
unsigned backlog = 5,
unsigned mss = 536);
1577 inline IPV6Host getRequest(
tpport_t *port = NULL)
const
1578 {
return Socket::getIPV6Sender(port);}
1588 inline IPV6Host getLocal(
tpport_t *port = NULL)
const
1589 {
return Socket::getIPV6Local(port);}
1602 virtual ~TCPV6Socket();
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);
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);};
1760 void connect(TCPV6Socket &server);
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);
1923 TCPSession(TCPV6Socket &server,
int pri = 0,
size_t stack = 0);
1982 void endStream(
void);
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
2095 IOException(str, systemError), _socketError(socketError) {};
2098 {
return _socketError; }
2102 #ifdef CCXX_NAMESPACES
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...
virtual bool isPending(Pending pend, timeout_t timeout=TIMEOUT_INF)
Get the status of pending operations.
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Simple TCP Stream, to be used with Common C++ Library.
long getSystemError(void) const
The Socket is used as the base for all Internet protocol services under Common C++.
Error setMulticast(bool enable)
void setError(bool enable)
This service is used to turn the error handler on or off for "throwing" exceptions by manipulating th...
ssize_t transmit(const char *buffer, size_t len)
Transmit "send" to use "connected" send rather than sendto.
Error setKeepAlive(bool enable)
UDP sockets implement the TCP SOCK_DGRAM UDP protocol.
int getSegmentSize(void)
Get the buffer size for servers.
IPV4Host getLocal(tpport_t *port=NULL) const
Error join(const IPV4Multicast &ia)
Join a multicast group.
Representing half of a two-way UDP connection, the UDP transmitter can broadcast data to another sele...
bool isRouted(void) const
Return if socket routing is enabled.
__EXPORT AppLog & error(AppLog &sl)
Manipulator for error level.
Error setTimeToLiveByFamily(unsigned char ttl, Family fam=IPV4)
Set the multicast time to live for a multicast socket.
DCCP sockets are used for stream based connected sessions between two sockets.
Error setRouting(bool enable)
SOCKET getTransmitter(void)
unsigned short tpport_t
Transport Protocol Ports.
class __EXPORT SimpleTCPStream
SOCKET volatile so
the actual socket descriptor, in Windows, unlike posix it cannot be used as an file descriptor that w...
IPV4Host getPeer(tpport_t *port=NULL) const
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Error setTypeOfService(Tos tos)
void endReceiver(void)
End receiver.
IPV4Host getNAT(tpport_t *port) const
This is a generic and portable string class.
Error getErrorNumber(void) const
Often used by a "catch" to fetch the last error of a thrown socket.
bool isBroadcast(void) const
Return if broadcast has been enabled for the specified socket.
IPV4Host getLocal(tpport_t *port=NULL) const
Used to get local bound address.
The TCP session is used to primarily to represent a client connection that can be managed on a sepera...
Error setMulticast(bool enable)
Set the multicast.
ssize_t send(const void *buf, size_t len)
Transmit "send" to use "connected" send rather than sendto.
IPV4Host getPeer(tpport_t *port=NULL) const
Error drop(const IPV4Multicast &ia)
Error setTimeToLive(char ttl)
Set time to live.
The broadcast address object is used to store the broadcast address for a specific subnet...
Error join(const IPV4Multicast &ia)
Error drop(const IPV4Multicast &ia)
Drop membership from a multicast group.
SOCKET getReceiver(void) const
Error setLoopbackByFamily(bool enable, Family family=IPV4)
Set the multicast loopback flag for the socket.
Network addresses and sockets related classes.
bool isOutputReady(unsigned long timeout=0l)
See if output queue is empty for sending more packets.
ssize_t receive(void *buf, size_t len)
Receive a data packet from the connected peer host.
void endSocket(void)
Used as the default destructor for ending a socket.
TCP sockets are used for stream based connected sessions between two sockets.
IPV4Host getIPV4Local(tpport_t *port=NULL) const
Get the local address and port number this socket is currently bound to.
Error setLoopback(bool enable)
Set the loopback.
Error setBroadcast(bool enable)
Representing a UDP socket used for subnet broadcasts, this class provides an alternate binding and se...
void endTransmitter(void)
Stop transmitter.
virtual IPV4Host getIPV4Sender(tpport_t *port=NULL) const
May be used to examine the origin of data waiting in the socket receive queue.
IPV4Host getSender(tpport_t *port=NULL) const
SOCKET getSocket(void)
Fetch out the socket.
void error(const char *err) const
This service is used to throw application defined socket errors where the application specific error ...
bool isInputReady(timeout_t timeout=TIMEOUT_INF)
See if input queue has data packets available.
Error setMulticast(bool enable)
std::iostream * tcp(void)
Used in derived classes to refer to the current object via it's iostream.
size_t getBufferSize(void) const
Return the size of the current stream buffering used.
Error setMulticastByFamily(bool enable, Family family=IPV4)
Setting multicast binds the multicast interface used for the socket to the interface the socket itsel...
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Every thread of execution in an application is created by instantiating an object of a class derived ...
void setTimeout(timeout_t timer)
Set the I/O operation timeout for socket I/O operations.
A specialization of IPV4Address that provides address validation for multicast addresses.
bool isPendingConnection(timeout_t timeout=TIMEOUT_INF)
Used to wait for pending connection requests.
UDP duplex connections impliment a bi-directional point-to-point UDP session between two peer hosts...
Error setRouting(bool enable)
Error setTimeToLive(unsigned char ttl)
This object is used to hold the actual and valid internet address of a specific host machine that wil...
bool isPendingReceive(timeout_t timeout)
Check for pending data.
IPV4Host getRequest(tpport_t *port=NULL) const
Return address and port of next connection request.
The network name and address objects are all derived from a common IPV4Address base class...
TCP streams are used to represent TCP client connections to a server by TCP protocol servers for acce...
Error setRouting(bool enable)
Set the socket routing to indicate if outgoing messages should bypass normal routing (set false)...
Error setKeepAlive(bool enable)
Set the keep-alive status of this socket and if keep-alive messages will be sent. ...
Error setBroadcast(bool enable)
Set the subnet broadcast flag for the socket.
unsigned short tpport_t
Transport Protocol Ports.
Error setBroadcast(bool enable)
Error setTypeOfService(Tos service)
Set packet scheduling on platforms which support ip quality of service conventions.
bool isPendingConnection(timeout_t timeout=TIMEOUT_INF)
Used to wait for pending connection requests.
Representing half of a two-way UDP connection, the UDP receiver can receive data from another peer ho...