This commit is contained in:
aixianling
2025-01-09 17:45:40 +08:00
commit 5c9f1dae4a
3482 changed files with 1146531 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
#include <stdlib.h>
#include <stdio.h>
#include "os_def.h"
#include "_ast.h"
#include "x_thread.h"
#include "x_tick.h"
#include "x_lock.h"
#include "container/queue_list.h"
#include "net/base_socket.h"
#include "share_util.h"
#include "memory/buffer_allocator.h"
#include "gate_proto.h"
#include "server_def.h"
#include "appitnmsg.h"
#include "data_packet.hpp"
#include "net/send_packet_pool.h"
#include "net/work_socket.h"
#include "net/net_session.h"
#include "net/server_socket.h"
#include "gate_user.h"
#include "gate_session.h"
#include "gate_manager.h"
GateManager::GateManager(const char* name) : Inherited(name)
{
ZeroMemory(gate_list_, sizeof(gate_list_));
}
GateManager::~GateManager()
{
}
bool GateManager::Startup()
{
for (int i = 0; i < MaxGateCount; ++i)
{
gate_list_[i]->gate_idx_ = i;
}
return Inherited::Startup();
}
NetSession* GateManager::NewSession(SOCKET s, sockaddr_in* addrIn)
{
for (int i = 0; i < MaxGateCount; ++i)
{
if (!gate_list_[i]->connected())
{
gate_list_[i]->SetClientSocket(s, addrIn);
gate_list_[i]->ClearSendBuffers();
OutputMsg(rmError, "new gate connect,idx=%d", gate_list_[i]->gate_idx_);
return gate_list_[i];
}
}
return NULL;
}
void GateManager::DestroySession(NetSession*)
{
}

View File

@@ -0,0 +1,60 @@
#ifndef _GATE_MANAGER_H_
#define _GATE_MANAGER_H_
class GateSession;
using namespace container;
class GateManager
: public ServerSocket
{
public:
typedef ServerSocket Inherited;
static const int MaxGateCount = 1;
public:
GateManager(const char* name);
~GateManager();
virtual void Initialize() = 0;
virtual void Uninitialize() = 0;
bool Startup();
inline GateSession* GetGate(int idx)
{
if (idx >= 0 && idx < (int)ArrayCount(gate_list_))
return gate_list_[idx];
return NULL;
}
inline GateUser* GetUser(NetId& netid)
{
GateSession* gate = GetGate(netid.gate_id_);
return gate ? gate->GetUser(netid) : NULL;
}
inline void PostCloseUser(NetId& netid, const char* reason, int p1 = 0, int p2 = 0)
{
OutputMsg(rmError, "GateManager::PostCloseUser %s:%d:%d", reason ? reason : "", p1, p2);
GateSession* gate = GetGate(netid.gate_id_);
if (gate)
{
gate->PostCloseUser(netid, 0, reason, p1, p2);
}
}
protected:
NetSession* NewSession(SOCKET socket, sockaddr_in* addrin);
void DestroySession(NetSession* client);
protected:
GateSession* gate_list_[MaxGateCount]; //Íø¹ØÁбí
};
#endif

View File

@@ -0,0 +1,28 @@
#ifndef _GATE_PROTO_H_
#define _GATE_PROTO_H_
/********************************************************
* 网关与内部服务器通信协议以及基础数据结构定义
**********************************************************/
#define GW_OPEN 1 //打开新用户会话,当客户端连接到网关时网关向服务器发送此消息
#define GW_CLOSE 2 //关闭用户会话,当客户端与网关的链接断开时网关向服务器发送此消息
//#define GW_CHECKSERVER 3 //网关回应服务器的心跳包消息
//#define GW_CHECKCLIENT 4 //服务器向网关发送心跳包消息
#define GW_DATA 5 //网关转发用户通信数据到服务器,服务器向网关发送用户通信数据也使用此消息
#define GW_CHANNEL 6 //同步频道的信息
#define GW_TEST 1026 // 用于测试网关和服务器之间的链路是否丢包
#define GW_CMD 1027
enum
{
ccAddUser = 1, // 增加用户
ccDelUser = 2, // 删除用户
ccBroadCast = 3, // 广播
};
#endif

View File

@@ -0,0 +1,207 @@
#include <stdio.h>
#include <stdlib.h>
#include "os_def.h"
#include "_ast.h"
#include "container/queue_list.h"
#include "x_tick.h"
#include "x_lock.h"
#include "x_thread.h"
#include "net/base_socket.h"
#include "server_def.h"
#include "gate_proto.h"
#include "share_util.h"
#include "memory/buffer_allocator.h"
#include "appitnmsg.h"
#include "data_packet.hpp"
#include "data_packet_reader.hpp"
#include "net/send_packet_pool.h"
#include "net/work_socket.h"
#include "net/net_session.h"
#include "net/server_socket.h"
#include "gate_user.h"
#include "gate_session.h"
#include "gate_manager.h"
GateSession::GateSession(const char* name) : Inherited(name)
{
gate_idx_ = 0;
}
GateSession::~GateSession()
{
CloseAllUser();
FreeBuffers();
}
void GateSession::FreeBuffers()
{
ClearSendList();
}
DataPacket& GateSession::AllocGateSendPacket(uint16_t cmd, NetId& netid)
{
DataPacket& packet = Inherited::allocProtoPacket(cmd);
packet << netid;
return packet;
}
DataPacket& GateSession::AllocGateSendPacket(NetId& netid)
{
DataPacket& packet = Inherited::allocProtoPacket(GW_DATA);
packet << netid;
return packet;
}
void GateSession::Disconnected()
{
Inherited::Disconnected();
FreeBuffers();
CloseAllUser();
}
void GateSession::OnRecvSysMsg(unsigned int msg, size_t p1, size_t p2, size_t p3, size_t p4)
{
switch (msg)
{
case CLOSE_GATE_SESSION:
// 这是由系统内部发出的关闭命令
NetId netid;
netid.socket_ = MAKEINT64(p1, p2);
netid.index_ = LOINT16(p3);
netid.gate_id_ = HIINT16(p3);
CloseUser(netid, "OnRecvSysMsg");
SendGateCloseUser(netid);
break;
}
}
void GateSession::OnRecv(const uint16_t cmd, char* buf, int size)
{
if (size < (int)sizeof(NetId) || buf == NULL) return;
NetId* netid = (NetId*)buf;
buf += sizeof(NetId);
size -= sizeof(NetId);
if (netid->index_ >= MAX_GATE_USER) return;
GateUser& user = user_list_[netid->index_];
netid->gate_id_ = (uint16_t)gate_idx_;
switch (cmd)
{
case GW_OPEN:
{
OpenNewUser(*netid, buf);
break;
}
case GW_CLOSE:
{
CloseUser(*netid, "GW_CLOSE");
break;
}
case GW_DATA:
{
if (!user.closed_ && user.netid_.socket_ == netid->socket_)
{
OnRecv(*netid, buf, size);
}
break;
}
case GW_TEST:
case GW_CMD:
{
// 原样数据返回
DataPacket& packet = AllocGateSendPacket(cmd, *netid);
packet.writeBuf(buf, size);
flushProtoPacket(packet);
break;
}
}
}
NetId* GateSession::OpenNewUser(NetId& netid, const char* addr)
{
if (netid.index_ >= MAX_GATE_USER) return NULL;
GateUser& user = user_list_[netid.index_];
user.netid_ = netid;
user.netid_.gate_id_ = (uint16_t)gate_idx_;
user.account_id_ = 0;
_STRNCPY_A(user.remote_addr_, addr);
user.closed_ = false;
user.account_name_[0] = 0;
user.gm_ = -1;
OnOpenUser(&user);
return &user.netid_;
}
void GateSession::OnOpenUser(GateUser*)
{
}
void GateSession::OnCloseUser(GateUser*, const char* reason)
{
}
bool GateSession::CloseUser(NetId& netid, const char* reason)
{
if (netid.index_ >= MAX_GATE_USER) return false;
if (netid.gate_id_ != gate_idx_) return false;
GateUser& user = user_list_[netid.index_];
if (user.closed_ == true) return true;
if (user.netid_.socket_ != netid.socket_) return false;
user.closed_ = true;
OnCloseUser(&user, reason);
return true;
}
void GateSession::CloseAllUser()
{
for (int i = 0; i < MAX_GATE_USER; i++)
{
GateUser& user = user_list_[i];
CloseUser(user.netid_, "CloseAllUser");
}
}
void GateSession::SendGateCloseUser(NetId& netid)
{
OutputMsg(rmNormal, ("close Socket=%d, GateSessionIdx=%d,reason=GW_CLOSE"), netid.socket_, netid.index_);
DataPacket& packet = AllocGateSendPacket(GW_CLOSE, netid);
flushProtoPacket(packet);
}
bool GateSession::OnValidateRegData(const SrvDef::PSERVER_REGDATA regData)
{
return (regData && regData->GameType == SrvDef::SERVER_REGDATA::GT_ID && regData->ServerType == SrvDef::GateServer);
}
GateUser* GateSession::GetUser(NetId& netid)
{
if (netid.index_ >= MAX_GATE_USER) return NULL;
if (netid.gate_id_ != gate_idx_) return NULL;
return &user_list_[netid.index_];
}

View File

@@ -0,0 +1,63 @@
#ifndef _GATE_CONNECTION_H_
#define _GATE_CONNECTION_H_
class GateManager;
class GateUser;
using namespace container;
class GateSession
: public NetSession
{
friend class GateManager;
public:
typedef NetSession Inherited;
public:
DataPacket& AllocGateSendPacket(uint16_t cmd, NetId& netid);
DataPacket& AllocGateSendPacket(NetId& netid);
inline void PostCloseUser(NetId& netid, int accountId, const char* reason, int p1 = 0, int p2 = 0)
{
OutputMsg(rmError, "GateSession::PostCloseUser %s:%d:%d", reason ? reason : "", p1, p2);
PostMsg(CLOSE_GATE_SESSION, LOINT32(netid.socket_), HIINT32(netid.socket_), MAKEINT32(netid.index_, netid.gate_id_), accountId);
}
GateUser* GetUser(NetId& netid);
protected:
virtual void OnOpenUser(GateUser* user);
virtual void OnCloseUser(GateUser* user, const char* reason);
virtual bool OnValidateRegData(const SrvDef::PSERVER_REGDATA regData);
protected:
void Disconnected();
void OnRecvSysMsg(unsigned int msg, size_t p1, size_t p2, size_t p3, size_t p4);
virtual void OnRecv(const uint16_t cmd, char* buf, int size);
NetId* OpenNewUser(NetId& netid, const char* addr);
bool CloseUser(NetId& netid, const char* reason);
void CloseAllUser();
virtual void OnRecv(NetId& netid, char* buf, int size) = 0;
void SendGateCloseUser(NetId& netid);
void FreeBuffers();
public:
GateSession(const char* name);
~GateSession();
protected:
static const int MAX_GATE_USER = 8192; //最大网关用户数
static const unsigned int CLOSE_GATE_SESSION = 102;
int gate_idx_; //网关编号
GateUser user_list_[MAX_GATE_USER];
};
#endif

View File

@@ -0,0 +1,26 @@
#ifndef _GATE_USER_H_
#define _GATE_USER_H_
class GateUser
{
public:
NetId netid_;
int account_id_; //网关用户的全局会话ID
ActorId actor_id_; // 选择的角色id在此之前是0
bool closed_; //是否标记为主动关闭
int64_t handle_;
int gm_; // gm等级
char remote_addr_[32]; //客户端地址
SrvDef::ACCOUNT account_name_; //帐号字符串
public:
GateUser(): account_id_(0), actor_id_(0), closed_(true), handle_(0), gm_(-1)
{
STATIC_ASSERT(sizeof(netid_.index_) == 2);
netid_.index_ = 0xffff;
remote_addr_[0] = 0;
account_name_[0] = 0;
}
};
#endif