186 lines
5.9 KiB
C++
186 lines
5.9 KiB
C++
#include "precompile.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <a8/tcpclient.h>
|
|
#include <a8/tcpclient2.h>
|
|
#include <a8/timer.h>
|
|
|
|
#include "virtualclient.h"
|
|
#include "app.h"
|
|
#include "ss_proto.pb.h"
|
|
#include "cs_proto.pb.h"
|
|
|
|
const int PACK_MAX = 1024 * 64;
|
|
|
|
void VirtualClient::Init()
|
|
{
|
|
recv_bufflen_ = 0;
|
|
recv_buff_ = (char*) malloc(PACK_MAX * 2);
|
|
|
|
#if TCP_CLIENT2
|
|
tcp_client_ = new a8::TcpClient2();
|
|
#else
|
|
tcp_client_ = new a8::TcpClient();
|
|
#endif
|
|
tcp_client_->remote_address = remote_ip;
|
|
tcp_client_->remote_port = remote_port;
|
|
tcp_client_->on_error = std::bind(&VirtualClient::on_error, this, std::placeholders::_1, std::placeholders::_2);
|
|
tcp_client_->on_connect = std::bind(&VirtualClient::on_connect, this, std::placeholders::_1);
|
|
tcp_client_->on_disconnect = std::bind(&VirtualClient::on_disconnect, this, std::placeholders::_1);
|
|
tcp_client_->on_socketread = std::bind(&VirtualClient::on_socketread, this ,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
|
|
tcp_client_->Open();
|
|
}
|
|
|
|
void VirtualClient::UnInit()
|
|
{
|
|
delete tcp_client_;
|
|
tcp_client_ = nullptr;
|
|
}
|
|
|
|
void VirtualClient::Update()
|
|
{
|
|
if (state_ == VCS_Joined) {
|
|
long long tick = a8::XGetTickCount() - last_active_tick_;
|
|
if (tick > 1200) {
|
|
// abort();
|
|
}
|
|
if (a8::XGetTickCount() - last_move_tick_ > 100) {
|
|
last_move_tick_ = a8::XGetTickCount();
|
|
SendMove();
|
|
}
|
|
if (a8::XGetTickCount() - last_move_dir_chg_tick_ > 2000 + 1000 * (rand() % 5)) {
|
|
last_move_dir_chg_tick_ = a8::XGetTickCount();
|
|
move_x = rand() % 1000;
|
|
move_y = rand() % 1000;
|
|
if (rand() % 5 == 0) {
|
|
move_x = -move_x;
|
|
move_y = -move_y;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void VirtualClient::SendJoin()
|
|
{
|
|
cs::CMJoin msg;
|
|
msg.set_server_id(2);
|
|
msg.set_account_id(account);
|
|
msg.set_baseskin(14001);
|
|
SendMsg(msg);
|
|
a8::XPrintf("sendjoin %s\n", {account});
|
|
a8::Timer::Instance()->AddDeadLineTimerAndAttach(
|
|
1000 * 30 + (rand() % 15) * 1000,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
VirtualClient* client = (VirtualClient*)param.sender.GetUserData();
|
|
client->jumped_ = true;
|
|
},
|
|
&timer_attacher_.timer_list_
|
|
);
|
|
}
|
|
|
|
void VirtualClient::SendMove()
|
|
{
|
|
cs::CMMove msg;
|
|
if (move_x != 0 && move_y != 0) {
|
|
auto p = msg.mutable_move_dir();
|
|
p->set_x(move_x);
|
|
p->set_y(move_y);
|
|
}
|
|
msg.set_shot_start(true);
|
|
msg.set_shot_hold(true);
|
|
msg.set_jump(jumped_);
|
|
SendMsg(msg);
|
|
}
|
|
|
|
#if TCP_CLIENT2
|
|
void VirtualClient::on_error(a8::TcpClient2* sender, int errorId)
|
|
#else
|
|
void VirtualClient::on_error(a8::TcpClient* sender, int errorId)
|
|
#endif
|
|
{
|
|
abort();
|
|
a8::UdpLog::Instance()->Error("VirtualClient errorid=%d", {errorId});
|
|
}
|
|
|
|
#if TCP_CLIENT2
|
|
void VirtualClient::on_connect(a8::TcpClient2* sender)
|
|
#else
|
|
void VirtualClient::on_connect(a8::TcpClient* sender)
|
|
#endif
|
|
{
|
|
recv_bufflen_ = 0;
|
|
a8::UdpLog::Instance()->Info("target server connected", {});
|
|
App::Instance()->AddIMMsg(IM_VirtualClientConnect,
|
|
a8::XParams()
|
|
.SetSender(instance_id)
|
|
);
|
|
}
|
|
|
|
#if TCP_CLIENT2
|
|
void VirtualClient::on_disconnect(a8::TcpClient2* sender)
|
|
#else
|
|
void VirtualClient::on_disconnect(a8::TcpClient* sender)
|
|
#endif
|
|
{
|
|
abort();
|
|
recv_bufflen_ = 0;
|
|
a8::UdpLog::Instance()->Info("target server %d disconnected after 10s later reconnect", {instance_id});
|
|
App::Instance()->AddIMMsg(IM_VirtualClientDisconnect,
|
|
a8::XParams()
|
|
.SetSender(instance_id)
|
|
);
|
|
}
|
|
|
|
#if TCP_CLIENT2
|
|
void VirtualClient::on_socketread(a8::TcpClient2* sender, char* buf, unsigned int len)
|
|
#else
|
|
void VirtualClient::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len)
|
|
#endif
|
|
{
|
|
if (recv_bufflen_ + len > 2 * PACK_MAX) {
|
|
recv_bufflen_ = 0;
|
|
a8::UdpLog::Instance()->Debug("recvied target server too long message", {});
|
|
return;
|
|
} else {
|
|
memmove(&recv_buff_[recv_bufflen_], buf, len);
|
|
recv_bufflen_ += len;
|
|
}
|
|
|
|
bool warning = false;
|
|
unsigned int offset = 0;
|
|
while (recv_bufflen_ - offset >= sizeof(f8::PackHead)) {
|
|
f8::PackHead* p = (f8::PackHead*) &recv_buff_[offset];
|
|
if (p->magic_code == f8::MAGIC_CODE) {
|
|
if (recv_bufflen_ - offset < sizeof(f8::PackHead) + p->packlen) {
|
|
break;
|
|
}
|
|
App::Instance()->AddSocketMsg(SF_VirtualClient,
|
|
instance_id,
|
|
0,
|
|
p->msgid,
|
|
p->seqid,
|
|
&recv_buff_[offset + sizeof(f8::PackHead)],
|
|
p->packlen);
|
|
offset += sizeof(f8::PackHead) + p->packlen;
|
|
} else {
|
|
warning = true;
|
|
offset++;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (warning) {
|
|
a8::UdpLog::Instance()->Debug("recvied bad package", {});
|
|
}
|
|
if (offset > 0 && offset < recv_bufflen_) {
|
|
memmove(recv_buff_, recv_buff_ + offset, recv_bufflen_ - offset);
|
|
}
|
|
recv_bufflen_ -= offset;
|
|
state_ = VCS_Joined;
|
|
last_active_tick_ = a8::XGetTickCount();
|
|
}
|