SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRUD_Events.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 "Buffer.h"
10 #include "CRUDP_Packet.h"
11 #include "CRUDP_Protocol.h"
12 #include "EventProcessor.h"
13 #include "LinkLevelEvent.h"
14 
15 #include <memory>
17 {
18 public:
20  EVENT_DECL(evPacket, 0)
21  EVENT_DECL(evControl, 1)
22  EVENT_DECL(evDisconnectRequest, 2)
23  EVENT_DECL(evIdle, 3)
24  EVENT_DECL(evUnknownEvent, 4)
25  EVENT_DECL(evDisconnectResponse, 5)
26  EVENT_DECL(evConnectRequest, 6)
27  EVENT_DECL(evConnectResponse, 7)
28  END_EVENTS(8)
29 };
30 class PacketEvent : public SEGSEvent
31 {
32 public:
33  PacketEvent(EventProcessor *evsrc, std::unique_ptr<CrudP_Packet> &&pkt, const ACE_INET_Addr &tgt)
34  : SEGSEvent(CRUD_EventTypes::evPacket, evsrc), m_pkt(std::move(pkt)), target(tgt)
35  {
36  }
37  std::unique_ptr<CrudP_Packet> m_pkt;
38  ACE_INET_Addr target;
39  const uint8_t *bytes() const { return m_pkt->GetStream()->read_ptr(); }
40  size_t size() const { return m_pkt->GetStream()->GetReadableDataSize(); }
41 };
42 class CRUDLink_Event : public SEGSEvent
43 {
44 public:
45  CRUDLink_Event(size_t evtype, EventProcessor *ev_src = nullptr) : SEGSEvent(evtype, ev_src) //,LINK
46  {
47  }
48  virtual void serializeto(BitStream &) const = 0;
49  virtual void serializefrom(BitStream &) = 0;
50  size_t m_seq_number;
51  bool m_reliable = true;
52 };
53 
55 {
56 public:
57  ConnectRequest() : CRUDLink_Event(CRUD_EventTypes::evConnectRequest) {}
58  void serializeto(BitStream &tgt) const override
59  {
60  tgt.StorePackedBits(1, 1); // opcode 1
62  tgt.StorePackedBits(1, m_version);
63  }
64  void serializefrom(BitStream &src) override
65  {
66  m_tickcount = src.GetPackedBits(1);
67  m_version = src.GetPackedBits(1);
68  }
69  uint32_t m_tickcount;
70  uint32_t m_version;
71 };
72 
74 {
75 public:
76  ConnectResponse() : CRUDLink_Event(CRUD_EventTypes::evConnectResponse) {}
77  void serializeto(BitStream &tgt) const override
78  {
79  tgt.StorePackedBits(1, 0); // ctrl opcode
80  tgt.StorePackedBits(1, 4); // opcode
81  }
82  void serializefrom(BitStream &) override {}
83 };
85 {
86 public:
87  DisconnectRequest() : CRUDLink_Event(CRUD_EventTypes::evDisconnectRequest) {}
88  void serializefrom(BitStream &bs) override
89  {
90  bs.GetPackedBits(1);
91  bs.GetPackedBits(1);
92  }
93  void serializeto(BitStream &bs) const override
94  {
95  bs.StorePackedBits(1, 0);
96  bs.StorePackedBits(1, 5);
97  }
98 };
100 {
101 public:
102  DisconnectResponse() : CRUDLink_Event(CRUD_EventTypes::evDisconnectResponse) {}
103  void serializefrom(BitStream &bs) override
104  {
105  bs.GetPackedBits(1);
106  bs.GetPackedBits(1);
107  }
108  void serializeto(BitStream &bs) const override
109  {
110  bs.StorePackedBits(1, 0);
111  bs.StorePackedBits(1, 6);
112  }
113 };
114 class IdleEvent : public CRUDLink_Event
115 {
116 public:
118  void serializefrom(BitStream &bs) override
119  {
120  // TODO: check this
121  bs.GetPackedBits(1);
122  bs.GetPackedBits(1);
123  }
124  void serializeto(BitStream &bs) const override
125  {
126  bs.StorePackedBits(1, 0);
127  bs.StorePackedBits(1, 0);
128  }
129 };
130 
132 {
133 public:
135  {
136  int32_t opcode = bs.GetPackedBits(1);
137  if (opcode != 0)
138  return nullptr;
139  // it seems idle commands can be shortened to only contain opcode==0.
140  int32_t control_opcode = bs.GetReadableBits() == 0 ? 0 : bs.GetPackedBits(1);
141  switch (control_opcode)
142  {
143  case 0:
144  return new IdleEvent(); // CTRL_IDLE
145  case 5:
146  return new DisconnectRequest(); // CTRL_DISCONNECT_REQ
147  default: break;
148  }
149  ACE_DEBUG((LM_WARNING, ACE_TEXT("Unhandled control event type %d\n"), control_opcode));
150  return nullptr;
151  }
152 };
void serializeto(BitStream &bs) const override
Definition: CRUD_Events.h:93
void serializefrom(BitStream &src) override
Definition: CRUD_Events.h:64
void serializefrom(BitStream &bs) override
Definition: CRUD_Events.h:103
DisconnectResponse()
Definition: CRUD_Events.h:102
Definition: SEGSEvent.h:32
Definition: CRUD_Events.h:16
#define BEGINE_EVENTS(parent_class)
Definition: SEGSEvent.h:17
virtual CRUDLink_Event * EventFromStream(BitStream &bs)
Definition: CRUD_Events.h:134
uint32_t m_version
Definition: CRUD_Events.h:70
int32_t GetPackedBits(uint32_t minbits)
Definition: BitStream.cpp:285
Definition: BitStream.h:30
void serializefrom(BitStream &) override
Definition: CRUD_Events.h:82
Definition: SEGSEvent.h:21
#define END_EVENTS(cnt)
Definition: SEGSEvent.h:20
IdleEvent()
Definition: CRUD_Events.h:117
void serializeto(BitStream &tgt) const override
Definition: CRUD_Events.h:58
PacketEvent(EventProcessor *evsrc, std::unique_ptr< CrudP_Packet > &&pkt, const ACE_INET_Addr &tgt)
Definition: CRUD_Events.h:33
void StorePackedBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:161
Definition: CRUD_Events.h:30
void serializeto(BitStream &bs) const override
Definition: CRUD_Events.h:124
ConnectRequest()
Definition: CRUD_Events.h:57
Definition: CRUD_Events.h:84
Definition: CRUD_Events.h:99
uint32_t m_tickcount
Definition: CRUD_Events.h:69
const uint8_t * bytes() const
Definition: CRUD_Events.h:39
void serializeto(BitStream &tgt) const override
Definition: CRUD_Events.h:77
void serializefrom(BitStream &bs) override
Definition: CRUD_Events.h:88
ConnectResponse()
Definition: CRUD_Events.h:76
ACE_INET_Addr target
Definition: CRUD_Events.h:38
Definition: CRUD_Events.h:73
void serializeto(BitStream &bs) const override
Definition: CRUD_Events.h:108
Definition: EventProcessor.h:13
size_t GetReadableBits() const
Definition: BitStream.h:71
std::unique_ptr< CrudP_Packet > m_pkt
Definition: CRUD_Events.h:37
void serializefrom(BitStream &bs) override
Definition: CRUD_Events.h:118
Definition: CRUD_Events.h:54
DisconnectRequest()
Definition: CRUD_Events.h:87
EventProcessor * src() const
Definition: SEGSEvent.h:62
size_t size() const
Definition: CRUD_Events.h:40
Definition: CRUD_Events.h:114
#define EVENT_DECL(name, cnt)
Definition: SEGSEvent.h:19
Definition: CRUD_Events.h:131