Skip to content

Commit

Permalink
Merge pull request #54 from pwnsky/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
i0gan authored Jul 28, 2024
2 parents 42d75ba + 6fdeae2 commit a5506d8
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 50 deletions.
2 changes: 2 additions & 0 deletions script/show_proc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/sh
ps aux | grep "./squick type"
4 changes: 2 additions & 2 deletions script/start.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ start cmd /c " squick type=backstage id=10 area=0 ip=127.0.0.1 port=10010 http_p
start cmd /c " squick type=world id=100 area=0 ip=127.0.0.1 port=10101 master_ip=127.0.0.1 master_port=10001"
start cmd /c " squick type=db_proxy id=300 area=0 ip=127.0.0.1 port=10201 master_ip=127.0.0.1 master_port=10001"
start cmd /c " squick type=web id=2 area=0 ip=127.0.0.1 port=10301 web_port=8088 master_ip=127.0.0.1 master_port=10001"
start cmd /c " squick type=lobby id=1000 area=0 ip=127.0.0.1 port=10401 master_ip=127.0.0.1 master_port=10001"
start cmd /c " squick type=lobby id=1001 area=0 ip=127.0.0.1 port=10402 master_ip=127.0.0.1 master_port=10001"
start cmd /c " squick type=player id=1000 area=0 ip=127.0.0.1 port=10401 master_ip=127.0.0.1 master_port=10001"
start cmd /c " squick type=player id=1001 area=0 ip=127.0.0.1 port=10402 master_ip=127.0.0.1 master_port=10001"
start cmd /c " squick type=proxy id=500 area=0 ip=127.0.0.1 port=10501 ws_port=10502 master_ip=127.0.0.1 master_port=10001"
17 changes: 9 additions & 8 deletions script/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
# Github: https://github.com/pwnsky/squick
# Description: Start all nodes
cd $(dirname $0)
log_path="../data/logs"

./squick.sh type=master id=1 area=0 ip=127.0.0.1 port=10001 http_port=50000 logshow=0 &
./squick.sh type=master id=1 area=0 ip=127.0.0.1 port=10001 http_port=50000 logshow=0 >> $log_path/master_run.log &
sleep 0.5
./squick.sh type=backstage id=10 area=0 ip=127.0.0.1 port=10010 http_port=8888 master_ip=127.0.0.1 master_port=10001 logshow=0 &
./squick.sh type=backstage id=10 area=0 ip=127.0.0.1 port=10010 http_port=8888 master_ip=127.0.0.1 master_port=10001 logshow=0 >> $log_path/backstage_run.log &
sleep 0.5
./squick.sh type=world id=100 area=0 ip=127.0.0.1 port=10101 master_ip=127.0.0.1 master_port=10001 logshow=0 &
./squick.sh type=world id=100 area=0 ip=127.0.0.1 port=10101 master_ip=127.0.0.1 master_port=10001 logshow=0 >> $log_path/world_run.log &
sleep 0.5
./squick.sh type=db_proxy id=300 area=0 ip=127.0.0.1 port=10201 master_ip=127.0.0.1 master_port=10001 logshow=0 &
./squick.sh type=db_proxy id=300 area=0 ip=127.0.0.1 port=10201 master_ip=127.0.0.1 master_port=10001 logshow=0 >> $log_path/db_proxy_run.log &
sleep 0.5
./squick.sh type=web id=2 area=0 ip=127.0.0.1 port=10301 http_port=8088 master_ip=127.0.0.1 master_port=10001 logshow=0 &
./squick.sh type=web id=2 area=0 ip=127.0.0.1 port=10301 http_port=8088 master_ip=127.0.0.1 master_port=10001 logshow=0 >> $log_path/web_run.log &
sleep 0.5
./squick.sh type=lobby id=1000 area=0 ip=127.0.0.1 port=10401 master_ip=127.0.0.1 master_port=10001 logshow=0 &
./squick.sh type=player id=1000 area=0 ip=127.0.0.1 port=10401 master_ip=127.0.0.1 master_port=10001 logshow=0 >> $log_path/player_run.log &
sleep 0.5
./squick.sh type=lobby id=1001 area=0 ip=127.0.0.1 port=10402 master_ip=127.0.0.1 master_port=10001 logshow=0 &
./squick.sh type=player id=1001 area=0 ip=127.0.0.1 port=10402 master_ip=127.0.0.1 master_port=10001 logshow=0 >> $log_path/player_run.log &
sleep 0.5
./squick.sh type=proxy id=500 area=0 ip=127.0.0.1 port=10501 ws_port=10502 master_ip=127.0.0.1 master_port=10001 logshow=0 &
./squick.sh type=proxy id=500 area=0 ip=127.0.0.1 port=10501 ws_port=10502 master_ip=127.0.0.1 master_port=10001 logshow=0 >> $log_path/proxy_run.log &
2 changes: 1 addition & 1 deletion src/node/global/logic/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ void Plugin::Install() { REGISTER_MODULE(pm_, ILogicModule, LogicModule) }

void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, ILogicModule, LogicModule) }

} // namespace global::logic
} // namespace global::logic
53 changes: 26 additions & 27 deletions src/node/proxy/logic/logic_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ void LogicModule::OnAckPlayerEnter(const socket_t sock, const int msg_id, const
return;
}

// start heatbeat
// m_schedule_->AddSchedule(s.account_id, "HeatbeatCheck", this, &LogicModule::OnHeatbeatCheck, 10.0f, 99999); // 每10秒check一次

uid = ack.data().uid();
socket_t player_sock = ack.proxy_sock();

Expand All @@ -122,7 +119,7 @@ void LogicModule::OnAckPlayerEnter(const socket_t sock, const int msg_id, const
return;
}

// 在这里创建玩家表
// Create the player info
players_socks_[uid] = player_sock;
pInfo->uid = uid;
pInfo->status = PlayerOnline;
Expand All @@ -135,6 +132,9 @@ void LogicModule::OnAckPlayerEnter(const socket_t sock, const int msg_id, const
} else if (pInfo->protocol_type == ProtocolType::WS) {
m_ws_->SendPBMsg(rpc::IdAckPlayerEnter, ack, player_sock);
}

// Start heatbeat check
m_schedule_->AddSchedule(Guid(0, uid), "HeatbeatCheck", this, &LogicModule::OnHeatbeatCheck, 10.0f, 99999); // 每10秒check一次
}

int LogicModule::GetLoadBanlanceNode(int type) { return m_net_client_->GetRandomNodeID(type); }
Expand All @@ -149,19 +149,26 @@ void LogicModule::OnClientDisconnected(const socket_t sock) {
sessions_.erase(iter);
}

// remove the tcp
NetObject *net_obj = m_net_->GetNet()->GetNetObject(sock);
if (net_obj) {
m_net_->GetNet()->CloseNetObject(sock);
}

// remove the ws
NetObject *ws_net_obj = m_ws_->GetNet()->GetNetObject(sock);
if (ws_net_obj) {
m_ws_->GetNet()->CloseNetObject(sock);
}

auto pInfo = GetPlayerConnInfo(sock);
if (pInfo == nullptr) {
return;
}

// 判断是否有新的连接
// 移除schedule
// m_schedule_->RemoveSchedule(iter->second.account_id);
m_schedule_->RemoveSchedule(Guid(0, pInfo->uid));
if (pInfo->player_node > 0) {
// when a net-object bind a account then tell that game-server
rpc::NNtfPlayerOffline ntf;
Expand All @@ -180,14 +187,13 @@ void LogicModule::OnRecivedPlayerNodeMsg(const socket_t sock, const int msg_id,
return;
}
SendToPlayer(msg_pak.uid(), msg_id, msg_pak.msg_data());
return;
}

bool LogicModule::SendToPlayer(uint64_t uid, const int msg_id, const string &data) {

bool ret = false;
try {
auto pInfo = GetPlayerConnInfo(uid);
auto pInfo = GetPlayerConnInfoByUID(uid);
if (pInfo == nullptr) {
throw;
}
Expand All @@ -210,21 +216,15 @@ void LogicModule::OnOtherMessage(const socket_t sock, const int msg_id, const ch
LOG_ERROR("OnOtherMessage get player connect info is null, sock<%v> msg_id<%v>", sock, msg_id);
return;
}
rpc::MsgBase xMsg;
xMsg.set_uid(pInfo->uid);
*xMsg.mutable_msg_data() = string(msg, len);
std::string pak;
if (!xMsg.SerializeToString(&pak)) {
return;
}

// 根据ID 来转发至不同服务器
if (msg_id >= 12000 && msg_id < 30000) {
// 转发到Player node
int node_player_id = pInfo->player_node;
if (node_player_id <= 0) {
return;
}
m_net_client_->SendByID(node_player_id, msg_id, pak);
m_net_client_->SendByID(node_player_id, msg_id, std::string(msg, len), pInfo->uid);
} else if (msg_id >= 10000 && msg_id < 32000) {

} else if (msg_id >= 30000 && msg_id < 32000) {
Expand All @@ -243,7 +243,7 @@ void LogicModule::OnHeartbeat(const socket_t sock, const int msg_id, const char
}

std::string msgData(msg, len);
pInfo->last_ping = SquickGetTimeMS();
pInfo->last_ping = SquickGetTimeS();
if (pInfo->protocol_type == ProtocolType::Tcp) {
m_net_->SendMsg(rpc::IdAckHeartBeat, msgData, sock);
} else if (pInfo->protocol_type == ProtocolType::WS) {
Expand Down Expand Up @@ -272,18 +272,17 @@ void LogicModule::OnReqTestProxy(const socket_t sock, const int msg_id, const ch
}

int LogicModule::OnHeatbeatCheck(const Guid &self, const std::string &heartBeat, const float time, const int count) {
/*
auto iter = players_.find(self.ToString());
if (iter == players_.end()) {
dout << "No this player to heatbeat check\n";
auto player_info = GetPlayerConnInfoByUID(self.GetData());
if (player_info == nullptr) {
LOG_ERROR("No this player %v to heatbeat check", self.GetData());
return 1;
}
// dout << "heatbeatcheck : " << self.ToString() << " " << iter->second.last_ping << " now " << SquickGetTimeMS() << std::endl;
time_t now = SquickGetTimeMS();
if (now - iter->second.last_ping > 30000) { // 大于30秒即断线
OnClientDisconnected(iter->second.sock);
time_t now = SquickGetTimeS();
if (now - player_info->last_ping > HEATBEAT_TIMEOUT) {
LOG_INFO("OnHeatbeatCheck Player %v has timeout, last ping time %v, current time: %v , can not above than: %v", player_info->uid, player_info->last_ping, now, HEATBEAT_TIMEOUT);
OnClientDisconnected(player_info->sock);
return 2;
}
return 0;*/
return 0;
}

Expand Down Expand Up @@ -353,7 +352,7 @@ void LogicModule::OnReqConnect(ProtocolType type, const socket_t sock, const int
Session s;
s.account_id = req.account_id();
s.key = req.key();
s.time = SquickGetTimeMS();
s.time = SquickGetTimeS();
s.sock = sock;
s.ip = pNetObject->GetIP();
s.protocol_type = type;
Expand Down Expand Up @@ -416,7 +415,7 @@ void LogicModule::OnNAckConnectVerify(const socket_t sock, const int msg_id, con

auto &client = players_[s.sock];
client.sock = s.sock;
client.last_ping = SquickGetTimeMSEx();
client.last_ping = SquickGetTimeS();
client.account_id = s.account_id;
client.account = data.account();
client.login_node = s.login_node;
Expand Down
4 changes: 3 additions & 1 deletion src/node/proxy/logic/logic_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "i_logic_module.h"
#include <vector>

namespace proxy::logic {
#define HEATBEAT_TIMEOUT 30 // seconds
enum class ProtocolType {
Tcp = 1,
WS = 2,
Expand Down Expand Up @@ -45,7 +48,6 @@ struct Session {
int login_node;
};

namespace proxy::logic {
class LogicModule : public ILogicModule {
public:
LogicModule(IPluginManager *p) {
Expand Down
2 changes: 1 addition & 1 deletion src/proto/n_game_mgr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ message ReqGameplayPrepared {
bytes name = 7; // Game Play 服务器名称
}

message AckGameplayPrepared { int32 code = 1; }
message AckGameplayPrepared { int32 code = 1; }
16 changes: 16 additions & 0 deletions src/pycli/batch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#! /bin/bash

nums=1000
sleep_time=0.01
kill_time=20
if [ "$1" != "" ];then
nums=$1
fi

for ((i=1; i<=$nums; i++))
do
timeout --foreground $kill_time python main.py >> batch_run.log &
sleep $sleep_time
done

echo "Run benchmark with $nums process"
9 changes: 8 additions & 1 deletion src/pycli/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ def HandleAckPlayerEnter(msg_id, msg):
print("Error")

def HandleAckPlayerData(msg_id, msg):
print("okkkk")
ack = AckPlayerData()
ack.ParseFromString(msg)
data = MessageToJson(ack)
print("Get the player data: \n", data)


def HandleOffline(msg_id, msg):
print("Player offline")
9 changes: 2 additions & 7 deletions src/pycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
import _thread
import time
import rel
import sys
from protocol import *
from logic import *
# pip install websocket-client rel
# pip install requests
# pip install http
# pip install protobuf

print("pycli:")

Instance = {}

Expand Down Expand Up @@ -102,4 +97,4 @@ def OnWsOpen(ws):

rel.signal(2, rel.abort) # Keyboard Interrupt
rel.dispatch()
print("run")
print("run")
3 changes: 2 additions & 1 deletion src/pycli/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from game_pb2 import *
from base_pb2 import *
from msg_id_pb2 import *
from google.protobuf.json_format import MessageToJson


print("sys", sys.path)
Expand All @@ -23,4 +24,4 @@ def Encode(msg_id, data):
def Decode(recv_data):
msg_id = int.from_bytes(recv_data[0:2], byteorder='big', signed=False)
length = int.from_bytes(recv_data[2:6], byteorder='big', signed=False)
return msg_id, recv_data[6:]
return msg_id, recv_data[6:]
5 changes: 5 additions & 0 deletions src/pycli/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
websocket-client
rel
requests
http
protobuf
2 changes: 1 addition & 1 deletion tools/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function check_err()
errno=$?
if [[ $errno != 0 ]];then
log_error "Has terminated process, The error number: $errno"
exit
exit $errno
else
log_info "No error"
fi
Expand Down

0 comments on commit a5506d8

Please sign in to comment.