SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ServerEndpoint.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt for details)
6  */
7 
8 #pragma once
9 #include "EventProcessor.h"
10 
11 #include <ace/ACE.h>
12 #include <ace/Synch.h>
13 #include <ace/INET_Addr.h>
14 #include <ace/SOCK_Dgram.h>
15 #include <ace/Reactor_Notification_Strategy.h>
16 
17 #include <unordered_map>
18 
19 namespace std
20 {
21  // std::hash override for ACE_INET_Addr
22  template<>
23  class hash<ACE_INET_Addr>
24  {
25  public:
26  size_t operator()( const ACE_INET_Addr& Key ) const
27  {
28  std::hash<ACE_UINT32> hasher;
29  return hasher(Key.get_ip_address()) ^ (hasher(Key.get_port_number()<<8));
30  }
31  };
32 }
33 
34 class SEGSEvent;
35 
36 class CRUDLink;
37 
38 // This class represents a UDP port<->Client link router
39 // when it receives any bytes it will pass them to appropriate ILink instance
40 // when it receives PacketEvents from ILink it sends the bytes over the UDP to associated address
42 {
43  typedef EventProcessor super;
44  typedef std::unordered_map<ACE_INET_Addr,CRUDLink *> hmAddrProto;
45 public:
46 
47  ServerEndpoint(const ACE_INET_Addr &local_addr) :
48  m_notifier(nullptr, nullptr, ACE_Event_Handler::WRITE_MASK),
49  endpoint_ (local_addr), m_downstream(nullptr)
50  {
51  m_notifier.event_handler(this);
52  }
54 private:
55  // Part of the low level ace interface, not passed on to derived classes
56  ACE_HANDLE get_handle(void) const override { return this->endpoint_.get_handle(); }
57  int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE) override;
58  int handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE) override;
59  int handle_close (ACE_HANDLE /*handle*/,ACE_Reactor_Mask /*close_mask*/) override;
60 public:
61  int open(void *p=nullptr) override;
63 protected:
64  void dispatch(SEGSEvent *) override
65  {
66  ACE_ASSERT(!"All events are dispatched from handle_* methods");
67  }
69  CRUDLink * getClientLink(const ACE_INET_Addr &from_addr);
70 virtual CRUDLink * createLink(EventProcessor *down) = 0;
71 
72  hmAddrProto client_links;
73  ACE_Reactor_Notification_Strategy m_notifier;
74  ACE_SOCK_Dgram endpoint_; // Wrapper for sending/receiving dgrams.
75  ACE_Thread_Mutex m_send_sema;
77 };
78 
80 {
81  ACE_INET_Addr m_listen_addr;
82  ACE_INET_Addr m_location_addr;
83  ListenAndLocationAddresses(ACE_INET_Addr listen,ACE_INET_Addr location,uint16_t inc=0)
84  {
85  m_listen_addr = listen;
86  m_location_addr = location;
87  m_listen_addr.set_port_number(listen.get_port_number()+inc);
88  m_location_addr.set_port_number(location.get_port_number()+inc);
89  }
91  {
94  m_listen_addr.set_port_number(oth.m_listen_addr.get_port_number()+inc);
95  m_location_addr.set_port_number(oth.m_location_addr.get_port_number()+inc);
96  }
97 };
void set_downstream(EventProcessor *ds)
Definition: ServerEndpoint.h:62
hmAddrProto client_links
Definition: ServerEndpoint.h:72
EventProcessor * m_downstream
All created links will have this as their downstream target.
Definition: ServerEndpoint.h:76
Definition: SEGSEvent.h:32
ListenAndLocationAddresses(const ListenAndLocationAddresses &oth, uint16_t inc=0)
Definition: ServerEndpoint.h:90
CRUDLink * createLinkInstance()
Definition: ServerEndpoint.cpp:97
ServerEndpoint(const ACE_INET_Addr &local_addr)
Definition: ServerEndpoint.h:47
Definition: ServerEndpoint.h:41
ACE_Reactor_Notification_Strategy m_notifier
Definition: ServerEndpoint.h:73
int open(void *p=nullptr) override
Definition: ServerEndpoint.cpp:82
ListenAndLocationAddresses(ACE_INET_Addr listen, ACE_INET_Addr location, uint16_t inc=0)
Definition: ServerEndpoint.h:83
virtual CRUDLink * createLink(EventProcessor *down)=0
Definition: ServerEndpoint.h:79
ACE_INET_Addr m_listen_addr
Definition: ServerEndpoint.h:81
size_t operator()(const ACE_INET_Addr &Key) const
Definition: ServerEndpoint.h:26
ACE_Thread_Mutex m_send_sema
Definition: ServerEndpoint.h:75
CRUDLink * getClientLink(const ACE_INET_Addr &from_addr)
Definition: ServerEndpoint.cpp:108
Definition: EventProcessor.h:13
~ServerEndpoint()
Definition: ServerEndpoint.cpp:21
ACE_SOCK_Dgram endpoint_
Definition: ServerEndpoint.h:74
ACE_INET_Addr m_location_addr
Definition: ServerEndpoint.h:82
void dispatch(SEGSEvent *) override
Definition: ServerEndpoint.h:64