44 #ifndef COMMONCPP_SOCKET_H_
45 #define COMMONCPP_SOCKET_H_
49 #ifndef COMMONCPP_CONFIG_H_
50 #include <commoncpp/config.h>
53 #ifndef COMMONCPP_STRING_H_
57 #ifndef COMMONCPP_ADDRESS_H_
61 #ifndef COMMONCPP_EXCEPTION_H_
66 #define MSG_DONTWAIT 0
70 #define MSG_NOSIGNAL 0
77 #define IPPROTO_DCCP 33
82 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
83 #define DCCP_SOCKOPT_CCID 13
84 #define DCCP_SOCKOPT_TX_CCID 14
85 #define DCCP_SOCKOPT_RX_CCID 15
89 typedef socket_t SOCKET;
102 typedef enum State State;
111 typedef enum Family Family;
135 errServiceUnavailable,
136 errMulticastDisabled,
145 typedef enum Error Error;
154 typedef enum Tos Tos;
161 typedef enum Pending Pending;
166 mutable const char *errstr;
169 void setSocket(
void);
172 static socket_t dupSocket(socket_t s,Socket::State state);
188 State
volatile state;
198 Error
error(Error error,
const char *err = NULL,
long systemError = 0)
const;
206 inline void error(
const char *err)
const
207 {
error(errExtended, err);};
215 inline void setError(
bool enable)
216 {flags.thrown = !enable;};
223 void endSocket(
void);
230 Error connectError(
void);
235 Error sendLimit(
int limit = 2048);
240 Error receiveLimit(
int limit = 1);
265 Error sendBuffer(
unsigned size);
274 Error receiveBuffer(
unsigned size);
283 Error bufferSize(
unsigned size);
293 Error setBroadcast(
bool enable);
306 Error setMulticastByFamily(
bool enable, Family family = IPV4);
316 Error setLoopbackByFamily(
bool enable, Family family = IPV4);
325 Error setTimeToLiveByFamily(
unsigned char ttl, Family fam = IPV4);
333 Error
join(
const IPV4Multicast &ia);
335 Error
join(
const IPV6Multicast &ia);
344 Error
drop(
const IPV4Multicast &ia);
346 Error
drop(
const IPV6Multicast &ia);
356 Error setRouting(
bool enable);
364 Error setNoDelay(
bool enable);
377 Socket(
int domain,
int type,
int protocol = 0);
400 Socket(
const Socket &source);
411 ssize_t readLine(
char *buf,
size_t len,
timeout_t timeout = 0);
424 virtual ssize_t readData(
void * buf,
size_t len,
char separator=0,
timeout_t t=0);
434 virtual ssize_t writeData(
const void* buf,
size_t len,
timeout_t t=0);
445 inline Error getErrorNumber(
void)
const {
return errid;}
453 inline const char *getErrorString(
void)
const {
return errstr;}
455 inline long getSystemError(
void)
const {
return syserr;}
457 const char *getSystemErrorString(
void)
const;
468 virtual bool isPending(Pending pend,
timeout_t timeout = TIMEOUT_INF);
476 static bool check(Family fam);
484 operator bool()
const;
500 virtual IPV4Host getIPV4Sender(tpport_t *port = NULL)
const;
502 inline IPV4Host getSender(tpport_t *port = NULL)
const
503 {
return getIPV4Sender(port);}
506 virtual IPV6Host getIPV6Sender(tpport_t *port = NULL)
const;
518 IPV4Host getIPV4Peer(tpport_t *port = NULL)
const;
520 inline IPV4Host getPeer(tpport_t *port = NULL)
const
521 {
return getIPV4Peer(port);}
524 IPV6Host getIPV6Peer(tpport_t *port = NULL)
const;
534 IPV4Host getIPV4Local(tpport_t *port = NULL)
const;
536 inline IPV4Host getLocal(tpport_t *port = NULL)
const
537 {
return getIPV4Local(port);}
540 IPV6Host getIPV6Local(tpport_t *port = NULL)
const;
553 void setCompletion(
bool immediate);
560 Error setLinger(
bool linger);
569 Error setKeepAlive(
bool enable);
579 Error setTypeOfService(Tos service);
589 bool isConnected(
void)
const;
598 bool isActive(
void)
const;
606 inline bool isBroadcast(
void)
const
607 {
return flags.broadcast;};
614 inline bool isRouted(
void)
const
615 {
return flags.route;};
618 inline struct in_addr getaddress(const IPV4Address &ia)
619 {
return ia.getAddress();}
622 inline struct in6_addr getaddress(const IPV6Address &ia)
623 {
return ia.getAddress();}
628 #if defined(CCXX_EXCEPTIONS)
630 class __EXPORT SockException :
public IOException
633 Socket::Error _socketError;
636 inline SockException(
const String &str, Socket::Error socketError,
long systemError = 0) :
637 IOException(str, systemError), _socketError(socketError) {};
639 inline Socket::Error getSocketError()
const
640 {
return _socketError; }
Common C++ generic string class.
int drop(const struct addrinfo *list)
Drop socket from multicast group.
bool operator!() const
Test if socket is invalid.
Network addresses and sockets related classes.
A generic socket base class.
unsigned long timeout_t
Typedef for millisecond timer values.
GNU Common C++ exception model base classes.
Socket & operator=(socket_t socket)
Assign socket from a socket descriptor.
int join(const struct addrinfo *list)
Join socket to multicast group.
static int error(void)
Return error code of last socket operation,.
virtual ~Socket()
Shutdown, close, and destroy socket.
Socket()
Create a socket object for use.
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.