43 #ifndef COMMONCPP_SOCKET_H_
44 #define COMMONCPP_SOCKET_H_
48 #ifndef COMMONCPP_CONFIG_H_
49 #include <commoncpp/config.h>
52 #ifndef COMMONCPP_STRING_H_
56 #ifndef COMMONCPP_ADDRESS_H_
60 #ifndef COMMONCPP_EXCEPTION_H_
65 #define MSG_DONTWAIT 0
69 #define MSG_NOSIGNAL 0
76 #define IPPROTO_DCCP 33
81 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
82 #define DCCP_SOCKOPT_CCID 13
83 #define DCCP_SOCKOPT_TX_CCID 14
84 #define DCCP_SOCKOPT_RX_CCID 15
88 typedef socket_t SOCKET;
101 typedef enum State State;
110 typedef enum Family Family;
134 errServiceUnavailable,
135 errMulticastDisabled,
144 typedef enum Error Error;
153 typedef enum Tos Tos;
160 typedef enum Pending Pending;
165 mutable const char *errstr;
168 void setSocket(
void);
171 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);
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);
332 Error
join(
const IPV4Multicast &ia);
334 Error
join(
const IPV6Multicast &ia);
343 Error
drop(
const IPV4Multicast &ia);
345 Error
drop(
const IPV6Multicast &ia);
355 Error setRouting(
bool enable);
363 Error setNoDelay(
bool enable);
376 Socket(
int domain,
int type,
int protocol = 0);
399 Socket(
const Socket &source);
410 ssize_t readLine(
char *buf,
size_t len,
timeout_t timeout = 0);
423 virtual ssize_t readData(
void * buf,
size_t len,
char separator=0,
timeout_t t=0);
433 virtual ssize_t writeData(
const void* buf,
size_t len,
timeout_t t=0);
444 inline Error getErrorNumber(
void)
const {
return errid;}
452 inline const char *getErrorString(
void)
const {
return errstr;}
454 inline long getSystemError(
void)
const {
return syserr;}
456 const char *getSystemErrorString(
void)
const;
467 virtual bool isPending(Pending pend,
timeout_t timeout = TIMEOUT_INF);
475 static bool check(Family fam);
483 operator bool()
const;
499 virtual IPV4Host getIPV4Sender(tpport_t *port = NULL)
const;
501 inline IPV4Host getSender(tpport_t *port = NULL)
const
502 {
return getIPV4Sender(port);}
505 virtual IPV6Host getIPV6Sender(tpport_t *port = NULL)
const;
517 IPV4Host getIPV4Peer(tpport_t *port = NULL)
const;
519 inline IPV4Host getPeer(tpport_t *port = NULL)
const
520 {
return getIPV4Peer(port);}
523 IPV6Host getIPV6Peer(tpport_t *port = NULL)
const;
533 IPV4Host getIPV4Local(tpport_t *port = NULL)
const;
535 inline IPV4Host getLocal(tpport_t *port = NULL)
const
536 {
return getIPV4Local(port);}
539 IPV6Host getIPV6Local(tpport_t *port = NULL)
const;
552 void setCompletion(
bool immediate);
559 Error setLinger(
bool linger);
568 Error setKeepAlive(
bool enable);
578 Error setTypeOfService(Tos service);
588 bool isConnected(
void)
const;
597 bool isActive(
void)
const;
605 inline bool isBroadcast(
void)
const
606 {
return flags.broadcast;};
613 inline bool isRouted(
void)
const
614 {
return flags.route;};
617 inline struct in_addr getaddress(const IPV4Address &ia)
618 {
return ia.getAddress();}
621 inline struct in6_addr getaddress(const IPV6Address &ia)
622 {
return ia.getAddress();}
627 #if defined(CCXX_EXCEPTIONS)
629 class __EXPORT SockException :
public IOException
632 Socket::Error _socketError;
635 inline SockException(
const String &str, Socket::Error socketError,
long systemError = 0) :
636 IOException(str, systemError), _socketError(socketError) {};
638 inline Socket::Error getSocketError()
const
639 {
return _socketError; }
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
bool operator!() const
Test if socket is invalid.
int join(const struct addrinfo *list)
Join socket to multicast group.
A generic socket base class.
Socket & operator=(socket_t socket)
Assign socket from a socket descriptor.
int drop(const struct addrinfo *list)
Drop socket from multicast group.
static int error(void)
Return error code of last socket operation,.
Network addresses and sockets related classes.
Common C++ generic string class.
virtual ~Socket()
Shutdown, close, and destroy socket.
Socket()
Create a socket object for use.
unsigned long timeout_t
Typedef for millisecond timer values.
GNU Common C++ exception model base classes.