Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MNUDPSocket.h

Go to the documentation of this file.
00001 /* Copyright (C) 2000 KOM/Darmstadt University of Technology 00002 * 00003 * You are allowed to use all other parts of the code under the following terms: 00004 * 00005 * For non-commercial use, code may be used in unmodified form provided 00006 * that this copyright notice and this permission notice appear in 00007 * supporting documentation. 00008 * 00009 * This software is provided "as is" and without any express or implied 00010 * warranties, including, without limitation, the implied warranty of 00011 * fitness for a particular purpose. 00012 * 00013 * The code may be subjected to the GNU General Public License, Version 2, 00014 * and re-distributed under the terms of this license. 00015 * As a special exception, permission is granted to link this code 00016 * with the Qt library and distribute executables, as long as you 00017 * follow the requirements of the GNU GPL in regard to all of the 00018 * software in the executable aside from Qt. 00019 * 00020 * Commercial use other than under the terms of the GNU General Public 00021 * License is allowed only after express negotiation of conditions 00022 * with the authors. 00023 */ 00024 #ifndef MN_OS_NET_UDPSOCKET_H 00025 #define MN_OS_NET_UDPSOCKET_H 00026 00027 /************************************************************* 00028 * Classes defined in this header file: 00029 * MNUDPSocket 00030 * MNUDPClientSocket 00031 */ 00032 00033 #include "MNSocket.h" 00034 #include "MNRope.h" 00035 #include <sys/uio.h> 00036 #include <assert.h> 00037 00038 class MNSelector; 00039 class MNRope; 00040 00041 /* The Linux netinet header files are a shame, because they're completely 00042 * inconsitant compared to what the standards define. 00043 * Better check if this is defined in the 2.2.2 Kernel 00044 */ 00045 00046 #define IP_RECVDSTADDR 7 /* bool; receive IP dst addr w/datagram */ 00047 00048 /*********************************************************************** 00049 * MNUDPSocket 00050 ***********************************************************************/ 00051 00052 class MNUDPSocket : public MNSocket 00053 { 00054 public: 00055 /********************************************************************* 00056 * MNUDPSocket::CallbackMode 00057 **********************************************************************/ 00058 00059 enum CallbackMode 00060 { 00061 ClientMode, 00062 IovecMode, 00063 RopeMode 00064 }; 00065 00066 /********************************************************************* 00067 * MNUDPSocket::CallbackSetting 00068 **********************************************************************/ 00069 00070 /* 00071 * The callback settings are intended as a means for existing MN software 00072 * to co-existing with stream handler approaches that rely on the same 00073 * basic classes. Finally, it may be a good idea to remove one of the 00074 * approaches or to copy this class for size and efficiency reasons. For 00075 * the integration, it is good to support Iovecs as well as ropes. 00076 * 00077 * The 3 modes have default values for buffer spaces requests. For the 00078 * client mode, receive buffers are not initialized, since there is no 00079 * need for receive buffers in a client-only (=send-only) environment. 00080 * For the Iovec mode, 3 buffers sized 8,8,128 are default, for backward 00081 * compatibility. 00082 * For Rope mode, 8*512 buffers are default, which seems like a good 00083 * trade-off between allocation size and overhead in the RTP receive 00084 * settings. 00085 */ 00086 class CallbackSetting 00087 { 00088 protected: 00089 const CallbackMode _cm; 00090 size_t* _buf_sizes; 00091 int _buf_num; 00092 00093 friend class MNUDPSocket; 00094 00095 public: 00096 CallbackSetting( CallbackMode cm ); 00097 CallbackSetting( CallbackMode cm, size_t size, int count ); 00098 CallbackSetting( CallbackMode cm, const size_t* sizes, int count ); 00099 CallbackSetting( const CallbackSetting& cs ); 00100 ~CallbackSetting( ); 00101 }; 00102 00103 private: 00104 const CallbackSetting _cs; 00105 00106 protected: 00107 inline CallbackMode cm() const { return _cs._cm; } 00108 00109 private: 00110 /* 00111 * no copy constructor or assignment operator allowed 00112 */ 00113 MNUDPSocket( const MNUDPSocket& orig ); 00114 MNUDPSocket& operator=( const MNSocket& orig ); 00115 00116 public: 00117 MNUDPSocket ( MNSocket& orig, int copyflags ); 00118 MNUDPSocket ( CallbackSetting cm, MNSocket& orig, int copyflags ); 00119 MNUDPSocket ( CallbackSetting cm ); 00120 MNUDPSocket ( CallbackSetting cm, int sockfd ); 00121 virtual ~MNUDPSocket ( ); 00122 00123 virtual void callback ( MNSelector* select, int fd ); 00124 00130 inline virtual bool isUdp() const {return true;} 00131 00146 bool join_mc( int port, const char *hostname, char *ifnamei, int onoff ); 00147 00148 virtual struct iovec* set_buffers ( int& iovlen ); 00149 virtual void unset_buffers ( struct iovec* iov, int iovlen ); 00150 MNRope* buffers_to_rope( struct iovec* iov, 00151 int& iovlen, 00152 size_t bytesused ); 00153 00154 /* 00155 * recvfrom() 00156 * All calls are implemented by the system's recvfrom() function. 00157 * Originally, all sock_addr variables were only internal. For UDP servers, 00158 * it is appropriate to respond to calls without some connect-like operation. 00159 * The fd parameter should always be identical to get_sockdesc() and is 00160 * allowed only for backward compatibility. 00161 */ 00162 int recvfrom ( char* buffer, 00163 int buflen); 00164 int recvfrom ( char* buffer, 00165 int buflen, 00166 struct sockaddr_in& sock_addr, 00167 unsigned& sock_size ); 00168 00169 int sendto ( const void* buf, 00170 int buflen, 00171 const struct sockaddr_in* server_addr ); 00172 int sendto ( const char* buff, 00173 int len, 00174 int port, 00175 const char* hostname ); 00176 00196 virtual int reader ( struct iovec* iov, 00197 int len, 00198 struct sockaddr_in*& dest ) = 0; 00199 00200 virtual void writer ( ) = 0; 00201 00225 virtual int reader ( MNRope*& rope, 00226 struct sockaddr_in*& dest ) = 0; 00227 00228 void connect(int port, char* hostname); 00229 }; 00230 00231 /*********************************************************************** 00232 * MNUDPClientSocket 00233 * The MNUDPClientSocket exists only to keep a socket alive for later 00234 * re-use in a different UDP socket class. It has been introduced to 00235 * prevent inefficient, stupid, unreliable, opening and closing of 00236 * sockets that is done for nothing else but to check whether a port is 00237 * free. 00238 ***********************************************************************/ 00239 00240 class MNUDPClientSocket : public MNUDPSocket 00241 { 00242 public: 00243 MNUDPClientSocket( ) 00244 : MNUDPSocket ( MNUDPSocket::ClientMode ) 00245 { } 00246 00247 virtual int reader ( struct iovec*, int, struct sockaddr_in*& ) 00248 { 00249 MN_FATAL("reader() not implemented for MNUDPClientSocket"); 00250 return 0; 00251 } 00252 00253 virtual int reader ( MNRope*& , struct sockaddr_in*& ) 00254 { 00255 MN_FATAL("reader() not implemented for MNUDPClientSocket"); 00256 return 0; 00257 } 00258 00259 virtual void writer ( ) 00260 { 00261 MN_FATAL("writer() not implemented for MNUDPClientSocket"); 00262 } 00263 00264 }; 00265 00266 00267 #endif /* MN_OS_NET_UDPSOCKET_H */ 00268 00269

Generated on Sun Mar 6 13:35:49 2005 for Komssys by doxygen 1.3.8