Browse Source

Some fixes for network

pull/25/head
Xadhoom 7 years ago
parent
commit
aff8769a97
  1. 9
      SourceX/dvlnet/base.cpp
  2. 2
      SourceX/dvlnet/loopback.cpp
  3. 2
      SourceX/dvlnet/packet.h
  4. 16
      SourceX/dvlnet/tcp_client.cpp
  5. 2
      SourceX/dvlnet/tcp_client.h
  6. 1
      SourceX/dvlnet/tcp_server.cpp
  7. 2
      SourceX/storm_net.cpp

9
SourceX/dvlnet/base.cpp

@ -7,6 +7,7 @@ using namespace dvlnet;
void base::setup_gameinfo(buffer_t info)
{
game_init_info = std::move(info);
pktfty = std::make_unique<packet_factory>();
}
void base::setup_password(std::string pw)
@ -192,10 +193,8 @@ void* base::SNetRegisterEventHandler(event_type evtype, snet_event_func func)
bool base::SNetLeaveGame(int type)
{
if(pktfty) {
auto pkt = pktfty->make_packet<PT_DISCONNECT>(plr_self, PLR_BROADCAST,
plr_self, type);
send(*pkt);
}
auto pkt = pktfty->make_packet<PT_DISCONNECT>(plr_self, PLR_BROADCAST,
plr_self, type);
send(*pkt);
return true;
}

2
SourceX/dvlnet/loopback.cpp

@ -32,7 +32,7 @@ bool loopback::SNetSendMessage(int dest, void* data, unsigned int size)
message_queue.push(message);
}
return true;
}
}
bool loopback::SNetReceiveTurns(char** data, unsigned int* size, DWORD* status)
{

2
SourceX/dvlnet/packet.h

@ -243,7 +243,7 @@ namespace dvlnet {
public:
static constexpr unsigned short max_packet_size = 0xFFFF;
packet_factory(std::string pw);
packet_factory(std::string pw = "");
std::unique_ptr<packet> make_packet(buffer_t buf);
template<packet_type t, typename... Args>
std::unique_ptr<packet> make_packet(Args... args);

16
SourceX/dvlnet/tcp_client.cpp

@ -1,22 +1,28 @@
#include "dvlnet/tcp_client.h"
#include <functional>
#include <system_error>
using namespace dvlnet;
int tcp_client::create(std::string addrstr, std::string passwd)
{
local_server = std::make_unique<tcp_server>(ioc, addrstr,
6112, passwd);
return join(local_server->localhost_self(), passwd);
try {
auto port = default_port;
local_server = std::make_unique<tcp_server>(ioc, addrstr, port, passwd);
return join(local_server->localhost_self(), passwd);
} catch(std::system_error) {
return -1;
}
}
int tcp_client::join(std::string addrstr, std::string passwd)
{
setup_password(passwd);
auto ipaddr = asio::ip::make_address(addrstr);
sock.connect(asio::ip::tcp::endpoint(ipaddr, 6112));
sock.connect(asio::ip::tcp::endpoint(ipaddr, default_port));
start_recv();
{ // hack: try to join for 5 seconds
{
randombytes_buf(reinterpret_cast<unsigned char*>(&cookie_self),
sizeof(cookie_t));
auto pkt = pktfty->make_packet<PT_JOIN_REQUEST>(PLR_BROADCAST,

2
SourceX/dvlnet/tcp_client.h

@ -18,7 +18,7 @@ namespace dvlnet {
int create(std::string addrstr, std::string passwd);
int join(std::string addrstr, std::string passwd);
static constexpr unsigned short default_port = 6112;
constexpr static unsigned short default_port = 6112;
virtual void poll();
virtual void send(packet& pkt);

1
SourceX/dvlnet/tcp_server.cpp

@ -1,4 +1,3 @@
#include "pch.h"
#include "dvlnet/tcp_server.h"
using namespace dvlnet;

2
SourceX/storm_net.cpp

@ -101,14 +101,12 @@ BOOL STORMAPI SNetCreateGame(const char *pszGameName, const char *pszGamePasswor
dvlnet::buffer_t game_init_info(GameTemplateData, GameTemplateData + GameTemplateSize);
dvlnet_inst->setup_gameinfo(std::move(game_init_info));
*playerID = dvlnet_inst->create("0.0.0.0", pszGamePassword);
return *playerID != -1;
}
BOOL STORMAPI SNetJoinGame(int id, char *pszGameName, char *pszGamePassword, char *playerName, char *userStats, int *playerID)
{
*playerID = dvlnet_inst->join(pszGameName, pszGamePassword);
return *playerID != -1;
}

Loading…
Cancel
Save