Electroneum
protocol_switcher.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 
29 #ifndef _PROTOCOL_SWITCHER_H_
30 #define _PROTOCOL_SWITCHER_H_
31 
32 #include "levin_base.h"
33 #include "http_server.h"
34 #include "levin_protocol_handler.h"
35 //#include "abstract_tcp_server.h"
36 
37 namespace epee
38 {
39 namespace net_utils
40 {
42  {
45  };
46 
47 
49  {
50  virtual bool handle_recv(const void* ptr, size_t cb)=0;
51  };
52 
53  template<class t>
55  {
56  public:
57  typedef t t_type;
58  t_protocol_handler(i_service_endpoint* psnd_hndlr, typename t_type::config_type& config, const connection_context& conn_context):m_hadler(psnd_hndlr, config, conn_context)
59  {}
60  private:
61  bool handle_recv(const void* ptr, size_t cb)
62  {
63  return m_hadler.handle_recv(ptr, cb);
64  }
65  t_type m_hadler;
66  };
67 
68 
70  {
71  public:
73 
75  virtual ~protocol_switcher(){}
76 
77  virtual bool handle_recv(const void* ptr, size_t cb);
78 
79  bool after_init_connection(){return true;}
80  private:
83  i_protocol_handler* pcurrent_handler;
84 
85  std::string m_cached_buff;
86  };
87 
88  protocol_switcher::protocol_switcher(net_utils::i_service_endpoint* psnd_hndlr, config_type& config, const net_utils::connection_context_base& conn_context):m_http_handler(psnd_hndlr, config.m_http_config, conn_context), m_levin_handler(psnd_hndlr, config.m_levin_config, conn_context), pcurrent_handler(NULL)
89  {}
90 
91  bool protocol_switcher::handle_recv(const void* ptr, size_t cb)
92  {
93  if(pcurrent_handler)
94  return pcurrent_handler->handle_recv(ptr, cb);
95  else
96  {
97  m_cached_buff.append((const char*)ptr, cb);
98  if(m_cached_buff.size() < sizeof(uint64_t))
99  return true;
100 
101  if(*((uint64_t*)&m_cached_buff[0]) == LEVIN_SIGNATURE)
102  {
103  pcurrent_handler = &m_levin_handler;
104  return pcurrent_handler->handle_recv(m_cached_buff.data(), m_cached_buff.size());
105  }
106  if(m_cached_buff.substr(0, 4) == "GET " || m_cached_buff.substr(0, 4) == "POST")
107  {
108  pcurrent_handler = &m_http_handler;
109  return pcurrent_handler->handle_recv(m_cached_buff.data(), m_cached_buff.size());
110  }else
111  {
112  LOG_ERROR("Wrong protocol accepted on port...");
113  return false;
114  }
115  }
116 
117  return true;
118  }
119 }
120 }
121 #endif //_PROTOCOL_SWITCHER_H_
#define LEVIN_SIGNATURE
Definition: levin_base.h:34
t_protocol_handler(i_service_endpoint *psnd_hndlr, typename t_type::config_type &config, const connection_context &conn_context)
::std::string string
Definition: gtest-port.h:1097
virtual bool handle_recv(const void *ptr, size_t cb)=0
unsigned __int64 uint64_t
Definition: stdint.h:136
levin::protocol_handler::config_type m_levin_config
http::http_custom_handler::config_type m_http_config
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
protocol_switcher(net_utils::i_service_endpoint *psnd_hndlr, config_type &config, const net_utils::connection_context_base &conn_context)
protocl_switcher_config config_type
virtual bool handle_recv(const void *ptr, size_t cb)