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
00038
00039
00040 #ifndef __CONNECTION_HPP__
00041 #define __CONNECTION_HPP__
00042
00043 #include "transmissible.hpp"
00044 #include "protocol.hpp"
00045
00046 #include <iostream>
00047 #include <string>
00048
00049 namespace ipsi
00050 {
00051
00063 class Connection
00064 {
00065 private:
00066
00068 std::string mIP;
00070 unsigned int mPort;
00072 Protocol* mProtocol;
00073
00074 public:
00075
00086 Connection ( const std::string& ip ,
00087 const unsigned int port ,
00088 Protocol* protocol );
00089
00090
00097 virtual ~Connection ( );
00098
00099
00105 void drop (const size_t sz) const;
00106
00110 void getInfo ( ) const;
00111
00116 unsigned int getPort() const
00117 {
00118 return mPort;
00119 }
00120
00125 std::string getIP() const
00126 {
00127 return mIP;
00128 }
00129
00130
00131
00132
00137 template<class T>
00138 void send ( const T& data ) const
00139 {
00140 sender<T, Connection>::Run( *this, data);
00141 }
00142
00147 template<class T, int N>
00148 void send ( const T (&data) [N] ) const
00149 {
00150 send(&data[0], N);
00151 }
00152
00158 template<class T>
00159 void send ( const T* data, const size_t sz ) const
00160 {
00161 typedef char* ptr;
00162 mProtocol->send(ptr(data), sz*sizeof(T));
00163 }
00164
00165
00166
00167
00172 template<class T>
00173 void receive ( T& data )
00174 {
00175 receiver<T, Connection>::Run( *this, data);
00176 }
00177
00182 template<class T, int N>
00183 void receive ( T (&data) [N] )
00184 {
00185 receive(&data[0], N);
00186 }
00187
00193 template<class T>
00194 void receive ( T* data, size_t sz )
00195 {
00196 typedef unsigned char* ptr;
00197 size_t rsz;
00198 mProtocol->receive(sz*sizeof(T), ptr(data), &rsz);
00199 }
00200
00201 };
00202
00203
00204
00205
00212 template<class T>
00213 Connection& operator<<( Connection& c, const T& d)
00214 {
00215 c.send(d);
00216 return c;
00217 }
00224 template<class T>
00225 Connection& operator>>( Connection& c, T& d)
00226 {
00227 c.receive(d);
00228 return c;
00229 }
00230
00231
00232
00233
00237 template<class T>
00238 class drop
00239 {
00240 private:
00242 size_t nbDrop;
00243
00244 public :
00249 drop(size_t sz) : nbDrop(sz*sizeof(T))
00250 {}
00251
00256 size_t operator()() const
00257 {
00258 return nbDrop;
00259 }
00260 };
00261
00262
00263
00267 template<class T>
00268 class array
00269 {
00270 private:
00272 size_t sz;
00274 T* data;
00275
00276 public :
00282 array(T* ptr, size_t v) : sz(v), data(ptr)
00283 {}
00284
00289 size_t size() const
00290 {
00291 return sz;
00292 }
00293
00298 T* const_ptr() const
00299 {
00300 return data;
00301 }
00302
00307 T* ptr()
00308 {
00309 return data;
00310 }
00311 };
00312
00313
00314
00321 template<class T>
00322 Connection& operator>>( Connection& c, const drop<T>& d )
00323 {
00324 c.drop(d());
00325 return c;
00326 }
00327
00328
00335 template<class T>
00336 Connection& operator>>( Connection& c, const array<T>& d )
00337 {
00338 c.receive(d.const_ptr(),d.size());
00339 return c;
00340 }
00341
00348 template<class T>
00349 Connection& operator<<( Connection& c, const array<T>& d )
00350 {
00351 c.send(d.const_ptr(),d.size());
00352 return c;
00353 }
00354
00355
00362 template<class C,class T,class A>
00363 Connection& operator>>( Connection& c, std::basic_string<C,T,A>& d )
00364 {
00365 size_t sz;
00366 c.receive(sz);
00367 std::string r(sz,'\0');
00368 c.receive(&r[0],sz);
00369 d = r;
00370 return c;
00371 }
00372
00379 template<class C,class T,class A>
00380 Connection& operator<<( Connection& c, const std::basic_string<C,T,A>& d )
00381 {
00382 std::cout << d.size() << ":" << d << std::endl;
00383 c.send(d.size());
00384 c.send(d.c_str(),d.size());
00385 return c;
00386 }
00387 }
00388 #endif