begin removing all references to metaserver in favor of pvpgn

This commit is contained in:
Tim Felgentreff 2020-08-13 13:43:39 +02:00
parent 201773b155
commit 61d4818a68
8 changed files with 75 additions and 431 deletions

View file

@ -232,7 +232,6 @@ set(network_SRCS
src/network/commands.cpp
src/network/net_lowlevel.cpp
src/network/net_message.cpp
src/network/master.cpp
src/network/netconnect.cpp
src/network/network.cpp
src/network/netsockets.cpp
@ -367,7 +366,6 @@ set(tolua_FILES
src/tolua/font.pkg
src/tolua/game.pkg
src/tolua/map.pkg
src/tolua/master.pkg
src/tolua/minimap.pkg
src/tolua/network.pkg
src/tolua/particle.pkg
@ -537,7 +535,6 @@ set(stratagus_generic_HDRS
src/include/iolib.h
src/include/luacallback.h
src/include/map.h
src/include/master.h
src/include/menus.h
src/include/minimap.h
src/include/missile.h
@ -1104,43 +1101,6 @@ endif()
########### next target ###############
set(metaserver_SRCS
metaserver/cmd.cpp
metaserver/db.cpp
metaserver/games.cpp
metaserver/main.cpp
metaserver/netdriver.cpp
src/network/net_lowlevel.cpp
)
set(metaserver_HDRS
metaserver/cmd.h
metaserver/db.h
metaserver/games.h
metaserver/netdriver.h
)
source_group(metaserver FILES ${metaserver_SRCS} ${metaserver_HDRS})
if(SQLITE_FOUND)
add_executable(metaserver ${metaserver_SRCS} ${metaserver_HDRS})
target_link_libraries(metaserver ${SDL2_LIBRARY} ${SQLITE_LIBRARIES})
if(WIN32)
target_link_libraries(metaserver winmm ws2_32)
endif()
if(WIN32 AND MINGW)
target_link_libraries(metaserver dxguid)
endif()
if(WIN32 AND MINGW AND ENABLE_STATIC)
set_target_properties(metaserver PROPERTIES LINK_FLAGS "${LINK_FLAGS} -static-libgcc -static-libstdc++")
endif()
endif()
########### next target ###############
set(png2stratagus_SRCS
tools/png2stratagus.cpp
)
@ -1175,8 +1135,6 @@ set(doxygen_FILES
doc/Doxyfile-header.html
${stratagus_SRCS}
${stratagus_HDRS}
${metaserver_SRCS}
${metaserver_HDRS}
${gameheaders_HDRS}
${png2stratagus_SRCS}
)
@ -1211,9 +1169,6 @@ endmacro()
if(ENABLE_UPX AND SELF_PACKER_FOR_EXECUTABLE)
self_packer(stratagus)
self_packer(png2stratagus)
if(SQLITE_FOUND)
self_packer(metaserver)
endif()
endif()
########### next target ###############
@ -1238,10 +1193,6 @@ endif()
install(TARGETS stratagus DESTINATION ${GAMEDIR})
install(TARGETS png2stratagus DESTINATION ${BINDIR})
if(SQLITE_FOUND)
install(TARGETS metaserver DESTINATION ${BINDIR} RENAME stratagus-metaserver)
endif()
if(ENABLE_DOC AND DOXYGEN_FOUND)
install(FILES doc/stratagus.6 DESTINATION ${MANDIR})
install(FILES
@ -1253,7 +1204,6 @@ if(ENABLE_DOC AND DOXYGEN_FOUND)
doc/index.html
doc/install.html
doc/media.html
doc/metaserver_protocol.txt
doc/README-SDL.txt
DESTINATION share/doc/stratagus
)

View file

@ -1,86 +0,0 @@
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
/**@name master.h - The master server headerfile. */
//
// (c) Copyright 2003-2007 by Tom Zickel and Jimmy Salmon
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
#ifndef __MASTER_H__
#define __MASTER_H__
//@{
#include <list>
#include "network/netsockets.h"
/*----------------------------------------------------------------------------
-- Declarations
----------------------------------------------------------------------------*/
struct lua_State;
// Log data used in metaserver client
struct CClientLog {
std::string entry; // command itself
};
// Class representing meta server client structure
class CMetaClient
{
public:
CMetaClient() : metaSocket(), metaPort(-1), lastRecvState(-1) {}
~CMetaClient();
void SetMetaServer(const std::string host, const int port);
int Init();
void Close();
int Send(const std::string cmd);
int Recv();
int GetLastRecvState() { return lastRecvState; }
int GetLogSize() { return events.size(); }
CClientLog *GetLastMessage() { return events.back(); }
int CreateGame(std::string desc, std::string map, std::string players);
CHost *GetMetaServer() { return new CHost(metaHost.c_str(), metaPort); }
private:
CTCPSocket metaSocket; /// This is a TCP socket
std::string metaHost; /// Address of metaserver
int metaPort; /// Port of metaserver
std::list <CClientLog *> events; /// All commands received from metaserver
int lastRecvState; /// Now many bytes have been received in last reply
};
/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/
// Metaserver itself
extern CMetaClient MetaClient;
//@}
#endif // !__MASTER_H__

View file

@ -1,6 +1,23 @@
#ifndef __ONLINE_SERVICE_H__
#define __ONLINE_SERVICE_H__
void GoOnline();
class OnlineContext {
public:
virtual void doOneStep();
#endif // !__EDITOR_H__
virtual void goOnline();
virtual void joinGame(std:string name, std::string pw);
// TODO: allow passing all the other options, like 1 peon only, resource amount, game type, ...
virtual void advertiseGame(std::string name, std::string pw, std::string creatorName, std::string mapName,
int mapX, int mapY, int maxPlayers, int playersInGame);
virtual void stopAdvertisingGame();
virtual void reportGameResult();
};
extern OnlineContext *OnlineContextHandler;
#endif

View file

@ -1,244 +0,0 @@
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
/**@name master.cpp - The master server. */
//
// (c) Copyright 2003-2007 by Tom Zickel and Jimmy Salmon
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include <errno.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include "stratagus.h"
#include "master.h"
#include "game.h"
#include "network/netsockets.h"
#include "network.h"
#include "net_lowlevel.h"
#include "parameters.h"
#include "script.h"
#include "version.h"
/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/
CMetaClient MetaClient;
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Set the metaserver to use for internet play.
**
** @param host Host to connect
** @param port Port to use to connect
*/
void CMetaClient::SetMetaServer(const std::string host, const int port)
{
metaHost = host;
metaPort = port;
}
CMetaClient::~CMetaClient()
{
for (std::list<CClientLog *>::iterator it = events.begin(); it != events.end(); ++it) {
CClientLog *log = *it;
delete log;
}
events.clear();
this->Close();
}
/**
** Initialize the TCP connection to the Meta Server and send test ping to it.
**
** @return -1 fail, 0 success.
*/
int CMetaClient::Init()
{
if (metaPort == -1) {
return -1;
}
// Server socket
CHost metaServerHost(metaHost.c_str(), metaPort);
// Client socket
// open on all interfaces, not the loopback, unless we have an override from the commandline
std::string localHost = CNetworkParameter::Instance.localHost;
if (!localHost.compare("127.0.0.1")) {
localHost = "0.0.0.0";
}
CHost metaClientHost(localHost.c_str(), CNetworkParameter::Instance.localPort);
metaSocket.Open(metaClientHost);
if (metaSocket.IsValid() == false) {
fprintf(stderr, "METACLIENT: No free port %d available, aborting\n", metaServerHost.getPort());
return -1;
}
if (metaSocket.Connect(metaServerHost) == false) {
fprintf(stderr, "METACLIENT: Unable to connect to host %s\n", metaServerHost.toString().c_str());
MetaClient.Close();
return -1;
}
if (this->Send("PING") == -1) { // not sent
MetaClient.Close();
return -1;
}
if (this->Recv() == -1) { // not received
MetaClient.Close();
return -1;
}
CClientLog &log = *GetLastMessage();
if (log.entry.find("PING_OK") != std::string::npos) {
// Everything is OK
return 0;
} else {
fprintf(stderr, "METACLIENT: inappropriate message received from %s\n", metaServerHost.toString().c_str());
MetaClient.Close();
return -1;
}
}
/**
** Close Connection to Master Server
**
** @return nothing
*/
void CMetaClient::Close()
{
if (metaSocket.IsValid()) {
metaSocket.Close();
}
}
/**
** Send a command to the meta server
**
** @param cmd command to send
**
** @returns -1 if failed, otherwise length of command
*/
int CMetaClient::Send(const std::string cmd)
{
int ret = -1;
if (metaSocket.IsValid()) {
std::string mes(cmd);
mes.append("\n");
ret = metaSocket.Send(mes.c_str(), mes.size());
}
return ret;
}
/**
** Receive reply from Meta Server
**
** @return error or number of bytes
*/
int CMetaClient::Recv()
{
if (metaSocket.HasDataToRead(5000) == -1) {
return -1;
}
char buf[1024];
memset(&buf, 0, sizeof(buf));
int n = metaSocket.Recv(&buf, sizeof(buf));
if (n == -1) {
return n;
}
// We know we now have the whole command.
// Convert to standard notation
std::string cmd(buf, strlen(buf));
cmd += '\n';
cmd += '\0';
CClientLog *log = new CClientLog;
log->entry = cmd;
events.push_back(log);
lastRecvState = n;
return n;
}
//@}
int CMetaClient::CreateGame(std::string desc, std::string map, std::string players) {
if (metaSocket.IsValid() == false) {
return -1;
}
if (NetworkFildes.IsValid() == false) {
return -1;
}
CHost metaServerHost(metaHost.c_str(), metaPort);
// Advertise an external IP address if we can
unsigned long ips[1];
int networkNumInterfaces = NetworkFildes.GetSocketAddresses(ips, 1);
std::string ipport = "";
if (!networkNumInterfaces || CNetworkParameter::Instance.localHost.compare("127.0.0.1")) {
ipport += CNetworkParameter::Instance.localHost.c_str();
} else {
ipport += inet_ntoa(((struct in_addr *)ips)[0]);
}
ipport += " ";
ipport += std::to_string(CNetworkParameter::Instance.localPort);
std::string cmd("CREATEGAME \"");
cmd += desc;
cmd += "\" \"";
cmd += map;
cmd += "\" ";
cmd += players;
cmd += " ";
cmd += ipport;
if (this->Send(cmd.c_str()) == -1) { // not sent
return -1;
}
if (this->Recv() == -1) { // not received
return -1;
}
CClientLog &log = *GetLastMessage();
if (log.entry.find("CREATEGAME_OK") != std::string::npos) {
// Everything is OK, let's inform metaserver of our UDP info
NetworkFildes.Send(metaServerHost, ipport.c_str(), ipport.size());
return 0;
} else {
fprintf(stderr, "METACLIENT: failed to create game: %s\n", log.entry.c_str());
return -1;
}
}

View file

@ -39,7 +39,6 @@
#include "interface.h"
#include "map.h"
#include "master.h"
#include "network.h"
#include "parameters.h"
#include "player.h"

View file

@ -584,7 +584,7 @@ protected:
int send(Context *ctx, BNCSOutputStream *buf);
};
class Context {
class Context : public OnlineContext {
public:
Context() {
this->udpSocket = new CUDPSocket();
@ -606,7 +606,29 @@ public:
delete host;
}
boolean isConnected() {
return !getCurrentChannel().empty();
}
// User and UI actions
void disconnect() {
if (isConnected()) {
// SID_STOPADV: according to bnetdocs.org, this is always sent when
// clients disconnect, regardless of state
BNCSOutputStream stop(0x02);
stop.flush(tcpSocket);
// SID_LEAVECHAT
BNCSOutputStream leave(0x10);
leave.flush(tcpSocket);
}
udpSocket->Close();
tcpSocket->Close();
state = NULL;
clientToken = MyRand();
username = "";
setPassword("");
}
void sendText(std::string txt) {
// C>S 0x0E SID_CHATCOMMAND
int pos = 0;
@ -759,7 +781,7 @@ public:
BNCSInputStream *getMsgIStream() { return istream; }
void doOneStep() { this->state->doOneStep(this); }
virtual void doOneStep() { if (this->state != NULL) this->state->doOneStep(this); }
void setState(NetworkState* newState) {
assert (newState != this->state);
@ -808,6 +830,7 @@ public:
std::cout << message << std::endl;
ctx->showInfo(message);
hasPrinted = true;
ctx->disconnect();
}
// the end
}
@ -822,6 +845,7 @@ class C2S_GAMERESULT_OR_STOPADV : public NetworkState {
// TODO - wait until the game lobby is left or the game is over and then send the result
// C>S 0x02 SID_STOPADV
// C>S 0x2C SID_GAMERESULT
// C>S 0x22 SID_NOTIFYJOIN
}
};
@ -850,6 +874,7 @@ public:
switch (msg) {
case 0x00: // SID_NULL
handleNull(ctx);
break;
case 0x25: // SID_PING
handlePing(ctx);
@ -894,9 +919,13 @@ public:
}
private:
void handleNull(Context *ctx) {
BNCSOutputStream buffer(0x00);
send(ctx, &buffer);
}
void handlePing(Context *ctx) {
uint32_t pingValue = ctx->getMsgIStream()->read32();
ctx->getMsgIStream()->finishMessage();
BNCSOutputStream buffer(0x25);
buffer.serialize32(pingValue);
send(ctx, &buffer);
@ -1585,12 +1614,13 @@ static gcn::Container *loginWindowContainer;
static gcn::TextField *username;
static gcn::TextField *password;
static Context *ctx;
static Context _ctx;
Context *OnlineContext = &_ctx;
class ChatInputListener : public gcn::ActionListener {
virtual void action(const std::string &) {
if (!ctx->getCurrentChannel().empty()) {
ctx->sendText(chatInput->getText());
if (!OnlineContext->getCurrentChannel().empty()) {
OnlineContext->sendText(chatInput->getText());
chatInput->setText("");
}
}
@ -1598,15 +1628,15 @@ class ChatInputListener : public gcn::ActionListener {
class UsernameInputListener : public gcn::ActionListener {
virtual void action(const std::string &) {
ctx->setUsername(username->getText());
ctx->setPassword(password->getText());
OnlineContext->setUsername(username->getText());
OnlineContext->setPassword(password->getText());
}
};
class PasswordInputListener : public gcn::ActionListener {
virtual void action(const std::string &) {
ctx->setUsername(username->getText());
ctx->setPassword(password->getText());
OnlineContext->setUsername(username->getText());
OnlineContext->setPassword(password->getText());
}
};
@ -1619,9 +1649,7 @@ void GoOnline() {
gcn::Widget *oldTop = Gui->getTop();
Gui->setUseDirtyDrawing(false);
ctx = new Context();
ctx->setHost(MetaClient.GetMetaServer());
ctx->setState(new ConnectState());
OnlineContext->setState(new ConnectState());
onlineServiceContainer = new gcn::Container();
onlineServiceContainer->setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height));
@ -1695,19 +1723,17 @@ void GoOnline() {
InterfaceState = IfaceStateNormal;
UI.SelectedViewport = UI.Viewports;
while (1) {
ctx->doOneStep();
if (!ctx->getCurrentChannel().empty()) {
if (OnlineContext->isConnected()) {
loginWindow->setVisible(false);
if ((FrameCounter % (FRAMES_PER_SECOND * 5)) == 0) {
ctx->refreshGames();
ctx->refreshFriends();
OnlineContext->refreshGames();
OnlineContext->refreshFriends();
}
if ((FrameCounter % (FRAMES_PER_SECOND * 1)) == 0) {
static_cast<gcn::TextBox*>(gamelistArea->getContent())->setText("");
for (auto g : ctx->getGames()) {
for (auto g : OnlineContext->getGames()) {
static_cast<gcn::TextBox*>(gamelistArea->getContent())->addRow(g->getMap() + " " +
g->getCreator() + " " +
g->getGameType() + " " +
@ -1716,25 +1742,25 @@ void GoOnline() {
}
static_cast<gcn::TextBox*>(usersArea->getContent())->setText("");
for (auto u : ctx->getUsers()) {
for (auto u : OnlineContext->getUsers()) {
static_cast<gcn::TextBox*>(usersArea->getContent())->addRow(u);
}
static_cast<gcn::TextBox*>(channelsArea->getContent())->setText("");
for (auto u : ctx->getChannels()) {
for (auto u : OnlineContext->getChannels()) {
static_cast<gcn::TextBox*>(channelsArea->getContent())->addRow(u);
}
static_cast<gcn::TextBox*>(friendsArea->getContent())->setText("");
for (auto u : ctx->getFriends()) {
for (auto u : OnlineContext->getFriends()) {
static_cast<gcn::TextBox*>(friendsArea->getContent())->addRow(u->getName() + ", " + u->getStatus() + ", " + u->getProduct());
}
}
}
while (!ctx->getInfo()->empty()) {
static_cast<gcn::TextBox*>(messageArea->getContent())->addRow(ctx->getInfo()->front());
ctx->getInfo()->pop();
while (!OnlineContext->getInfo()->empty()) {
static_cast<gcn::TextBox*>(messageArea->getContent())->addRow(OnlineContext->getInfo()->front());
OnlineContext->getInfo()->pop();
}
Gui->draw();

View file

@ -1,23 +0,0 @@
$#include "master.h"
struct CClientLog
{
std::string entry;
};
class CMetaClient
{
public:
CMetaClient();
void SetMetaServer(const std::string host, const int port);
int Init();
void Close();
int Send(const std::string cmd);
int Recv();
int GetLastRecvState() { return lastRecvState; }
int GetLogSize() { return events.size(); }
CClientLog* GetLastMessage() { return events.back(); }
int CreateGame(std::string desc, std::string map, std::string players);
};
CMetaClient MetaClient;

View file

@ -35,6 +35,7 @@
----------------------------------------------------------------------------*/
#include "stratagus.h"
#include "online_service.h"
#ifdef DEBUG
#include <signal.h>
@ -676,6 +677,10 @@ void WaitEventsOneFrame()
GetCallbacks()->NetworkEvent();
}
}
// Online session
OnlineContextHandler->doOneStep();
// No more input and time for frame over: return
if (!i && s <= 0 && interrupts) {
break;