00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#ifndef MN_OS_NET_UDPSOCKET_H
00025
#define MN_OS_NET_UDPSOCKET_H
00026
00027
00028
00029
00030
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
00042
00043
00044
00045
00046
#define IP_RECVDSTADDR 7
00047
00048
00049
00050
00051
00052 class MNUDPSocket :
public MNSocket
00053 {
00054
public:
00055
00056
00057
00058
00059 enum CallbackMode
00060 {
00061
ClientMode,
00062
IovecMode,
00063
RopeMode
00064 };
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
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
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
00156
00157
00158
00159
00160
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
00233
00234
00235
00236
00237
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
00268
00269