00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00043 #ifndef COMMONCPP_UDP_H_
00044 #define COMMONCPP_UDP_H_
00045
00046 #include <cstdio>
00047
00048 #ifndef COMMONCPP_CONFIG_H_
00049 #include <commoncpp/config.h>
00050 #endif
00051
00052 #ifndef COMMONCPP_STRING_H_
00053 #include <commoncpp/string.h>
00054 #endif
00055
00056 #ifndef COMMONCPP_ADDRESS_H_
00057 #include <commoncpp/address.h>
00058 #endif
00059
00060 #ifndef COMMONCPP_SOCKET_H_
00061 #include <commoncpp/socket.h>
00062 #endif
00063
00064 NAMESPACE_COMMONCPP
00065
00098 class __EXPORT UDPSocket : public Socket
00099 {
00100 private:
00101 inline Error setKeepAlive(bool enable)
00102 {return Socket::setKeepAlive(enable);};
00103
00104 protected:
00105 #ifdef CCXX_IPV6
00106 union {
00107 struct sockaddr_in6 ipv6;
00108 struct sockaddr_in ipv4;
00109 } peer;
00110 #else
00111 union {
00112 struct sockaddr_in ipv4;
00113 } peer;
00114 #endif
00115
00116 Family family;
00117
00118 public:
00122 UDPSocket(Family family = IPV4);
00123
00127 UDPSocket(const char *name, Family family = IPV4);
00128
00138 UDPSocket(const IPV4Address &bind, tpport_t port);
00139 #ifdef CCXX_IPV6
00140 UDPSocket(const IPV6Address &bind, tpport_t port);
00141 #endif
00142
00146 virtual ~UDPSocket();
00147
00151 inline Error setLoopback(bool enable)
00152 {return Socket::setLoopbackByFamily(enable, family);}
00153
00157 inline Error setMulticast(bool enable)
00158 {return Socket::setMulticastByFamily(enable, family);}
00159
00163 inline Error setTimeToLive(char ttl)
00164 {return Socket::setTimeToLiveByFamily(ttl, family);}
00165
00173 void setPeer(const IPV4Host &host, tpport_t port);
00174 void connect(const IPV4Host &host, tpport_t port);
00175 #ifdef CCXX_IPV6
00176 void setPeer(const IPV6Host &host, tpport_t port);
00177 void connect(const IPV6Host &host, tpport_t port);
00178 #endif
00179
00187 Socket::Error getInterfaceIndex(const char *ethX,int& InterfaceIndex);
00188
00197 Socket::Error join(const IPV4Multicast &ia,int InterfaceIndex);
00198
00206 ssize_t send(const void *buf, size_t len);
00207
00216 ssize_t receive(void *buf, size_t len, bool reply = false);
00217
00226 IPV4Host getIPV4Peer(tpport_t *port = NULL) const;
00227 inline IPV4Host getPeer(tpport_t *port = NULL) const
00228 {return getIPV4Peer(port);}
00229
00230 #ifdef CCXX_IPV6
00231 IPV6Host getIPV6Peer(tpport_t *port = NULL) const;
00232 #endif
00233
00241 inline ssize_t peek(void *buf, size_t len)
00242 {return ::recv(so, (char *)buf, len, MSG_PEEK);};
00243
00247 void setPeer(const char *service);
00248 void connect(const char *service);
00249
00254 Error disconnect(void);
00255 };
00256
00265 class __EXPORT UDPBroadcast : public UDPSocket
00266 {
00267 private:
00268 void setPeer(const IPV4Host &ia, tpport_t port);
00269
00270 Error setBroadcast(bool enable)
00271 {return Socket::setBroadcast(enable);};
00272
00273 public:
00280 UDPBroadcast(const IPV4Address &ia, tpport_t port);
00281
00288 void setPeer(const IPV4Broadcast &subnet, tpport_t port);
00289 };
00290
00299 class __EXPORT UDPTransmit : protected UDPSocket
00300 {
00301 private:
00309 Error cConnect(const IPV4Address &ia, tpport_t port);
00310
00311 protected:
00315 UDPTransmit(Family family = IPV4);
00316
00328 UDPTransmit(const IPV4Address &bind, tpport_t port = 5005);
00329 #ifdef CCXX_IPV6
00330 UDPTransmit(const IPV6Address &bind, tpport_t port = 5005);
00331 #endif
00332
00342 Error connect(const IPV4Host &host, tpport_t port);
00343 #ifdef CCXX_IPV6
00344 Error connect(const IPV6Address &host, tpport_t port);
00345 #endif
00346
00356 Error connect(const IPV4Broadcast &subnet, tpport_t port);
00357
00365 Error connect(const IPV4Multicast &mgroup, tpport_t port);
00366 #ifdef CCXX_IPV6
00367 Error connect(const IPV6Multicast &mgroup, tpport_t port);
00368 #endif
00369
00377 inline ssize_t send(const void *buf, size_t len)
00378 {return ::send(so, (const char *)buf, len, MSG_NOSIGNAL);}
00379
00383 inline void endTransmitter(void)
00384 {Socket::endSocket();}
00385
00386
00387
00388
00389
00390
00391 inline SOCKET getTransmitter(void)
00392 {return so;};
00393
00394 inline Error setMulticast(bool enable)
00395 {return Socket::setMulticastByFamily(enable, family);}
00396
00397 inline Error setTimeToLive(unsigned char ttl)
00398 {return Socket::setTimeToLiveByFamily(ttl, family);};
00399
00400 public:
00410 inline ssize_t transmit(const char *buffer, size_t len)
00411 {return ::send(so, buffer, len, MSG_DONTWAIT|MSG_NOSIGNAL);}
00412
00419 inline bool isOutputReady(unsigned long timeout = 0l)
00420 {return Socket::isPending(Socket::pendingOutput, timeout);};
00421
00422
00423 inline Error setRouting(bool enable)
00424 {return Socket::setRouting(enable);};
00425
00426 inline Error setTypeOfService(Tos tos)
00427 {return Socket::setTypeOfService(tos);};
00428
00429 inline Error setBroadcast(bool enable)
00430 {return Socket::setBroadcast(enable);};
00431 };
00432
00441 class __EXPORT UDPReceive : protected UDPSocket
00442 {
00443 protected:
00454 UDPReceive(const IPV4Address &bind, tpport_t port);
00455 #ifdef CCXX_IPV6
00456 UDPReceive(const IPV6Address &bind, tpport_t port);
00457 #endif
00458
00468 Error connect(const IPV4Host &host, tpport_t port);
00469 #ifdef CCXX_IPV6
00470 Error connect(const IPV6Host &host, tpport_t port);
00471 #endif
00472
00479 bool isPendingReceive(timeout_t timeout)
00480 {return Socket::isPending(Socket::pendingInput, timeout);};
00481
00485 inline void endReceiver(void)
00486 {Socket::endSocket();}
00487
00488 inline SOCKET getReceiver(void) const
00489 {return so;};
00490
00491 inline Error setRouting(bool enable)
00492 {return Socket::setRouting(enable);}
00493
00494 inline Error setMulticast(bool enable)
00495 {return Socket::setMulticastByFamily(enable, family);}
00496
00497 inline Error join(const IPV4Multicast &ia)
00498 {return Socket::join(ia);}
00499
00500 #ifdef CCXX_IPV6
00501 inline Error join(const IPV6Multicast &ia)
00502 {return Socket::join(ia);}
00503 #endif
00504
00505 inline Error drop(const IPV4Multicast &ia)
00506 {return Socket::drop(ia);}
00507
00508 #ifdef CCXX_IPV6
00509 inline Error drop(const IPV6Multicast &ia)
00510 {return Socket::drop(ia);}
00511 #endif
00512
00513 public:
00521 inline ssize_t receive(void *buf, size_t len)
00522 {return ::recv(so, (char *)buf, len, 0);};
00523
00530 inline bool isInputReady(timeout_t timeout = TIMEOUT_INF)
00531 {return Socket::isPending(Socket::pendingInput, timeout);};
00532 };
00533
00544 class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive
00545 {
00546 public:
00554 UDPDuplex(const IPV4Address &bind, tpport_t port);
00555 #ifdef CCXX_IPV6
00556 UDPDuplex(const IPV6Address &bind, tpport_t port);
00557 #endif
00558
00568 Error connect(const IPV4Host &host, tpport_t port);
00569 #ifdef CCXX_IPV6
00570 Error connect(const IPV6Host &host, tpport_t port);
00571 #endif
00572
00579 Error disconnect(void);
00580 };
00581
00582 END_NAMESPACE
00583
00584 #endif