SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SEGSEvent.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 <ace/Time_Value.h>
10 #include <atomic>
11 #include <typeinfo>
12 #include <cassert>
13 class EventProcessor;
14 
15 // Helper defines to ease the definition of event types
16 #define INIT_EVENTS() enum : uint32_t { base = 0,
17 #define BEGINE_EVENTS(parent_class) enum : uint32_t { base = parent_class::evLAST_EVENT,
18 #define BEGINE_EVENTS_INTERNAL() enum : uint32_t { base = 20000,
19 #define EVENT_DECL(name,cnt) name = base+cnt+1,
20 #define END_EVENTS(cnt) evLAST_EVENT=base+cnt+1};
22 {
23 public:
24  INIT_EVENTS()
25  EVENT_DECL(evFinish,0) // this event will finish the Processor that receives it
26  EVENT_DECL(evConnect,1)
27  EVENT_DECL(evDisconnect,2)
28  EVENT_DECL(evTimeout,3)
29  END_EVENTS(4)
30 };
31 
32 class SEGSEvent
33 {
34 protected:
35  const uint32_t m_type;
37  std::atomic<int> m_ref_count {1}; // used to prevent event being deleted when it's in multiple queues
38 
39 public:
40 
41 virtual ~SEGSEvent()
42  {
43  // we allow delete when there is 1 reference left (static variables on exit)
44  assert(m_ref_count<=1);
45  m_event_source=nullptr;
46  }
47  SEGSEvent(uint32_t evtype,EventProcessor *ev_src=nullptr) :
48  m_type(evtype),m_event_source(ev_src)
49  {}
50  SEGSEvent * shallow_copy() // just incrementing the ref count
51  {
52  ++m_ref_count;
53  return this;
54  }
55  void release()
56  {
57  if(0==--m_ref_count)
58  delete this;
59  }
60  int get_ref_count() const {return m_ref_count; }
61  void src(EventProcessor *ev_src) {m_event_source=ev_src;}
62  EventProcessor *src() const {return m_event_source;}
63  uint32_t type() const {return m_type;}
64 virtual const char * info();
65 
67 };
68 
69 class TimerEvent final: public SEGSEvent
70 {
71  ACE_Time_Value m_arrival_time;
72  void * m_data;
73 
74 public:
75  TimerEvent(const ACE_Time_Value &time, void *dat,EventProcessor *source)
76  : SEGSEvent(SEGS_EventTypes::evTimeout,source), m_arrival_time(time), m_data(dat)
77  {
78  }
79  void * data() { return m_data; }
80  const ACE_Time_Value & arrival_time() { return m_arrival_time; }
81 };
TimerEvent(const ACE_Time_Value &time, void *dat, EventProcessor *source)
Definition: SEGSEvent.h:75
const uint32_t m_type
Definition: SEGSEvent.h:35
virtual ~SEGSEvent()
Definition: SEGSEvent.h:41
Definition: SEGSEvent.h:32
uint32_t type() const
Definition: SEGSEvent.h:63
SEGSEvent * shallow_copy()
Definition: SEGSEvent.h:50
Definition: SEGSEvent.h:21
#define END_EVENTS(cnt)
Definition: SEGSEvent.h:20
EVENT_DECL(evFinish, 0) EVENT_DECL(evConnect
std::atomic< int > m_ref_count
Definition: SEGSEvent.h:37
void release()
Definition: SEGSEvent.h:55
static SEGSEvent s_ev_finish
Definition: SEGSEvent.h:66
virtual const char * info()
Definition: SEGSEvent.cpp:17
const ACE_Time_Value & arrival_time()
Definition: SEGSEvent.h:80
int get_ref_count() const
Definition: SEGSEvent.h:60
Definition: EventProcessor.h:13
EventProcessor * m_event_source
Definition: SEGSEvent.h:36
#define INIT_EVENTS()
Definition: SEGSEvent.h:16
void * data()
Definition: SEGSEvent.h:79
SEGSEvent(uint32_t evtype, EventProcessor *ev_src=nullptr)
Definition: SEGSEvent.h:47
Definition: SEGSEvent.h:69
void src(EventProcessor *ev_src)
Definition: SEGSEvent.h:61
EventProcessor * src() const
Definition: SEGSEvent.h:62