1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-07 07:10:04 +02:00

intermediate connection

This commit is contained in:
nordsoft 2023-01-08 05:42:12 +04:00
parent 445f6c8598
commit d3710b82ad
6 changed files with 131 additions and 50 deletions

View File

@ -11,6 +11,7 @@
//
#include "StdInc.h"
#include <enet/enet.h>
#include <boost/program_options.hpp>
#include <vcmi/scripting/Service.h>
@ -250,6 +251,11 @@ int main(int argc, char * argv[])
*console->cb = processCommand;
console->start();
#endif
if(enet_initialize() != 0)
{
return EXIT_FAILURE;
}
const bfs::path logPath = VCMIDirs::get().userLogsPath() / "VCMI_Client_log.txt";
logConfig = new CBasicLogConfigurator(logPath, console);

View File

@ -55,6 +55,7 @@
#include "../lib/serializer/Cast.h"
#include <vcmi/events/EventBus.h>
#include <enet/enet.h>
#ifdef VCMI_WINDOWS
#include <windows.h>
@ -120,13 +121,16 @@ extern std::string NAME;
CServerHandler::CServerHandler()
: state(EClientState::NONE), mx(std::make_shared<boost::recursive_mutex>()), client(nullptr), loadMode(0), campaignStateToSend(nullptr), campaignServerRestartLock(false)
{
enetClient = enet_host_create(NULL, 8, 2, 0, 0);
enetClient = enet_host_create(NULL, 1, 2, 0, 0);
uuid = boost::uuids::to_string(boost::uuids::random_generator()());
//read from file to restore last session
if(!settings["server"]["uuid"].isNull() && !settings["server"]["uuid"].String().empty())
uuid = settings["server"]["uuid"].String();
applier = std::make_shared<CApplier<CBaseForLobbyApply>>();
registerTypesLobbyPacks(*applier);
threadPollClient = std::make_shared<boost::thread>(&CServerHandler::threadPoll, this);
threadPollClient->detach();
}
CServerHandler::~CServerHandler()
@ -175,6 +179,49 @@ void CServerHandler::resetStateForLobby(const StartInfo::EMode mode, const std::
#endif
}
void CServerHandler::threadPoll()
{
ENetEvent event;
while(true)
{
if(enet_host_service(enetClient, &event, 100) > 0)
{
switch(event.type)
{
case ENET_EVENT_TYPE_CONNECT: {
enet_packet_destroy(event.packet);
if(c && c->getPeer() == event.peer)
{
state = EClientState::CONNECTING;
}
break;
}
case ENET_EVENT_TYPE_RECEIVE: {
if(c && c->getPeer() == event.peer)
{
c->dispatch(event.packet);
}
else
{
enet_packet_destroy(event.packet);
}
break;
}
case ENET_EVENT_TYPE_DISCONNECT:
{
if(c && c->getPeer() == event.peer)
{
c.reset();
}
break;
}
}
}
}
}
void CServerHandler::startLocalServerAndConnect()
{
/*auto errorMsg = CGI->generaltexth->localizedTexts["server"]["errors"]["existingProcess"].String();
@ -272,7 +319,6 @@ void CServerHandler::startLocalServerAndConnect()
void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port)
{
state = EClientState::CONNECTING;
while(!c && state != EClientState::CONNECTION_CANCELLED)
{
try
@ -295,6 +341,11 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po
logNetwork->info("Connection aborted by player!");
return;
}
while(state != EClientState::CONNECTING)
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
c->init();
c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this);
@ -781,9 +832,11 @@ void CServerHandler::threadHandleConnection()
{
setThreadName("CServerHandler::threadHandleConnection");
c->enterLobbyConnectionMode();
try
{
while(!c->isOpen())
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
sendClientConnecting();
while(c->connected)
{

View File

@ -86,6 +86,7 @@ class CServerHandler : public IServerAPI, public LobbyInfo
void threadHandleConnection();
void threadRunServer();
void threadPoll();
void onServerFinished();
void sendLobbyPack(const CPackForLobby & pack) const override;
@ -103,6 +104,7 @@ public:
std::unique_ptr<CStopWatch> th;
std::shared_ptr<boost::thread> threadRunLocalServer;
std::shared_ptr<boost::thread> threadPollClient;
std::shared_ptr<CConnection> c;
CClient * client;

View File

@ -46,6 +46,8 @@ void CConnection::init()
std::string pom;
//we got connection
oser & std::string("Aiya!\n") & name & uuid & myEndianess; //identify ourselves
enet_host_flush(client);
iser & pom & pom & contactUuid & contactEndianess;
logNetwork->info("Established connection with %s. UUID: %s", pom, contactUuid);
mutexRead = std::make_shared<boost::mutex>();
@ -55,13 +57,13 @@ void CConnection::init()
}
CConnection::CConnection(ENetHost * _client, ENetPeer * _peer, std::string Name, std::string UUID)
: client(_client), peer(_peer), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0)
: client(_client), peer(_peer), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0), connected(false)
{
init();
//init();
}
CConnection::CConnection(ENetHost * _client, std::string host, ui16 port, std::string Name, std::string UUID)
: client(_client), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0)
: client(_client), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0), connected(false)
{
ENetAddress address;
enet_address_set_host(&address, host.c_str());
@ -73,7 +75,7 @@ CConnection::CConnection(ENetHost * _client, std::string host, ui16 port, std::s
throw std::runtime_error("Can't establish connection :(");
}
ENetEvent event;
/*ENetEvent event;
if(enet_host_service(client, &event, 10000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
{
logNetwork->info("Connection succeded");
@ -82,39 +84,47 @@ CConnection::CConnection(ENetHost * _client, std::string host, ui16 port, std::s
{
enet_peer_reset(peer);
throw std::runtime_error("Connection refused by server");
}
}*/
init();
//init();
}
void CConnection::dispatch(ENetPacket * packet)
{
packets.push_back(packet);
}
const ENetPeer * CConnection::getPeer() const
{
return peer;
}
int CConnection::write(const void * data, unsigned size)
{
ENetPacket * packet = enet_packet_create(data, size, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(peer, 0, packet);
enet_host_flush(client);
return size;
}
int CConnection::read(void * data, unsigned size)
{
ENetEvent event;
while(bufferSize < size)
{
if(enet_host_service(client, &event, 100) <= 0)
continue;
if(event.type == ENET_EVENT_TYPE_CONNECT)
throw std::runtime_error("Connectin event receieved while package expected");
if(event.type == ENET_EVENT_TYPE_RECEIVE)
if(packets.empty())
{
if(event.packet->dataLength > 0)
{
memcpy(buffer + bufferSize, event.packet->data, event.packet->dataLength);
bufferSize += event.packet->dataLength;
}
boost::this_thread::sleep(boost::posix_time::milliseconds(20));
continue;
}
enet_packet_destroy(event.packet);
auto * packet = packets.front();
packets.pop_front();
if(packet->dataLength > 0)
{
memcpy(buffer + bufferSize, packet->data, packet->dataLength);
bufferSize += packet->dataLength;
}
enet_packet_destroy(packet);
}
assert(bufferSize == size);
@ -122,7 +132,7 @@ int CConnection::read(void * data, unsigned size)
unsigned ret = std::min(size, bufferSize);
memcpy(data, buffer, ret);
if(ret < bufferSize)
memcpy(buffer, buffer + ret, bufferSize);
memcpy(buffer, buffer + ret, bufferSize - ret);
bufferSize -= ret;
return ret;
@ -134,6 +144,9 @@ CConnection::~CConnection()
if(handler)
handler->join();
for(auto * packet : packets)
enet_packet_destroy(packet);
close();
}
@ -149,22 +162,7 @@ CConnection & CConnection::operator&(const T &t) {
void CConnection::close()
{
ENetEvent event;
enet_peer_disconnect(peer, 0);
while(enet_host_service(client, & event, 100) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_RECEIVE:
enet_packet_destroy(event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
return;
}
}
/* We've arrived here, so the disconnect attempt didn't */
/* succeed yet. Force the connection down. */
enet_peer_reset(peer);
}
@ -205,6 +203,7 @@ void CConnection::sendPack(const CPack * pack)
boost::unique_lock<boost::mutex> lock(*mutexWrite);
logNetwork->trace("Sending a pack of type %s", typeid(*pack).name());
oser & pack;
enet_host_flush(client);
}
void CConnection::disableStackSendingByID()

View File

@ -23,12 +23,12 @@ struct CPack;
class DLL_LINKAGE CConnection
: public IBinaryReader, public IBinaryWriter, public std::enable_shared_from_this<CConnection>
{
void init();
void reportState(vstd::CLoggerBase * out) override;
int write(const void * data, unsigned size) override;
int read(void * data, unsigned size) override;
std::list<ENetPacket*> packets;
ENetPeer * peer = nullptr;
ENetHost * client = nullptr;
char * buffer;
@ -51,6 +51,9 @@ public:
CConnection(ENetHost * client, ENetPeer * peer, std::string Name, std::string UUID);
CConnection(ENetHost * client, std::string host, ui16 port, std::string Name, std::string UUID);
void init(); //must be called from outside after connection message received
void dispatch(ENetPacket * packet);
const ENetPeer * getPeer() const;
void close();
bool isOpen() const;

View File

@ -174,10 +174,10 @@ void CVCMIServer::run()
if(!lobbyConnectionsThread)
lobbyConnectionsThread = vstd::make_unique<boost::thread>(&CVCMIServer::startAsyncAccept, this);
if(!remoteConnectionsThread && cmdLineOptions.count("lobby"))
/*if(!remoteConnectionsThread && cmdLineOptions.count("lobby"))
{
remoteConnectionsThread = vstd::make_unique<boost::thread>(&CVCMIServer::establishRemoteConnections, this);
}
}*/
#if defined(VCMI_ANDROID)
CAndroidVMHelper vmHelper;
@ -322,27 +322,45 @@ void CVCMIServer::startGameImmidiately()
void CVCMIServer::startAsyncAccept()
{
ENetEvent event;
ENetHost * client = enet_host_create(NULL, 8, 2, 0, 0);
while(true)
{
if(enet_host_service(server, &event, 1000) > 0)
if(enet_host_service(server, &event, 100) > 0)
{
switch(event.type)
{
case ENET_EVENT_TYPE_CONNECT: {
auto c = std::make_shared<CConnection>(server, event.peer, SERVER_NAME, uuid);
connections.insert(c);
c->handler = std::make_shared<boost::thread>(&CVCMIServer::threadHandleClient, this, c);
if(state == EServerState::LOBBY)
{
auto c = std::make_shared<CConnection>(server, event.peer, SERVER_NAME, uuid);
c->init();
connections.insert(c);
c->handler = std::make_shared<boost::thread>(&CVCMIServer::threadHandleClient, this, c);
}
enet_packet_destroy(event.packet);
break;
}
case ENET_EVENT_TYPE_RECEIVE:
case ENET_EVENT_TYPE_RECEIVE: {
bool receiverFound = false;
for(auto & c : connections)
{
if(c->getPeer() == event.peer)
{
c->dispatch(event.packet);
receiverFound = true;
break;
}
}
if(!receiverFound)
enet_packet_destroy(event.packet);
break;
}
}
}
}
enet_host_destroy(client);
}
/*void CVCMIServer::connectionAccepted(const boost::system::error_code & ec)