RTP Trace System
1.0
|
00001 // ########################################################################## 00002 // #### #### 00003 // #### RTP Audio Server Project #### 00004 // #### ============================ #### 00005 // #### #### 00006 // #### Socket #### 00007 // #### #### 00008 // #### Version 1.00 -- February 16, 2001 #### 00009 // #### #### 00010 // #### Copyright (C) 1999 Thomas Dreibholz #### 00011 // #### 2000 Universität Bonn, Abt. IV #### 00012 // #### 2001 EMail: dreibh@iem.uni-due.de #### 00013 // #### WWW: https://www.uni-due.de/~be0001 #### 00014 // #### #### 00015 // ########################################################################## 00016 00017 00018 #ifndef SOCKETCLASS_H 00019 #define SOCKETCLASS_H 00020 00021 00022 #include "system.h" 00023 #include "internetaddress.h" 00024 #include "internetflow.h" 00025 00026 00027 namespace Coral { 00028 00029 00033 const cardinal UDPHeaderSize = 8; 00034 00038 const cardinal IPv4HeaderSize = 20; 00039 00043 const cardinal IPv6HeaderSize = 40; 00044 00045 00054 class Socket 00055 { 00056 // ====== Definitions ==================================================== 00057 public: 00058 enum SocketCommunicationDomain { 00059 UndefinedSocketCommunicationDomain = -1, 00060 IP = 255, 00061 IPv4 = AF_INET, // Do not use IPv4/IPv6! 00062 IPv6 = AF_INET6, // Use IP instead! 00063 Unix = AF_UNIX 00064 }; 00065 enum SocketType { 00066 UndefinedSocketType = -1, 00067 UDP = SOCK_DGRAM, 00068 Datagram = SOCK_DGRAM, 00069 TCP = SOCK_STREAM, 00070 Stream = SOCK_STREAM, 00071 Raw = SOCK_RAW, 00072 RDM = SOCK_RDM, 00073 SeqPacket = SOCK_SEQPACKET 00074 }; 00075 enum SocketProtocol { 00076 UndefinedSocketProtocol = -1, 00077 Default = 0, 00078 ICMPv4 = IPPROTO_ICMP, 00079 ICMPv6 = IPPROTO_ICMPV6 00080 }; 00081 00082 // ====== Constructor/Destructor ========================================= 00086 Socket(); 00087 00100 Socket(const SocketCommunicationDomain communicationDomain, 00101 const SocketType socketType, 00102 const SocketProtocol socketProtocol = Default); 00103 00107 virtual ~Socket(); 00108 00109 00110 // ====== Create/close socket ============================================ 00121 bool create(const SocketCommunicationDomain communicationDomain = IP, 00122 const SocketType socketType = TCP, 00123 const SocketProtocol socketProtocol = Default); 00124 00128 void close(); 00129 00138 void shutdown(const cardinal shutdownLevel); 00139 00140 00141 // ====== Statistics functions =========================================== 00147 inline card64 getBytesSent() const; 00148 00154 inline card64 getBytesReceived() const; 00155 00159 inline void resetBytesSent(); 00160 00164 inline void resetBytesReceived(); 00165 00166 00167 // ====== Socket control functions ======================================= 00173 inline bool ready() const; 00174 00182 bool bind(const SocketAddress& address = InternetAddress()); 00183 00191 bool listen(const cardinal backlog = 5); 00192 00198 Socket* accept(); 00199 00208 bool connect(const SocketAddress& address, const card8 trafficClass = 0); 00209 00210 00211 // ====== Error code ===================================================== 00217 inline integer getLastError(); 00218 00219 00220 // ====== Socket options ================================================= 00230 inline integer getSocketOption(const cardinal level, 00231 const cardinal optionNumber, 00232 void* optionValue, 00233 socklen_t* optionLength) const; 00234 00240 cardinal getSoLinger() const; 00241 00247 bool getSoReuseAddress() const; 00248 00254 bool getSoBroadcast() const; 00255 00261 bool getTCPNoDelay() const; 00262 00268 bool getBlockingMode(); 00269 00270 00280 inline integer setSocketOption(const cardinal level, 00281 const cardinal optionNumber, 00282 const void* optionValue, 00283 const socklen_t optionLength); 00284 00291 void setSoLinger(const bool on, const cardinal linger); 00292 00298 void setSoReuseAddress(const bool on); 00299 00305 void setSoBroadcast(const bool on); 00306 00312 void setTCPNoDelay(const bool on); 00313 00319 void setBlockingMode(const bool on); 00320 00321 00322 // ====== Get flow label/traffic class =================================== 00330 inline card32 getSendFlowLabel() const; 00331 00339 inline card8 getSendTrafficClass() const; 00340 00346 inline card32 getReceivedFlowLabel() const; 00347 00353 inline card8 getReceivedTrafficClass() const; 00354 00355 00356 // ====== I/O functions ================================================== 00367 virtual ssize_t sendTo(const void* buffer, 00368 const size_t length, 00369 const cardinal flags, 00370 const SocketAddress& receiver, 00371 const card8 trafficClass = 0); 00372 00385 virtual ssize_t send(const void* buffer, 00386 const size_t length, 00387 const cardinal flags = 0, 00388 const card8 trafficClass = 0); 00389 00399 virtual ssize_t receiveFrom(void* buffer, 00400 const size_t length, 00401 SocketAddress& sender, 00402 const cardinal flags = 0); 00403 00412 virtual ssize_t receive(void* buffer, 00413 const size_t length, 00414 const cardinal flags = 0); 00415 00423 virtual ssize_t read(void* buffer, 00424 const size_t length); 00425 00433 virtual ssize_t write(const void* buffer, 00434 const size_t length); 00435 00443 inline integer fcntl(const integer cmd, const long arg = 0); 00444 00445 00453 inline integer ioctl(const integer request, const void* argp); 00454 00455 00456 // ====== Get address ==================================================== 00468 bool getSocketAddress(SocketAddress& address) const; 00469 00481 bool getPeerAddress(SocketAddress& address) const; 00482 00483 00484 // ====== IPv6 flow functions ============================================ 00495 InternetFlow allocFlow(const InternetAddress& address, 00496 const card32 flowLabel = 0, 00497 const card8 shareLevel = IPV6_FL_S_PROCESS); 00498 00504 void freeFlow(InternetFlow& flow); 00505 00517 bool renewFlow(InternetFlow& flow, 00518 const cardinal expires, 00519 const cardinal linger = 6); 00520 00529 bool renewFlow(const cardinal expires, 00530 const cardinal linger = 6); 00531 00532 00533 // ====== Bind pair of internet sockets ================================== 00542 static bool bindInternetSocketPair(Socket& senderSocket, 00543 Socket& receiverSocket, 00544 const InternetAddress& receiver = InternetAddress()); 00545 00546 00547 // ====== Traffic shaper methods ========================================= 00557 virtual void setTrafficConstraint(const card8 trafficClass, 00558 const card64 bandwidth, 00559 const double bufferDelay); 00560 00565 virtual void flush(); 00566 00567 00568 // ====== Get system's socket descriptor ================================= 00576 inline int getSystemSocketDescriptor() const; 00577 00578 00579 // ====== Constants ====================================================== 00585 static const cardinal MinAutoSelectPort = 16384; 00586 00592 static const cardinal MaxAutoSelectPort = 65535; 00593 00594 00595 // ====== Private data =================================================== 00596 protected: 00597 friend class TrafficShaper; 00598 00599 00600 void init(); 00601 bool setTOS(const card8 trafficClass); 00602 ssize_t recvFrom(int fd, 00603 void* buf, 00604 const size_t len, 00605 const integer flags, 00606 struct sockaddr* addr, 00607 size_t* addrlen); 00608 00609 00610 card64 BytesSent; 00611 card64 BytesReceived; 00612 card32 SendFlow; 00613 card32 ReceivedFlow; 00614 cardinal Backlog; 00615 cardinal LastError; 00616 int SocketDescriptor; 00617 sockaddr* Destination; 00618 SocketCommunicationDomain CommunicationDomain; 00619 SocketType Type; 00620 SocketProtocol Protocol; 00621 }; 00622 00623 00624 } 00625 00626 00627 #include "socket.icc" 00628 00629 00630 #endif