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 #ifndef _terimber_aiogate_h_
00029 #define _terimber_aiogate_h_
00030
00031 #include "aiosock/aiosock.h"
00032 #include "base/map.h"
00033 #include "base/list.h"
00034 #include "aiogate/aiogatefactory.h"
00035 #include "threadpool/thread.h"
00036
00037 BEGIN_TERIMBER_NAMESPACE
00038 #pragma pack(4)
00039
00042 enum aiogate_chunk { BUFFER_CHUNK = 1024*64 };
00043
00046 class fixed_size_buffer
00047 {
00048 public:
00050 fixed_size_buffer() :
00051 _begin(0),
00052 _end(0),
00053 _next(0)
00054 {
00055 }
00056
00057 ub1_t _ptr[BUFFER_CHUNK];
00058 size_t _begin;
00059 size_t _end;
00060
00061 fixed_size_buffer* _next;
00062 };
00063
00066 class aiogate : public terimber_aiogate,
00067 public terimber_aiosock_callback,
00068 public terimber_thread_employer
00069 {
00072 class listener_info
00073 {
00074 public:
00076 listener_info( terimber_aiogate_pin_factory* factory,
00077 void* arg
00078 ) :
00079 _factory(factory), _arg(arg)
00080 {
00081 }
00082
00084 listener_info(const listener_info& x)
00085 : _factory(x._factory), _arg(x._arg)
00086 {
00087 }
00088
00089 terimber_aiogate_pin_factory* _factory;
00090 void* _arg;
00091 };
00092
00095 class pin_info_minimum
00096 {
00097 public:
00099 pin_info_minimum() :
00100 _pin(0),
00101 _rbuf(0),
00102 _shead(0),
00103 _factory(0)
00104 {}
00105
00107 pin_info_minimum(const pin_info_minimum& x) :
00108 _pin(x._pin),
00109 _rbuf(x._rbuf),
00110 _shead(x._shead),
00111 _factory(x._factory)
00112 {
00113 }
00114
00115 terimber_aiogate_pin* _pin;
00116 fixed_size_buffer* _rbuf;
00117 fixed_size_buffer* _shead;
00118 terimber_aiogate_pin_factory* _factory;
00119 };
00120
00123 class pin_info : public pin_info_minimum
00124 {
00125 public:
00127 pin_info() :
00128 pin_info_minimum(),
00129 _stail(0),
00130 _send_timeout(INFINITE),
00131 _recv_timeout(INFINITE),
00132 _in_progress_mask(0x0),
00133 _callback_invoking_mask(0x0),
00134 _still_alive(true),
00135 _tcp_udp(true)
00136 {}
00137
00139 pin_info(const pin_info& x) :
00140 pin_info_minimum(x),
00141 _stail(x._stail),
00142 _send_timeout(x._send_timeout),
00143 _recv_timeout(x._recv_timeout),
00144 _in_progress_mask(x._in_progress_mask),
00145 _callback_invoking_mask(x._callback_invoking_mask),
00146 _still_alive(x._still_alive),
00147 _tcp_udp(x._tcp_udp)
00148 {
00149 }
00150
00151 ub8_t _leader;
00152 fixed_size_buffer* _stail;
00153 size_t _send_timeout;
00154 size_t _recv_timeout;
00155 ub4_t _in_progress_mask;
00156 ub4_t _callback_invoking_mask;
00157 bool _still_alive;
00158 bool _tcp_udp;
00159 };
00160
00163 class pin_info_extra : public pin_info_minimum
00164 {
00165 public:
00167 pin_info_extra( const pin_info_minimum& x,
00168 size_t ident,
00169 size_t mask,
00170 bool invoke_callback
00171 ) :
00172 pin_info_minimum(x),
00173 _ident(ident),
00174 _mask(mask),
00175 _invoke_callback(invoke_callback)
00176 {
00177 }
00178
00180 pin_info_extra(const pin_info_extra& x) :
00181 pin_info_minimum(x),
00182 _ident(x._ident),
00183 _mask(x._mask),
00184 _invoke_callback(x._invoke_callback)
00185 {
00186 }
00187
00188 size_t _ident;
00189 size_t _mask;
00190 bool _invoke_callback;
00191 };
00192
00195 class udp_header
00196 {
00197 public:
00198 sockaddr_in _addr;
00199 ub4_t _payload;
00200 };
00201
00204 typedef map< size_t, listener_info > listener_map_t;
00207 typedef map< size_t, pin_info > pin_map_t;
00210 typedef node_allocator< fixed_size_buffer > chunk_allocator_t;
00213 typedef list< pin_info_extra > close_list_t;
00214 public:
00216 aiogate(size_t addional_working_threads,
00217 size_t mem_usage
00218 );
00219
00221 virtual ~aiogate();
00222
00223 public:
00224
00226 virtual
00227 void
00228 v_on_error( size_t handle,
00229 int err,
00230 aiosock_type mask,
00231 void* userdata
00232 );
00234 virtual
00235 void
00236 v_on_connect(size_t handle,
00237 const sockaddr_in& peeraddr,
00238 void* userdata
00239 );
00241 virtual
00242 void
00243 v_on_send( size_t handle,
00244 void* buf,
00245 size_t requested,
00246 size_t processed,
00247 const sockaddr_in& toaddr,
00248 void* userdata
00249 );
00251 virtual
00252 void
00253 v_on_receive(size_t handle,
00254 void* buf,
00255 size_t requested,
00256 size_t processed,
00257 const sockaddr_in& toaddr,
00258 void* userdata
00259 );
00262 virtual
00263 void
00264 v_on_accept(size_t handle,
00265 size_t handle_accepted,
00266 terimber_aiosock_callback*& callback,
00267 const sockaddr_in& peeraddr,
00268 void* userdata
00269 );
00270
00271
00272
00274 virtual
00275 size_t
00276 listen( const char* address,
00277 unsigned short port,
00278 size_t max_connection,
00279 unsigned short buffered_acceptors,
00280 terimber_aiogate_pin_factory* factory,
00281 void* arg
00282 );
00283
00285 virtual
00286 void
00287 deaf( size_t ident
00288 );
00289
00294 virtual
00295 size_t
00296 connect( const char* remote,
00297 unsigned short rport,
00298 const char* local,
00299 unsigned short lport,
00300 size_t timeout,
00301 terimber_aiogate_pin_factory* factory,
00302 void* arg
00303 );
00304
00305
00307 virtual
00308 size_t
00309 bind( const char* address,
00310 unsigned short port,
00311 terimber_aiogate_pin_factory* factory,
00312 void* arg
00313 );
00314
00315
00317 virtual
00318 bool
00319 send( size_t ident,
00320 const void* buf,
00321 size_t len,
00322 const sockaddr_in* toaddr
00323 );
00324
00326 virtual
00327 bool
00328 send_bulk( size_t ident,
00329 const terimber_aiogate_buffer* bulk,
00330 size_t count,
00331 const sockaddr_in* toaddr
00332 );
00333
00337 virtual
00338 bool
00339 recv( size_t ident,
00340 bool expect_delivery,
00341 const sockaddr_in* toaddr
00342 );
00344 virtual
00345 bool
00346 close( size_t ident
00347 );
00348
00350 virtual
00351 bool
00352 set_send_timeout(size_t ident,
00353 size_t timeout
00354 );
00355
00357 virtual
00358 bool
00359 set_recv_timeout(size_t ident,
00360 size_t timeout
00361 );
00362
00364 virtual
00365 void
00366 doxray();
00367
00369 bool
00370 on();
00372 void
00373 off();
00374
00375 protected:
00376
00378 virtual
00379 bool
00380 v_has_job( size_t ident,
00381 void* user_data
00382 );
00384 virtual
00385 void
00386 v_do_job( size_t ident,
00387 void* user_data
00388 );
00389
00390 private:
00392 void
00393 lock_pin( mutex_keeper& mtx,
00394 pin_info& info,
00395 ub4_t mask
00396 );
00397
00399 bool
00400 unlock_pin(mutex_keeper& mtx,
00401 size_t ident,
00402 ub4_t mask,
00403 bool unmask_in_progress
00404 );
00405
00407 void
00408 initiate_close(size_t ident,
00409 size_t mask,
00410 bool invoke_callback
00411 );
00413 void
00414 final_close(pin_info_extra& info
00415 );
00416 private:
00417 bool _on;
00418 mutex _pin_mtx;
00419 listener_map_t _listener_map;
00420 pin_map_t _pin_map;
00421 aiosock _pin_port;
00422 chunk_allocator_t _pin_allocator;
00423 close_list_t _pin_list;
00424 thread _pin_thread;
00425 };
00426
00427
00428 #pragma pack()
00429 END_TERIMBER_NAMESPACE
00430
00431 #endif // _terimber_aiogate_h_