Added auto-discovery of LAN servers using UDP broadcast. (for now the client must press the A key from the join menu to trigger this)

This commit is contained in:
Mark Vejvoda
2010-04-01 06:31:10 +00:00
parent 5ecc84099a
commit 0bcb710606
13 changed files with 515 additions and 13 deletions

View File

@@ -23,6 +23,8 @@
#include <netdb.h>
#include <fcntl.h>
#include <map>
#include <vector>
#include "thread.h"
using std::string;
@@ -53,11 +55,16 @@ class Socket {
protected:
int sock;
long lastDebugEvent;
static int broadcast_portno;
public:
Socket(int sock);
Socket();
~Socket();
virtual ~Socket();
static int getBroadCastPort() { return broadcast_portno; }
static void setBroadCastPort(int value) { broadcast_portno = value; }
static std::vector<std::string> getLocalIPAddressList();
// Int lookup is socket fd while bool result is whether or not that socket was signalled for reading
static bool hasDataToRead(std::map<int,bool> &socketTriggeredList);
@@ -91,6 +98,28 @@ protected:
class ClientSocket: public Socket{
public:
void connect(const Ip &ip, int port);
static std::vector<string> discoverServers();
};
class BroadCastSocketThread : public Thread
{
private:
Mutex mutexRunning;
Mutex mutexQuit;
bool quit;
bool running;
void setRunningStatus(bool value);
void setQuitStatus(bool value);
public:
BroadCastSocketThread();
virtual void execute();
void signalQuit();
bool getQuitStatus();
bool getRunningStatus();
};
// =====================================================
@@ -99,9 +128,18 @@ public:
class ServerSocket: public Socket{
public:
ServerSocket();
virtual ~ServerSocket();
void bind(int port);
void listen(int connectionQueueSize= SOMAXCONN);
Socket *accept();
void stopBroadCastThread();
protected:
BroadCastSocketThread *broadCastThread;
void startBroadCastThread();
};
}}//end namespace

View File

@@ -108,6 +108,7 @@ public:
// Misc
// =====================================================
int MessageBox(int handle, const char *msg, const char *title, int buttons);
void Tokenize(const string& str,vector<string>& tokens,const string& delimiters = " ");
bool isdir(const char *path);
void findDirs(const vector<string> &paths, vector<string> &results, bool errorOnNotFound=false);
void findAll(const vector<string> &paths, const string &fileFilter, vector<string> &results, bool cutExtension=false, bool errorOnNotFound=true);

View File

@@ -1,7 +1,7 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<EFBFBD>o Figueroa
// Copyright (C) 2001-2008 Martio Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
@@ -101,6 +101,7 @@ public:
// =====================================================
// Misc
// =====================================================
void Tokenize(const string& str,vector<string>& tokens,const string& delimiters = " ");
bool isdir(const char *path);
void findDirs(const vector<string> &paths, vector<string> &results, bool errorOnNotFound=false);
void findAll(const vector<string> &paths, const string &fileFilter, vector<string> &results, bool cutExtension=false, bool errorOnNotFound=true);

View File

@@ -103,10 +103,15 @@ public:
// =====================================================
class ServerSocket: public Socket{
public:
void bind(int port);
void listen(int connectionQueueSize= SOMAXCONN);
Socket *accept();
protected:
void broadcast_thread();
};
}}//end namespace