socketUDP.h
Go to the documentation of this file.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
00033
00035
00036
00037 #ifndef __SOCKET_UDP_
00038 #define __SOCKET_UDP_
00039
00040 #ifdef MIP_HOST_APPLE
00041 #include <applePatch.h>
00042 #endif
00043
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047 #include <errno.h>
00048 #include <string.h>
00049 #include <sys/types.h>
00050 #include <sys/socket.h>
00051 #include <netinet/in.h>
00052 #include <arpa/inet.h>
00053 #include <netdb.h>
00054
00055 typedef unsigned char BYTE;
00056
00057 #include <net/if.h>
00058 #include <sys/ioctl.h>
00059
00060 #ifndef SIOCGIFCONF
00061 #include <linux/sockios.h>
00062 #endif
00063
00064 #include <iostream>
00065 #include <sstream>
00066
00067 #include <Types.h>
00068 #include <LinuxShMem.h>
00069
00070
00071 using namespace std;
00072
00073
00074
00075 #define UDP_MSG_SIZE 40000
00076
00077
00078
00082 class SocketUDP : public MIPObject{
00083 protected:
00084
00085 stringstream _ssdebug;
00086
00087 public:
00088 int m_sockfd;
00089 struct sockaddr_in my_addr;
00090 struct sockaddr_in their_addr;
00091 struct hostent *he;
00092
00094 SocketUDP();
00095
00097 ~SocketUDP();
00098
00100 void closeConnection();
00101
00103 virtual string getObjectName() const {
00104 return string("SocketUDP");
00105 }
00106 };
00107
00109 class ParamsToWrapper{
00110 public:
00111 int local_port;
00112 int broadcast_port;
00113 char subnet_address[64];
00114 };
00115
00119 class Wrapper: public SocketUDP{
00120 private:
00121 pthread_t wrapper_thread_id;
00122
00123 int lastListNum;
00124
00125 LinuxShMem shMem;
00126
00127 public:
00128 int broadcast_port;
00129 int local_port;
00130 const char *subnet;
00131 int numero_ip;
00132 bool active;
00133
00135 Wrapper(int bp, int lp, const char*net);
00136
00138 ~Wrapper(void);
00139
00142 int findIP(const char *net);
00143
00145 int Init(void);
00146
00148 void run(void);
00149
00151 void start(void);
00152
00154 void stop(void);
00155
00157 string getObjectName() const {
00158 return string("Wrapper");
00159 }
00160 };
00161
00165 class ListenerUDP: public SocketUDP{
00166 private:
00167 ParamsToWrapper params;
00168 Wrapper *wrapper;
00169
00170 LinuxShMem shMem;
00171 public:
00173 ListenerUDP(const int port);
00174
00176 ListenerUDP(const int port, string& my_host);
00177
00179 ListenerUDP(const int bp, const int lp, string& net_addr);
00180
00182 ~ListenerUDP();
00183
00185 int receive(string &buffe, string &senderAddr);
00186
00188 void receive(char* buf, string &senderAddr, int numbytes);
00189
00191 int receive(string &buffe);
00192
00194 int lSelect();
00195
00197 string getObjectName() const {
00198 return string("ListenerUDP");
00199 }
00200 };
00201
00205 class SenderUDP: public SocketUDP{
00206
00207 public:
00208
00210 SenderUDP(const int port, string& host);
00211
00213 ~SenderUDP();
00214
00217 int send(string &msg);
00218
00222 int send(char * msg, int lsize);
00223
00225 virtual string getObjectName() const {return string("SenderUDP");}
00226 };
00227
00231 class BroadcasterUDP: public SenderUDP{
00232 public:
00233 int m_broadcast;
00234
00236 BroadcasterUDP(const int port,string& host);
00237
00239 ~BroadcasterUDP();
00240
00242 virtual string getObjectName() const {return string("BroadcasterUDP");}
00243 };
00244
00245 #endif
00246
00247
00248
00249