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);
186 State
volatile state;
196 Error error(Error error,
const char *err = NULL,
long systemError = 0)
const;
204 inline void error(
const char *err)
const
205 {error(errExtended, err);}
213 inline void setError(
bool enable)
214 {flags.thrown = !enable;}
221 void endSocket(
void);
228 Error connectError(
void);
233 Error sendLimit(
int limit = 2048);
238 Error receiveLimit(
int limit = 1);
263 Error sendBuffer(
unsigned size);
272 Error receiveBuffer(
unsigned size);
281 Error bufferSize(
unsigned size);
291 Error setBroadcast(
bool enable);
304 Error setMulticastByFamily(
bool enable, Family family = IPV4);
314 Error setLoopbackByFamily(
bool enable, Family family = IPV4);
323 Error setTimeToLiveByFamily(
unsigned char ttl, Family fam = IPV4);
334 inline Error join(
const IPV6Multicast &ia,
int iface = 0) {
return join(
ucommon::Socket::address(getaddress(ia)), iface); }
346 Error drop(
const IPV6Multicast &ia,
int iface = 0) {
return drop(
ucommon::Socket::address(getaddress(ia)), iface); }
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);
482 bool operator!()
const;
484 operator bool()
const;
489 Socket &operator=(
const Socket &from);
502 virtual IPV4Host getIPV4Sender(tpport_t *port = NULL)
const;
504 inline IPV4Host getSender(tpport_t *port)
const
505 {
return getIPV4Sender(port);}
508 virtual IPV6Host getIPV6Sender(tpport_t *port = NULL)
const;
522 IPV4Host getIPV4Peer(tpport_t *port = NULL)
const;
524 inline IPV4Host getPeer(tpport_t *port)
const
525 {
return getIPV4Peer(port);}
528 IPV6Host getIPV6Peer(tpport_t *port = NULL)
const;
538 IPV4Host getIPV4Local(tpport_t *port = NULL)
const;
540 inline IPV4Host getLocal(tpport_t *port)
const
541 {
return getIPV4Local(port);}
544 IPV6Host getIPV6Local(tpport_t *port = NULL)
const;
559 void setCompletion(
bool immediate);
566 Error setLinger(
bool linger);
575 Error setKeepAlive(
bool enable);
585 Error setTypeOfService(Tos service);
595 bool isConnected(
void)
const;
604 bool isActive(
void)
const;
612 inline bool isBroadcast(
void)
const
613 {
return flags.broadcast;}
620 inline bool isRouted(
void)
const
621 {
return flags.route;}
624 inline struct in_addr getaddress(const IPV4Address &ia)
625 {
return ia.getAddress();}
628 inline struct in6_addr getaddress(const IPV6Address &ia)
629 {
return ia.getAddress();}
634 #if defined(CCXX_EXCEPTIONS)
636 class __EXPORT SockException :
public IOException
639 Socket::Error _socketError;
642 inline SockException(
const String &str, Socket::Error socketError,
long systemError = 0) :
643 IOException(str, systemError), _socketError(socketError) {}
645 inline Socket::Error getSocketError()
const
646 {
return _socketError;}
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
A generic socket base class.
Network addresses and sockets related classes.
Common C++ generic string class.
A generic socket address class.
unsigned long timeout_t
Typedef for millisecond timer values.
GNU Common C++ exception model base classes.