mirror of
https://github.com/Fluorohydride/ygopro.git
synced 2026-10-28 21:08:53 +08:00
* Use size_t for length in HandleCTOSPacket Co-authored-by: Copilot <copilot@github.com> * Use uint32_t in deck loading functions Co-authored-by: Copilot <copilot@github.com> * fix Co-authored-by: Copilot <copilot@github.com> * Validate deck update length in SingleDuel and TagDuel Co-authored-by: Copilot <copilot@github.com> * revert --------- Co-authored-by: Copilot <copilot@github.com>
68 lines
2.2 KiB
C++
68 lines
2.2 KiB
C++
#ifndef NETSERVER_H
|
|
#define NETSERVER_H
|
|
|
|
#include "bufferio.h"
|
|
#include "network.h"
|
|
|
|
namespace ygo {
|
|
|
|
class NetServer {
|
|
private:
|
|
static unsigned char net_server_write[SIZE_NETWORK_BUFFER];
|
|
static size_t last_sent;
|
|
|
|
public:
|
|
static bool StartServer(unsigned short port);
|
|
static bool StartBroadcast();
|
|
static void StopServer();
|
|
static void StopBroadcast();
|
|
static void StopListen();
|
|
static void BroadcastEvent(evutil_socket_t fd, short events, void* arg);
|
|
static void ServerAccept(evconnlistener* listener, evutil_socket_t fd, sockaddr* address, int socklen, void* ctx);
|
|
static void ServerAcceptError(evconnlistener *listener, void* ctx);
|
|
static void ServerEchoRead(bufferevent* bev, void* ctx);
|
|
static void ServerEchoEvent(bufferevent* bev, short events, void* ctx);
|
|
static int ServerThread();
|
|
static void DisconnectPlayer(DuelPlayer* dp);
|
|
static void HandleCTOSPacket(DuelPlayer* dp, unsigned char* data, size_t len);
|
|
static size_t CreateChatPacket(unsigned char* src, int src_size, unsigned char* dst, uint16_t dst_player_type);
|
|
static void SendPacketToPlayer(DuelPlayer* dp, unsigned char proto) {
|
|
auto p = net_server_write;
|
|
BufferIO::Write<uint16_t>(p, 1);
|
|
BufferIO::Write<uint8_t>(p, proto);
|
|
last_sent = 3;
|
|
if (dp)
|
|
bufferevent_write(dp->bev, net_server_write, 3);
|
|
}
|
|
template<typename ST>
|
|
static void SendPacketToPlayer(DuelPlayer* dp, unsigned char proto, const ST& st) {
|
|
auto p = net_server_write;
|
|
static_assert(sizeof(ST) <= MAX_DATA_SIZE, "Packet size is too large.");
|
|
BufferIO::Write<uint16_t>(p, (uint16_t)(1 + sizeof(ST)));
|
|
BufferIO::Write<uint8_t>(p, proto);
|
|
std::memcpy(p, &st, sizeof(ST));
|
|
last_sent = sizeof(ST) + 3;
|
|
if (dp)
|
|
bufferevent_write(dp->bev, net_server_write, sizeof(ST) + 3);
|
|
}
|
|
static void SendBufferToPlayer(DuelPlayer* dp, unsigned char proto, void* buffer, size_t len) {
|
|
auto p = net_server_write;
|
|
if (len > MAX_DATA_SIZE)
|
|
len = MAX_DATA_SIZE;
|
|
BufferIO::Write<uint16_t>(p, (uint16_t)(1 + len));
|
|
BufferIO::Write<uint8_t>(p, proto);
|
|
std::memcpy(p, buffer, len);
|
|
last_sent = len + 3;
|
|
if (dp)
|
|
bufferevent_write(dp->bev, net_server_write, len + 3);
|
|
}
|
|
static void ReSendToPlayer(DuelPlayer* dp) {
|
|
if(dp)
|
|
bufferevent_write(dp->bev, net_server_write, last_sent);
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
#endif //NETSERVER_H
|