Electroneum
Loading...
Searching...
No Matches
abstract_tcp_server_cp.h File Reference
#include <winsock2.h>
#include <rpc.h>
#include <string>
#include <map>
#include <boost/shared_ptr.hpp>
#include "misc_log_ex.h"
#include "syncobj.h"
#include "profile_tools.h"
#include "net_utils_base.h"
#include "pragma_comp_defs.h"
#include "abstract_tcp_server_cp.inl"
Include dependency graph for abstract_tcp_server_cp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  epee::net_utils::cp_server_impl< TProtocol >

Namespaces

namespace  epee
namespace  epee::net_utils

Macros

#define ENABLE_PROFILING
#define ELECTRONEUM_DEFAULT_LOG_CATEGORY   "net"
#define LEVIN_DEFAULT_DATA_BUFF_SIZE   2000

Typedefs

typedef std::map< SOCKET, boost::shared_ptr< connection< TProtocol > > > epee::net_utils::connections_container

Functions

 connection (typename TProtocol::config_type &ref_config)
connection< TProtocol > & operator= (const connection< TProtocol > &obj)
bool init_buffers ()
bool query_shutdown ()
 ~connection ()
virtual bool handle_send (const void *ptr, size_t cb)
else if (0==res)
PRAGMA_WARNING_POP bool epee::net_utils::worker_thread_member ()
bool epee::net_utils::add_new_connection (SOCKET new_sock, long ip_from, int port_from)
bool epee::net_utils::shutdown_connection (connection< TProtocol > *pconn)

Variables

return true
SOCKET epee::net_utils::m_listen_socket
HANDLE epee::net_utils::m_completion_port
connections_container epee::net_utils::m_connections
critical_section epee::net_utils::m_connections_lock
int epee::net_utils::m_port
volatile LONG epee::net_utils::m_stop
bool epee::net_utils::m_initialized
volatile LONG epee::net_utils::m_worker_thread_counter
TProtocol::config_type epee::net_utils::m_config

Macro Definition Documentation

◆ ELECTRONEUM_DEFAULT_LOG_CATEGORY

#define ELECTRONEUM_DEFAULT_LOG_CATEGORY   "net"

Definition at line 46 of file abstract_tcp_server_cp.h.

◆ ENABLE_PROFILING

#define ENABLE_PROFILING

Definition at line 40 of file abstract_tcp_server_cp.h.

◆ LEVIN_DEFAULT_DATA_BUFF_SIZE

#define LEVIN_DEFAULT_DATA_BUFF_SIZE   2000

Definition at line 48 of file abstract_tcp_server_cp.h.

Function Documentation

◆ connection()

resize_send_buff::connection ( typename TProtocol::config_type & ref_config)

Definition at line 188 of file abstract_tcp_server_cp.h.

188 {
189 if(m_psend_data->TotalBuffBytes >= new_size)
190 return true;
Here is the caller graph for this function:

◆ handle_send()

virtual bool resize_send_buff::handle_send ( const void * ptr,
size_t cb )
virtual

Definition at line 234 of file abstract_tcp_server_cp.h.

◆ if()

else resize_send_buff::if ( 0 = res)

Definition at line 265 of file abstract_tcp_server_cp.h.

315{
316namespace net_utils
317{
318
319 template<class TProtocol>
320 class cp_server_impl//: public abstract_handler
321 {
322 public:
323 cp_server_impl(/*abstract_handler* phandler = NULL*/);
324 virtual ~cp_server_impl();
325
326 bool init_server(int port_no);
327 bool deinit_server();
328 bool run_server(int threads_count = 0);
329 bool send_stop_signal();
330 bool is_stop_signal();
331 virtual bool on_net_idle(){return true;}
333 typename TProtocol::config_type& get_config_object(){return m_config;}
334 private:
335 enum overlapped_operation_type
336 {
337 op_type_recv,
338 op_type_send,
339 op_type_stop
340 };
341
342 struct io_data_base
343 {
344 OVERLAPPED m_overlapped;
345 WSABUF DataBuf;
346 overlapped_operation_type m_op_type;
347 DWORD TotalBuffBytes;
348 volatile LONG m_is_in_use;
349 char Buffer[1];
350 };
351
352PRAGMA_WARNING_PUSH
353PRAGMA_WARNING_DISABLE_VS(4355)
354 template<class TProtocol>
355 struct connection: public net_utils::i_service_endpoint
356 {
357 connection(typename TProtocol::config_type& ref_config):m_sock(INVALID_SOCKET), m_tprotocol_handler(this, ref_config, context), m_psend_data(NULL), m_precv_data(NULL), m_asked_to_shutdown(0), m_connection_shutwoned(0)
358 {
359 }
360
361 //connection():m_sock(INVALID_SOCKET), m_tprotocol_handler(this, m_dummy_config, context), m_psend_data(NULL), m_precv_data(NULL), m_asked_to_shutdown(0), m_connection_shutwoned(0)
362 //{
363 //}
364
365 connection<TProtocol>& operator=(const connection<TProtocol>& obj)
366 {
367 return *this;
368 }
369
370 bool init_buffers()
371 {
372 m_psend_data = (io_data_base*)new char[sizeof(io_data_base) + LEVIN_DEFAULT_DATA_BUFF_SIZE-1];
373 m_psend_data->TotalBuffBytes = LEVIN_DEFAULT_DATA_BUFF_SIZE;
374 m_precv_data = (io_data_base*)new char[sizeof(io_data_base) + LEVIN_DEFAULT_DATA_BUFF_SIZE-1];
375 m_precv_data->TotalBuffBytes = LEVIN_DEFAULT_DATA_BUFF_SIZE;
376 return true;
377 }
378
379 bool query_shutdown()
380 {
381 if(!::InterlockedCompareExchange(&m_asked_to_shutdown, 1, 0))
382 {
383 m_psend_data->m_op_type = op_type_stop;
384 ::PostQueuedCompletionStatus(m_completion_port, 0, (ULONG_PTR)this, &m_psend_data->m_overlapped);
385 }
386 return true;
387 }
388
389 //bool set_config(typename TProtocol::config_type& config)
390 //{
391 // this->~connection();
392 // new(this) connection<TProtocol>(config);
393 // return true;
394 //}
396 {
397 if(m_psend_data)
398 delete m_psend_data;
399
400 if(m_precv_data)
401 delete m_precv_data;
402 }
403 virtual bool handle_send(const void* ptr, size_t cb)
404 {
405 PROFILE_FUNC("[handle_send]");
406 if(m_psend_data->TotalBuffBytes < cb)
407 resize_send_buff((DWORD)cb);
408
409 ZeroMemory(&m_psend_data->m_overlapped, sizeof(OVERLAPPED));
410 m_psend_data->DataBuf.len = (u_long)cb;//m_psend_data->TotalBuffBytes;
411 m_psend_data->DataBuf.buf = m_psend_data->Buffer;
412 memcpy(m_psend_data->DataBuf.buf, ptr, cb);
413 m_psend_data->m_op_type = op_type_send;
414 InterlockedExchange(&m_psend_data->m_is_in_use, 1);
415 DWORD bytes_sent = 0;
416 DWORD flags = 0;
417 int res = 0;
418 {
419 PROFILE_FUNC("[handle_send] ::WSASend");
420 res = ::WSASend(m_sock, &(m_psend_data->DataBuf), 1, &bytes_sent, flags, &(m_psend_data->m_overlapped), NULL);
421 }
422
423 if(res == SOCKET_ERROR )
424 {
425 int err = ::WSAGetLastError();
426 if(WSA_IO_PENDING == err )
427 return true;
428 }
429 LOG_ERROR("BIG FAIL: WSASend error code not correct, res=" << res << " last_err=" << err);
430 ::InterlockedExchange(&m_psend_data->m_is_in_use, 0);
432 //closesocket(m_psend_data);
433 return false;
434 }else if(0 == res)
435 {
436 ::InterlockedExchange(&m_psend_data->m_is_in_use, 0);
437 if(!bytes_sent || bytes_sent != cb)
438 {
439 int err = ::WSAGetLastError();
440 LOG_ERROR("BIG FAIL: WSASend immediatly complete? but bad results, res=" << res << " last_err=" << err);
442 return false;
443 }else
444 {
445 return true;
446 }
447 }
448
449 return true;
450 }
451 bool resize_send_buff(DWORD new_size)
452 {
453 if(m_psend_data->TotalBuffBytes >= new_size)
454 return true;
455
456 delete m_psend_data;
457 m_psend_data = (io_data_base*)new char[sizeof(io_data_base) + new_size-1];
458 m_psend_data->TotalBuffBytes = new_size;
459 LOG_PRINT("Connection buffer resized up to " << new_size, LOG_LEVEL_3);
460 return true;
461 }
462
463
464 SOCKET m_sock;
465 net_utils::connection_context_base context;
466 TProtocol m_tprotocol_handler;
467 typename TProtocol::config_type m_dummy_config;
468 io_data_base* m_precv_data;
469 io_data_base* m_psend_data;
470 HANDLE m_completion_port;
471 volatile LONG m_asked_to_shutdown;
472 volatile LONG m_connection_shutwoned;
473 };
474PRAGMA_WARNING_POP
475
477 static unsigned CALLBACK worker_thread(void* param);
478
479 bool add_new_connection(SOCKET new_sock, long ip_from, int port_from);
481
482
483 typedef std::map<SOCKET, boost::shared_ptr<connection<TProtocol> > > connections_container;
485 HANDLE m_completion_port;
487 critical_section m_connections_lock;
488 int m_port;
489 volatile LONG m_stop;
490 //abstract_handler* m_phandler;
491 bool m_initialized;
492 volatile LONG m_worker_thread_counter;
493 typename TProtocol::config_type m_config;
494 };
495}
496}
497#include "abstract_tcp_server_cp.inl"
498
499
500#endif //_LEVIN_SERVER_H_
virtual bool handle_send(const void *ptr, size_t cb)
connection< TProtocol > & operator=(const connection< TProtocol > &obj)
bool query_shutdown()
bool init_buffers()
#define LEVIN_DEFAULT_DATA_BUFF_SIZE
connection(typename TProtocol::config_type &ref_config)
Represents a single connection from a client.
TProtocol::config_type & get_config_object()
bool run_server(int threads_count=0)
void * memcpy(void *a, const void *b, size_t c)
const char * res
#define INVALID_SOCKET
#define SOCKET
#define LOG_ERROR(x)
Definition misc_log_ex.h:98
bool shutdown_connection(connection< TProtocol > *pconn)
connections_container m_connections
TProtocol::config_type m_config
PRAGMA_WARNING_POP bool worker_thread_member()
volatile LONG m_worker_thread_counter
bool add_new_connection(SOCKET new_sock, long ip_from, int port_from)
std::map< SOCKET, boost::shared_ptr< connection< TProtocol > > > connections_container
critical_section m_connections_lock
#define PROFILE_FUNC(immortal_ptr_str)
Here is the caller graph for this function:

◆ init_buffers()

bool resize_send_buff::init_buffers ( )

Definition at line 201 of file abstract_tcp_server_cp.h.

◆ operator=()

connection< TProtocol > & resize_send_buff::operator= ( const connection< TProtocol > & obj)

Definition at line 196 of file abstract_tcp_server_cp.h.

Here is the caller graph for this function:

◆ query_shutdown()

bool resize_send_buff::query_shutdown ( )

Definition at line 210 of file abstract_tcp_server_cp.h.

◆ ~connection()

resize_send_buff::~connection ( )

Definition at line 226 of file abstract_tcp_server_cp.h.

Variable Documentation

◆ true

return true

Definition at line 280 of file abstract_tcp_server_cp.h.