43 #ifndef CCXX_SOCKET_H_
44 #define CCXX_SOCKET_H_
46 #ifndef CCXX_ADDRESS_H_
50 #if defined(WIN32) && !defined(__CYGWIN32__)
52 #define _IOLEN64 (unsigned)
53 #define _IORET64 (int)
54 #define TIMEOUT_INF ~((timeout_t) 0)
55 typedef int socklen_t;
57 #define INVALID_SOCKET -1
70 #define MSG_DONTWAIT 0
74 #define MSG_NOSIGNAL 0
77 #ifdef CCXX_NAMESPACES
179 mutable const char *errstr;
182 void setSocket(
void);
216 Error error(
Error error,
char *err = NULL,
long systemError = 0)
const;
224 inline void error(
char *err)
const
225 {
error(errExtended, err);};
233 inline void setError(
bool enable)
234 {flags.thrown = !enable;};
241 void endSocket(
void);
248 Error connectError(
void);
253 Error sendLimit(
int limit = 2048);
258 Error receiveLimit(
int limit = 1);
283 Error sendBuffer(
unsigned size);
292 Error receiveBuffer(
unsigned size);
301 Error bufferSize(
unsigned size);
311 Error setBroadcast(
bool enable);
324 Error setMulticastByFamily(
bool enable, Family family = IPV4);
334 Error setLoopbackByFamily(
bool enable, Family family = IPV4);
343 Error setTimeToLiveByFamily(
unsigned char ttl, Family fam = IPV4);
374 Error setRouting(
bool enable);
383 Error setNoDelay(
bool enable);
396 Socket(
int domain,
int type,
int protocol = 0);
430 ssize_t readLine(
char *buf,
size_t len,
timeout_t timeout = 0);
443 virtual ssize_t readData(
void * buf,
size_t len,
char separator=0,
timeout_t t=0);
453 virtual ssize_t writeData(
const void* buf,
size_t len,
timeout_t t=0);
471 static bool check(Family fam);
490 {
return getIPV4Sender(port);}
508 {
return getIPV4Peer(port);}
524 {
return getIPV4Local(port);}
560 {
return getIPV4NAT(port);}
576 void setCompletion(
bool immediate);
583 Error setLinger(
bool linger);
592 Error setKeepAlive(
bool enable);
602 Error setTypeOfService(Tos service);
612 bool isConnected(
void)
const;
621 bool isActive(
void)
const;
627 bool operator!()
const;
635 inline bool isBroadcast(
void)
const
636 {
return flags.broadcast;};
643 inline bool isRouted(
void)
const
644 {
return flags.route;};
664 const char *getSystemErrorString(
void)
const;
713 inline Error setKeepAlive(
bool enable)
719 struct sockaddr_in6 ipv6;
720 struct sockaddr_in ipv4;
724 struct sockaddr_in ipv4;
739 UDPSocket(
const char *name, Family family = IPV4);
763 inline Error setLoopback(
bool enable)
769 inline Error setMulticast(
bool enable)
775 inline Error setTimeToLive(
char ttl)
799 Socket::Error getInterfaceIndex(
const char *ethX,
int& InterfaceIndex);
819 ssize_t send(
const void *buf,
size_t len);
829 ssize_t receive(
void *buf,
size_t len,
bool reply =
false);
841 {
return getIPV4Peer(port);}
854 inline ssize_t peek(
void *buf,
size_t len)
855 {
return _IORET64 ::recv(so, (
char *)buf,
_IOLEN64 len, MSG_PEEK);};
860 void setPeer(
const char *service);
861 void connect(
const char *service);
867 Error disconnect(
void);
884 Error setBroadcast(
bool enable)
991 inline ssize_t send(
const void *buf,
size_t len)
997 inline void endTransmitter(
void)
1011 inline Error setTimeToLive(
unsigned char ttl)
1024 inline ssize_t transmit(
const char *buffer,
size_t len)
1033 inline bool isOutputReady(
unsigned long timeout = 0l)
1099 inline void endReceiver(
void)
1135 inline ssize_t receive(
void *buf,
size_t len)
1136 {
return _IORET64 ::recv(so, (
char *)buf,
_IOLEN64 len, 0);};
1193 Error disconnect(
void);
1225 void setSegmentSize(
unsigned mss);
1250 inline int getSegmentSize(
void)
1277 TCPSocket(
const char *name,
unsigned backlog = 5,
unsigned mss = 536);
1344 void setSegmentSize(
unsigned mss);
1366 inline int getSegmentSize(
void)
1393 TCPV6Socket(
const char *name,
unsigned backlog = 5,
unsigned mss = 536);
1441 #pragma warning(disable:4275) // disable C4275 warning
1462 void segmentBuffering(
unsigned mss);
1483 void disconnect(
void);
1488 int getSegmentSize(
void);
1497 void allocate(
size_t size);
1503 void endStream(
void);
1530 int overflow(
int ch);
1552 void connect(
const char *name,
unsigned mss = 536);
1561 std::iostream *tcp(
void)
1562 {
return ((std::iostream *)
this);};
1613 TCPStream(
const char *name, Family family = IPV4,
unsigned mss = 536,
bool throwflag =
false,
timeout_t timer = 0);
1645 #ifdef HAVE_SNPRINTF
1652 size_t printf(
const char *format, ...);
1671 inline ssize_t peek(
void *buf,
size_t len)
1672 {
return _IORET64 ::recv(so, (
char *)buf,
_IOLEN64 len, MSG_PEEK);};
1679 inline size_t getBufferSize(
void)
const
1732 tpport_t port,
size_t size = 536,
int pri = 0,
size_t stack = 0);
1735 tpport_t port,
size_t size = 536,
int pri = 0,
size_t stack = 0);
1808 void endStream(
void);
1881 ssize_t read(
char *bytes,
size_t length,
timeout_t timeout = 0);
1894 ssize_t write(
const char *bytes,
size_t length,
timeout_t timeout = 0);
1909 ssize_t peek(
char *bytes,
size_t length,
timeout_t timeout = 0);
1913 #ifdef COMMON_STD_EXCEPTION
1914 class __EXPORT SockException :
public IOException
1920 SockException(
const String &str,
Socket::Error socketError,
long systemError = 0) :
1921 IOException(str, systemError), _socketError(socketError) {};
1924 {
return _socketError; }
1928 #ifdef CCXX_NAMESPACES