45 #ifndef COMMONCPP_SOCKET_H_
46 #define COMMONCPP_SOCKET_H_
50 #ifndef COMMONCPP_CONFIG_H_
51 #include <commoncpp/config.h>
54 #ifndef COMMONCPP_STRING_H_
58 #ifndef COMMONCPP_ADDRESS_H_
62 #ifndef COMMONCPP_EXCEPTION_H_
67 #define MSG_DONTWAIT 0
71 #define MSG_NOSIGNAL 0
78 #define IPPROTO_DCCP 33
83 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
84 #define DCCP_SOCKOPT_CCID 13
85 #define DCCP_SOCKOPT_TX_CCID 14
86 #define DCCP_SOCKOPT_RX_CCID 15
90 typedef socket_t SOCKET;
103 typedef enum State State;
112 typedef enum Family Family;
136 errServiceUnavailable,
137 errMulticastDisabled,
146 typedef enum Error Error;
155 typedef enum Tos Tos;
162 typedef enum Pending Pending;
167 mutable const char *errstr;
170 void setSocket(
void);
173 static socket_t dupSocket(socket_t s,Socket::State state);
187 State
volatile state;
197 Error error(Error error,
const char *err = NULL,
long systemError = 0)
const;
205 inline void error(
const char *err)
const
206 {error(errExtended, err);}
214 inline void setError(
bool enable)
215 {flags.thrown = !enable;}
222 void endSocket(
void);
229 Error connectError(
void);
234 Error sendLimit(
int limit = 2048);
239 Error receiveLimit(
int limit = 1);
247 Error sendTimeout(timeout_t timer);
255 Error receiveTimeout(timeout_t timer);
264 Error sendBuffer(
unsigned size);
273 Error receiveBuffer(
unsigned size);
282 Error bufferSize(
unsigned size);
292 Error setBroadcast(
bool enable);
305 Error setMulticastByFamily(
bool enable, Family family = IPV4);
315 Error setLoopbackByFamily(
bool enable, Family family = IPV4);
324 Error setTimeToLiveByFamily(
unsigned char ttl, Family fam = IPV4);
335 inline Error join(
const IPV6Multicast &ia,
int iface = 0) {
return join(
ucommon::Socket::address(getaddress(ia)), iface); }
347 Error drop(
const IPV6Multicast &ia,
int iface = 0) {
return drop(
ucommon::Socket::address(getaddress(ia)), iface); }
357 Error setRouting(
bool enable);
365 Error setNoDelay(
bool enable);
378 Socket(
int domain,
int type,
int protocol = 0);
401 Socket(
const Socket &source);
412 ssize_t readLine(
char *buf,
size_t len, timeout_t timeout = 0);
425 virtual ssize_t readData(
void * buf,
size_t len,
char separator=0,timeout_t t=0);
435 virtual ssize_t writeData(
const void* buf,
size_t len,timeout_t t=0);
446 inline Error getErrorNumber(
void)
const {
return errid;}
454 inline const char *getErrorString(
void)
const {
return errstr;}
456 inline long getSystemError(
void)
const {
return syserr;}
458 const char *getSystemErrorString(
void)
const;
469 virtual bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
477 static bool check(Family fam);
483 bool operator!()
const;
485 operator bool()
const;
490 Socket &operator=(
const Socket &from);
503 virtual IPV4Host getIPV4Sender(tpport_t *port = NULL)
const;
505 inline IPV4Host getSender(tpport_t *port)
const
506 {
return getIPV4Sender(port);}
509 virtual IPV6Host getIPV6Sender(tpport_t *port = NULL)
const;
523 IPV4Host getIPV4Peer(tpport_t *port = NULL)
const;
525 inline IPV4Host getPeer(tpport_t *port)
const
526 {
return getIPV4Peer(port);}
529 IPV6Host getIPV6Peer(tpport_t *port = NULL)
const;
539 IPV4Host getIPV4Local(tpport_t *port = NULL)
const;
541 inline IPV4Host getLocal(tpport_t *port)
const
542 {
return getIPV4Local(port);}
545 IPV6Host getIPV6Local(tpport_t *port = NULL)
const;
560 void setCompletion(
bool immediate);
567 Error setLinger(
bool linger);
576 Error setKeepAlive(
bool enable);
586 Error setTypeOfService(Tos service);
596 bool isConnected(
void)
const;
605 bool isActive(
void)
const;
613 inline bool isBroadcast(
void)
const
614 {
return flags.broadcast;}
621 inline bool isRouted(
void)
const
622 {
return flags.route;}
625 inline struct in_addr getaddress(const IPV4Address &ia)
626 {
return ia.getAddress();}
629 inline struct in6_addr getaddress(const IPV6Address &ia)
630 {
return ia.getAddress();}
635 #if defined(CCXX_EXCEPTIONS)
637 class __EXPORT SockException :
public IOException
640 Socket::Error _socketError;
643 inline SockException(
const String &str, Socket::Error socketError,
long systemError = 0) :
644 IOException(str, systemError), _socketError(socketError) {}
646 inline Socket::Error getSocketError()
const
647 {
return _socketError;}
A generic socket base class.
Network addresses and sockets related classes.
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Common C++ generic string class.
GNU Common C++ exception model base classes.
A generic socket address class.