TDME2  1.9.200
ApplicationServerClient.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
11 #include <tdme/utilities/Console.h>
13 
14 using std::endl;
15 using std::string;
16 using std::to_string;
17 using std::vector;
18 
25 
27 
28 ApplicationServerClient::ApplicationServerClient(const uint32_t clientId, const string& ip, const unsigned int port) :
29  UDPServerClient(clientId, ip, port), networkPacketsMutex("wsserverclient-networkpacketsmutex") {
30 }
31 
33  Console::println("ApplicationServerClient::~ApplicationServerClient()");
34 }
35 
36 void ApplicationServerClient::onRequest(const UDPPacket* packet, const uint32_t messageId, const uint8_t retries) {
37  // check if safe message
38  auto safe = packet->getBool();
39  if (safe == true && processSafeMessage(messageId) == false) {
40  return;
41  }
42  //
44  // while having datas
45  while (packet->getPosition() < UDPPacket::PACKET_MAX_SIZE) {
46  auto size = packet->getByte();
47  // end of
48  if (size == 0) break;
49  // game logic type id
50  auto logicTypeId = packet->getInt();
51  // create network packet
52  LogicNetworkPacket logicNetworkPacket(
53  messageId,
54  safe,
55  retries,
56  logicTypeId,
57  packet,
58  size
59  );
60  logicNetworkPacket.setSender(getKey());
61  networkPackets.push_back(logicNetworkPacket);
62  }
64 }
65 
67  Console::println("Initiated connection with '" + getIp() + ":" + to_string(getPort()) + "', sending key");
68 }
69 
71  Console::println("Closed connection with '" + getIp() + ":" + to_string(getPort()) + "'");
72 }
73 
74 void ApplicationServerClient::onCustom(const string& type) {
75  Console::println("Custom event '" + type + "' with '" + getIp() + ":" + to_string(getPort()) + "'");
76 }
77 
79  return networkPacketsMutex;
80 }
81 
82 vector<LogicNetworkPacket>& ApplicationServerClient::getNetworkPackets() {
83  return networkPackets;
84 }
virtual void onCustom(const string &type)
On custom.
void onRequest(const UDPPacket *packet, const uint32_t messageId, const uint8_t retries)
On request.
void setSender(const string &sender)
Set sender.
uint32_t getInt() const
Get a int from packet.
Definition: UDPPacket.h:159
uint16_t getPosition() const
Get position.
Definition: UDPPacket.h:53
bool getBool() const
Get a bool from packet.
Definition: UDPPacket.h:88
uint8_t getByte() const
Get a byte from packet.
Definition: UDPPacket.h:106
Base class for network UDP server clients.
const string & getKey() const
Client identification key.
const uint16_t getPort() const
returns client port
const string & getIp() const
returns client's ip
bool processSafeMessage(const uint32_t messageId)
Checks if message has already been processed and sends an acknowlegdement to client / safe client mes...
Base class for network UDP servers.
Definition: UDPServer.h:42
Mutex implementation.
Definition: Mutex.h:19
void unlock()
Unlocks this mutex.
Definition: Mutex.h:54
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Definition: Mutex.h:47
Console class.
Definition: Console.h:29
std::exception Exception
Exception base class.
Definition: Exception.h:18