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_msg_comm_h_
00029 #define _terimber_msg_comm_h_
00030
00031 #include "base/except.h"
00032 #include "base/map.h"
00033 #include "base/keymaker.h"
00034 #include "crypt/crypt.h"
00035 #include "aiosock/aiosock.h"
00036 #include "threadpool/timer.h"
00037 #include "threadpool/threadpoolfactory.h"
00038 #include "xml/sxml.h"
00039 #include "aiomsg/msg_que.h"
00040
00041
00042 extern guid_t uuid_gen();
00043
00044 BEGIN_TERIMBER_NAMESPACE
00045 #pragma pack(4)
00046
00047
00048 class msg_connection;
00049 class msg_communicator;
00050 class msg_listener;
00051
00053 #ifdef _DEBUG
00054 const size_t rsa_key_size = 256;
00055 #else
00056 const size_t rsa_key_size = 2048;
00057 #endif
00058
00060 static const size_t stay_on_alert_time = 10000;
00062 static const size_t max_thread_capacity = 64;
00063
00071 class msg_communicator : public msg_queue_processor,
00072 public timer_callback,
00073 public terimber_log_helper
00074 {
00077 typedef list< msg_listener* > listener_list_t;
00080 typedef map< guid_t, msg_connection* > connection_map_t;
00083 typedef map< guid_t, msg_communicator* > this_map_t;
00084
00085 public:
00087 msg_communicator();
00089 ~msg_communicator();
00094 void
00095 init( const char* info_,
00096 const char* ini_key_
00097 );
00102 void
00103 init( const void* info_,
00104 size_t len
00105 );
00106
00109 void
00110 init( xml_designer* nav
00111 );
00114
00115 void
00116 uninit();
00118 void
00119 add_connection(msg_connection* connection
00120 );
00121
00122 void
00123 change_connection_address(const guid_t& old_address,
00124 msg_connection* connection
00125 );
00126
00127
00131 void
00132 shutdown_connection(msg_connection* connection
00133 );
00135 inline
00136 const guid_t&
00137 get_address() const
00138 {
00139 return _address;
00140 }
00143 msg_cpp*
00144 construct_msg(size_t size
00145 );
00147 bool
00148 resize_msg( msg_cpp* msg,
00149 size_t size
00150 );
00152 msg_cpp*
00153 copy_msg( msg_cpp* msg
00154 );
00156 bool
00157 destroy_msg(msg_cpp* msg
00158 );
00160 void
00161 get_msg_key(room_byte_t& crypt_private,
00162 const room_byte_t& crypt_external,
00163 const guid_t& session
00164 ) const;
00166 inline
00167 static
00168 msg_cpp*
00169 cast( msg_t* msg
00170 )
00171 {
00172 try
00173 {
00174 return dynamic_cast< msg_cpp * >(static_cast< msg_cpp * >(msg));
00175 }
00176 catch (...)
00177 {
00178 return 0;
00179 }
00180 }
00182 inline
00183 static
00184 const msg_cpp*
00185 cast( const msg_t* msg
00186 )
00187 {
00188 try
00189 {
00190 return dynamic_cast< const msg_cpp * >(static_cast< const msg_cpp * >(msg));
00191 }
00192 catch (...)
00193 {
00194 return 0;
00195 }
00196 }
00198 inline
00199 static
00200 bool
00201 set_receiver( msg_cpp* msg,
00202 const guid_t& receiver
00203 )
00204 {
00205 if (!msg)
00206 return false;
00207 msg->_receiver = receiver;
00208 return true;
00209 }
00211 inline
00212 static
00213 bool
00214 get_sender( const msg_cpp* msg,
00215 guid_t& sender
00216 )
00217 {
00218 if (!msg)
00219 return false;
00220 sender = msg->_sender;
00221 return true;
00222 }
00224 msg_connection*
00225 find_connection(const guid_t& addr
00226 );
00228 msg_listener*
00229 find_listener( transport_type type
00230 );
00233 bool
00234 validate_connection(const msg_connection* connection
00235 ) const;
00239 void
00240 comm_msg( msg_cpp* msg
00241 );
00244 msg_cpp*
00245 construct_handshake(const rsa* rsa
00246 );
00250 msg_cpp*
00251 reply_handshake(const msg_cpp* msg,
00252 room_byte_t* symetric_private_key
00253 );
00256 void
00257 check_handshake(const guid_t& sessionid,
00258 const msg_cpp* reply,
00259 const rsa* rsa,
00260 room_byte_t& symetric_private_key
00261 );
00264 msg_cpp*
00265 construct_ping();
00267 aiosock&
00268 get_aiosock();
00270 threadpool&
00271 get_thread_manager();
00273 event_pool_t&
00274 get_event_pool();
00275
00279 void
00280 add_connection_config(const conf_connection& atom
00281 );
00283 void
00284 add_listener_config(const conf_listener& atom,
00285 bool start
00286 );
00287
00289 void
00290 remove_connection_config(const guid_t& address
00291 );
00293 void
00294 remove_listener_config(transport_type type
00295 );
00297 static
00298 void
00299 parse_connection(xml_designer* nav,
00300 conf_connection& atom
00301 );
00303 static
00304 void
00305 parse_listener( xml_designer* nav,
00306 conf_listener& atom
00307 );
00308
00310 static msg_communicator* loan_communicator(const guid_t& addr);
00312 static void return_communicator(const guid_t& addr, msg_communicator* comm);
00313
00315 void doxray();
00316
00318 void
00319 log_msg( const msg_cpp* msg
00320 );
00321
00322 protected:
00324 virtual
00325 void
00326 v_do_job( size_t ident,
00327 void* user_data
00328 );
00331 virtual
00332 void
00333 v_on();
00336 virtual
00337 void
00338 v_off();
00340 virtual
00341 void
00342 wakeup();
00344 virtual
00345 void
00346 notify( size_t ident,
00347 size_t interval,
00348 size_t multiplier
00349 );
00350
00351 private:
00354 msg_connection*
00355 _connect( const conf_connection& info_,
00356 ub8_t timeout
00357 );
00358
00360 void
00361 _close_connection(const guid_t& addr
00362 );
00364 msg_cpp*
00365 _generate_crypt_private_key(const rsa& rsa,
00366 room_byte_t& symetric_private_key
00367 );
00369 msg_cpp*
00370 _pack_keys( const integer& n,
00371 const integer& e
00372 );
00374 void
00375 _extract_keys( const msg_cpp* msg,
00376 integer& n,
00377 integer& e
00378 );
00380 void
00381 _decrypt_private_key(const msg_cpp* reply,
00382 const rsa& rsa,
00383 room_byte_t& symetric_private_key
00384 );
00386 void
00387 _turn_off_listeners();
00389 void
00390 _turn_on_listeners();
00392 void
00393 _destroy_listeners();
00395 void
00396 _turn_off_connections();
00398 void
00399 _turn_on_connections();
00401 void
00402 _destroy_connections();
00404 void
00405 _create_listeners();
00407 void
00408 _create_listener(const conf_listener& atom
00409 );
00410
00412 void _register_this();
00414 void _revoke_this();
00415
00416
00417 private:
00418 guid_t _address;
00419 guid_t _debug_address;
00420 mutex _mtx_listeners;
00421 listener_list_t _listeners;
00422 mutex _mtx_conn;
00423 connection_map_t _connections;
00424 mem_pool_t _manager;
00425 threadpool _thread_manager;
00426 aiosock _aio_port;
00427 timer _ping_timer;
00428 mutex _mtx_config;
00429 conf_unit _config;
00430 event_pool_t _event_pool;
00431 static keylocker _this_access;
00432
00433 static this_map_t _this_map;
00434 };
00435
00438 class msg_creator : public proto_creator< msg_creator, msg_cpp, size_t >
00439 {
00440 public:
00442 msg_creator( msg_communicator* communicator
00443 ) :
00444 _communicator(communicator)
00445 {
00446 }
00448 msg_cpp*
00449 create( size_t n
00450 )
00451 {
00452 return _communicator->construct_msg(n);
00453 }
00455 void
00456 destroy( msg_cpp* obj
00457 )
00458 {
00459 if (obj)
00460 _communicator->destroy_msg(obj);
00461 }
00462 private:
00463 msg_communicator* _communicator;
00464 };
00465
00468 typedef smart_pointer< msg_creator > msg_pointer_t;
00469
00470 #pragma pack()
00471 END_TERIMBER_NAMESPACE
00472
00473 #endif // _terimber_msg_comm_h_
00474