SEGS  0.6.1-develop [3e741b81]
Super Entity Game Server
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 - 2019 SEGS Team (see AUTHORS.md)
5  * This software is licensed under the terms of the 3-clause BSD License. See LICENSE.md for details.
6  */
7 
8 #pragma once
9 #include "CompiletimeHash.h"
10 #include <ace/Time_Value.h>
11 #include "cereal/archives/binary.hpp"
12 #include <atomic>
13 #include <typeinfo>
14 #include <cassert>
15 
16 class EventSrc;
17 namespace SEGSEvents
18 {
19 // Helper macros to ease the definition of event types
20 #define BEGINE_EVENTS(enum_name,parent_class) base_##enum_name = (uint32_t)parent_class::ID_LAST_##parent_class,
21 #define BEGINE_EVENTS_SKIP(enum_name,parent_class,skip) base_##enum_name = (uint32_t)parent_class::ID_LAST_##parent_class + skip,
22 #define BEGINE_EVENTS_INTERNAL(enum_name) base_##enum_name = 20000,
23 #define EVENT_DECL(enum_name,name,cnt) name = base_##enum_name+cnt+1,
24 #define END_EVENTS(enum_name,cnt) ID_LAST_##enum_name=base_##enum_name+cnt+1
25 
26 enum CommonTypes : uint32_t
27 {
28  evFinish=0, // this event will finish the Processor that receives it
33 };
34 
35 class Event
36 {
37 protected:
38  const uint32_t m_type;
39  std::atomic<int> m_ref_count {1}; // used to prevent event being deleted when it's in multiple queues
41 
42 public:
43 
44 virtual ~Event()
45  {
46  // we allow delete when there is 1 reference left (static variables on exit)
47  assert(m_ref_count<=1);
48  m_event_source=nullptr;
49  }
50  Event(uint32_t evtype,EventSrc *ev_src=nullptr) :
51  m_type(evtype),m_event_source(ev_src)
52  {}
53  Event * shallow_copy() // just incrementing the ref count
54  {
55  ++m_ref_count;
56  return this;
57  }
58  void release()
59  {
60  if(0==--m_ref_count)
61  delete this;
62  }
63  int get_ref_count() const {return m_ref_count; }
64  void src(EventSrc *ev_src) {m_event_source=ev_src;}
65  EventSrc * src() const {return m_event_source;}
66  uint32_t type() const {return m_type;}
67 virtual const char * info();
68 
69 protected:
70 // Note those are friend functions that will store/restore this message in the std::stream.
71 friend Event * from_storage(std::istream &istr);
72 friend void to_storage(std::ostream &ostr,Event *ev);
73 
74 virtual void do_serialize(std::ostream &os) = 0;
75 virtual void serialize_from(std::istream &os) = 0;
76 };
77 #define EVENT_IMPL(name)\
78  template<class Archive>\
79  void serialize(Archive & archive); \
80  void do_serialize(std::ostream &os) override {\
81  cereal::BinaryOutputArchive oarchive(os);\
82  oarchive(*this);\
83  }\
84  void serialize_from(std::istream &os) override {\
85  cereal::BinaryInputArchive iarchive(os);\
86  iarchive(*this);\
87  }\
88  ~name() override = default;
89 
90 
91 // [[ev_def:type]]
92 struct Finish final: public Event
93 {
94 public:
95  Finish(EventSrc *source=nullptr) : Event(evFinish,source) {}
96 static Finish * s_instance;
98 };
99 } // end of SEGSEventsNamespace
friend Event * from_storage(std::istream &istr)
Definition: SEGSEventFactory.cpp:57
EventSrc * m_event_source
Definition: SEGSEvent.h:40
Event * shallow_copy()
Definition: SEGSEvent.h:53
int get_ref_count() const
Definition: SEGSEvent.h:63
Event(uint32_t evtype, EventSrc *ev_src=nullptr)
Definition: SEGSEvent.h:50
Definition: EventProcessor.h:14
virtual void do_serialize(std::ostream &os)=0
Definition: SEGSEvent.h:31
Definition: SEGSEvent.h:35
Definition: LinkLevelEvent.h:13
std::atomic< int > m_ref_count
Definition: SEGSEvent.h:39
virtual const char * info()
Definition: SEGSEvent.cpp:19
void src(EventSrc *ev_src)
Definition: SEGSEvent.h:64
static Finish * s_instance
Definition: SEGSEvent.h:96
on the link level this means a new connection, higher level handlers are also notified by this event ...
Definition: SEGSEvent.h:30
uint32_t type() const
Definition: SEGSEvent.h:66
#define EVENT_IMPL(name)
Definition: SEGSEvent.h:77
Definition: SEGSEvent.h:28
friend void to_storage(std::ostream &ostr, Event *ev)
Definition: SEGSEventFactory.cpp:75
Finish(EventSrc *source=nullptr)
Definition: SEGSEvent.h:95
Definition: SEGSEvent.h:32
void release()
Definition: SEGSEvent.h:58
virtual void serialize_from(std::istream &os)=0
EventSrc * src() const
Definition: SEGSEvent.h:65
Definition: SEGSEvent.h:92
const uint32_t m_type
Definition: SEGSEvent.h:38
virtual ~Event()
Definition: SEGSEvent.h:44
Definition: SEGSEvent.h:29
CommonTypes
Definition: SEGSEvent.h:26