#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/tcp.h>
#include "GameMessage.pb.h"
#define IMPORT_USER
#include "User.h"
#include "SQLTool.h"
#include <pthread.h>
#include <atomic>
using namespace std;
const int MAXPTHREADCOUNT = 4;
struct MessageNode
{
int iClient;
uint8_t iLen;
uint8_t iType;
uint8_t *PointerBuffer;
MessageNode()
{
iClient = 0;
iLen = 0;
iType = 0;
PointerBuffer = nullptr;
}
};
//消息队列
queue<MessageNode *> MessageQueue;
pthread_mutex_t mutex_MessageQueue;
//辅助消息队列,减少主线程与其他线程争夺消息队列的耗时
queue<MessageNode *> supMessageQueue; //辅助队列
pthread_mutex_t mutex_supMessageQueue;
const int MaxClient = 1001;
const int MaxEvents = 20000;
const int MaxMessageQueue = 100000;
int ClientCount = 0;
bool isBegin = false;
//客户端号与fd对应
std::unordered_map<int, int> FdToClientID;
int ClientIDToFd[MaxClient];
bool isClientLogin[MaxClient];
//主线程读取epoll有输入的缓冲区
uint8_t Buffer[MaxClient][200];
uint8_t Size[MaxClient];
uint8_t MessageType[MaxClient];
uint8_t MessageLen[MaxClient];
//原子类型的int,线程安全
std::atomic<int> NowLoginCount = 0;
//待发送的消息的队列
int iFrameCount = 0;
int iQueueLen = 0;
uint8_t *BufferFrameQueue[MaxMessageQueue];
uint8_t BufferEffectiveSize[MaxMessageQueue];
pthread_mutex_t mutex_BufferFrameQueue;
enum class MType
{
RegisteredRequest = 1,
RegisteredRespond,
LoginRequest,
LoginRespond,
GameBegin,
ActionRequest,
PlayerAction,
MoveRequest,
PlayerMove,
SetUserInfo,
SetUserInfoRespond,
GetUserInfo,
GetUserInfoRespond,
};
inline void Init()
{
InitUserInfo();
memset(Size, 0, sizeof(Size));
memset(MessageType, 0, sizeof(MessageType));
memset(MessageLen, 0, sizeof(MessageLen));
memset(isClientLogin, false, sizeof(isClientLogin));
memset(ClientIDToFd, -1, sizeof(ClientIDToFd));
iFrameCount = 0;
iQueueLen = 0;
pthread_mutex_init(&mutex_MessageQueue, NULL);
pthread_mutex_init(&mutex_BufferFrameQueue, NULL);
pthread_mutex_init(&mutex_supMessageQueue, NULL);
printf("Init Done\n");
}
inline void SendAll(uint8_t *netMessage, uint32_t SumLen)
{
for (int ID = 1; ID <= ClientCount; ID += 1)
{
if (ClientIDToFd[ID] < 0)
{
continue;
}
if (isClientLogin[ID]) // DANGER
{
send(ClientIDToFd[ID], netMessage, SumLen, 0);
}
}
}
inline void FrameQueue_SendAllMessages()
{
if (iQueueLen == 0)
{
//处理没有消息的时候
}
pthread_mutex_lock(&mutex_BufferFrameQueue);
for (int i = 0; i < iQueueLen; i++)
{
for (int ID = 1; ID <= ClientCount; ID += 1)
{
if (ClientIDToFd[ID] < 0)
{
continue;
}
if (isClientLogin[ID]) // DANGER
{
send(ClientIDToFd[ID], BufferFrameQueue[i], BufferEffectiveSize[i], 0);
}
}
if (BufferFrameQueue[i])
{
delete BufferFrameQueue[i];
BufferFrameQueue[i] = nullptr;
帧同步游戏服务器3
最新推荐文章于 2024-07-03 11:56:06 发布
本文深入解析了使用C++构建帧同步游戏服务器的技术细节,涵盖了帧同步的概念,如何处理网络延迟和不稳定性,以及在游戏场景中的应用。通过实例展示了如何设计并实现帧同步算法,确保游戏状态的一致性和公平性。
摘要由CSDN通过智能技术生成