5 #include <unordered_map>
16 using std::unordered_map;
26 UDPServerClient::UDPServerClient(
const uint32_t clientId,
const string& ip,
const uint16_t port) :
32 shutdownRequested(false),
33 messageMapSafeMutex(
"nioudpserverclient_messagemapsafe") {
39 for (
const auto& [messageId, message]:
messageMapSafe)
delete message;
68 "UDPServerClient::send(): send failed for client '" +
71 RTTI::demangle(
typeid(exception).name()) +
73 string(exception.what())
79 bool messageProcessed =
false;
88 messageProcessed =
true;
89 auto message = it->second;
90 message->receptions++;
94 message->messageId = messageId;
95 message->receptions = 1;
96 message->time = Time::getCurrentMillis();
114 "UDPServerClient::processSafeMessage(): send failed for client '" +
117 RTTI::demangle(
typeid(exception).name()) +
119 string(exception.what())
124 return messageProcessed ==
true?
false:
true;
130 packet->putString(
key);
138 "UDPServerClient::sendConnected(): send failed for client '" +
141 RTTI::demangle(
typeid(exception).name()) +
143 string(exception.what())
165 Console::println(
"UDPServerClient::onPacketReceived(): client request declined from '" + (
ip) +
"'. Shutting down client");
234 auto now = Time::getCurrentMillis();
237 auto message = it->second;
251 if (retries == 0)
return 0L;
Base exception class for network server exceptions.
static STATIC_DLL_IMPEXT const char * KEY_PREFIX_UNNAMED
static const uint32_t MESSAGE_ID_UNSUPPORTED
static const uint8_t MESSAGE_RETRIES_NONE
@ REQUESTTYPE_CLIENT_INIT
@ REQUESTTYPE_CLIENT_CUSTOM
@ REQUESTTYPE_CLIENT_CLOSE
@ REQUESTTYPE_CLIENT_REQUEST
static const string EVENT_CUSTOM_NONE
bool setClientKey(CLIENT *client, const string &clientKey)
sets a client identification key
void closeClient(CLIENT *client)
closes a client connection
Base class for network UDP server clients.
void cleanUpSafeMessages()
Clean up safe messages.
void init()
initiates this network client
void send(UDPPacket *packet, bool safe=true, bool deleteFrame=true)
Sends a frame to client, takes over ownership of frame.
MessageMapSafe messageMapSafe
void fireEvent(const string &type)
fires an custom event
void close()
Shuts down this network client.
static const uint64_t MESSAGESSAFE_KEEPTIME
uint64_t getRetryTime(const uint8_t retries)
void shutdown()
Shuts down this network client.
virtual void onPacketReceived(const UDPPacket *packet, const uint32_t messageId=0, const uint8_t retries=0)
Event, which will be called if packet has been received, defaults to worker thread pool.
const bool setKey(const string &key)
sets the clients identification key
static UDPPacket * createPacket()
Creates a packet to be used with send.
virtual ~UDPServerClient()
public destructor, should only be called implicitly by Reference::releaseReference()
Mutex messageMapSafeMutex
void sendConnected()
Sends an connect message to client.
volatile bool shutdownRequested
bool processSafeMessage(const uint32_t messageId)
Checks if message has already been processed and sends an acknowlegdement to client / safe client mes...
static const int MESSAGEACK_RESENDTIMES_TRIES
STATIC_DLL_IMPEXT static const uint64_t MESSAGEACK_RESENDTIMES[MESSAGEACK_RESENDTIMES_TRIES]
unique_ptr< ServerWorkerThreadPool > workerThreadPool
static void initializeHeader(UDPPacket *packet)
Writes a empty header to packet.
@ MESSAGETYPE_ACKNOWLEDGEMENT
void sendMessage(const UDPServerClient *client, UDPPacket *packet, const bool safe, const bool deleteFrame, const MessageType messageType, const uint32_t messageId=MESSAGE_ID_NONE)
pushes a message to be send, takes over ownership of frame
void unlock()
Unlocks this mutex.
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Run time type information utility class.
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
virtual void acquireReference()
Acquires a reference, incrementing the counter.