Ripped out all networking systems; simply not working correctly
This commit is contained in:
+1
-11
@@ -9,25 +9,17 @@ using namespace std;
|
|||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, TCPSocket* sock) {
|
Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
cout << "entering Lobby" << endl;
|
cout << "entering Lobby" << endl;
|
||||||
#endif
|
#endif
|
||||||
configUtil = cUtil;
|
configUtil = cUtil;
|
||||||
surfaceMgr = sMgr;
|
surfaceMgr = sMgr;
|
||||||
socket = sock;
|
|
||||||
|
|
||||||
//ping the network, ping the preset "phone home" servers
|
//ping the network, ping the preset "phone home" servers
|
||||||
//generate the server list
|
//generate the server list
|
||||||
//eventually
|
//eventually
|
||||||
|
|
||||||
try {
|
|
||||||
socket->Open(configUtil->CString("ip"), configUtil->Integer("port"));
|
|
||||||
}
|
|
||||||
catch(exception& e) {
|
|
||||||
cerr << "Network Error: " << e.what() << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
SetNextScene(SceneList::TESTSYSTEMS);
|
SetNextScene(SceneList::TESTSYSTEMS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,5 +76,3 @@ void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
|
|||||||
void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
|
void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-3
@@ -5,12 +5,11 @@
|
|||||||
|
|
||||||
#include "config_utility.hpp"
|
#include "config_utility.hpp"
|
||||||
#include "surface_manager.hpp"
|
#include "surface_manager.hpp"
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
class Lobby : public BaseScene {
|
class Lobby : public BaseScene {
|
||||||
public:
|
public:
|
||||||
//Public access members
|
//Public access members
|
||||||
Lobby(ConfigUtility*, SurfaceManager*, TCPSocket*);
|
Lobby(ConfigUtility*, SurfaceManager*);
|
||||||
virtual ~Lobby();
|
virtual ~Lobby();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -30,7 +29,6 @@ protected:
|
|||||||
//members
|
//members
|
||||||
ConfigUtility* configUtil;
|
ConfigUtility* configUtil;
|
||||||
SurfaceManager* surfaceMgr;
|
SurfaceManager* surfaceMgr;
|
||||||
TCPSocket* socket;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
void NetworkInit() {
|
|
||||||
WSADATA wsaData; // if this doesn't work
|
|
||||||
//WSAData wsaData; // then try this instead
|
|
||||||
|
|
||||||
// MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
|
|
||||||
|
|
||||||
if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
|
|
||||||
throw(std::runtime_error("WSAStartup failed"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkQuit() {
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#ifndef NETWORK_HPP_
|
|
||||||
#define NETWORK_HPP_
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define _WIN32_WINNT 0x501
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
|
|
||||||
void NetworkInit();
|
|
||||||
void NetworkQuit();
|
|
||||||
|
|
||||||
class TCPSocket {
|
|
||||||
public:
|
|
||||||
TCPSocket();
|
|
||||||
TCPSocket(const char* ip, int port);
|
|
||||||
~TCPSocket();
|
|
||||||
|
|
||||||
/* param 1: ip address to connect to
|
|
||||||
* param 2: port to open the server socket on
|
|
||||||
*/
|
|
||||||
void Open(const char* ip, int port);
|
|
||||||
void Close();
|
|
||||||
|
|
||||||
/* Send() and Receive()
|
|
||||||
* param 1: data to be sent/received
|
|
||||||
* param 2: length/maxlength of the data
|
|
||||||
* param 3: flags to the internal function
|
|
||||||
* return:
|
|
||||||
* the amount of data sent/received (not necessarily the same value as len/maxlen)
|
|
||||||
*/
|
|
||||||
int Send(const void* data, int len, int flags = 0);
|
|
||||||
int Recv(void* data, int maxlen, int flags = 0);
|
|
||||||
private:
|
|
||||||
SOCKET sock;
|
|
||||||
friend class TCPServerSocket;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TCPServerSocket {
|
|
||||||
public:
|
|
||||||
TCPServerSocket();
|
|
||||||
TCPServerSocket(int port);
|
|
||||||
~TCPServerSocket();
|
|
||||||
|
|
||||||
void Open(int port);
|
|
||||||
void Close();
|
|
||||||
|
|
||||||
int Accept(TCPSocket*, int uSeconds = 0);
|
|
||||||
private:
|
|
||||||
SOCKET sock;
|
|
||||||
};
|
|
||||||
|
|
||||||
//TODO: Write the UDP systems
|
|
||||||
//
|
|
||||||
//class UDPRemote {
|
|
||||||
//public:
|
|
||||||
// UDPRemote();
|
|
||||||
// UDPRemote(const char* ip, int port);
|
|
||||||
//
|
|
||||||
// /* param 1: ip of the remote to connect to, null to clear
|
|
||||||
// * param 2: port of the remote to connect to
|
|
||||||
// */
|
|
||||||
// void Set(const char* ip, int port);
|
|
||||||
// //TODO: Get?
|
|
||||||
//private:
|
|
||||||
// sockaddr addr;
|
|
||||||
// friend class UDPSocket;
|
|
||||||
//};
|
|
||||||
//
|
|
||||||
//class UDPSocket {
|
|
||||||
//public:
|
|
||||||
// UDPSocket();
|
|
||||||
// UDPSocket(int port);
|
|
||||||
// ~UDPSocket();
|
|
||||||
//
|
|
||||||
// int Open(int port);
|
|
||||||
// void Close();
|
|
||||||
//
|
|
||||||
// int Send(const void* data, int len, UDPRemote* rem, int flags = 0);
|
|
||||||
// int Recv(void* data, int maxlen, UDPRemote* rem, int flags = 0);
|
|
||||||
//private:
|
|
||||||
// SOCKET sock;
|
|
||||||
//};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,184 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <cstdio>
|
|
||||||
|
|
||||||
/* TCPSocket definition
|
|
||||||
*/
|
|
||||||
|
|
||||||
TCPSocket::TCPSocket() {
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPSocket::TCPSocket(const char* ip, int port) {
|
|
||||||
Open(ip, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPSocket::~TCPSocket() {
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPSocket::Open(const char* ip, int port) {
|
|
||||||
addrinfo *ptr = nullptr, hints;
|
|
||||||
char buf[100];
|
|
||||||
sprintf(buf, "%d",port); //std compliant itoa()
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
|
|
||||||
hints.ai_family = AF_UNSPEC;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
|
||||||
|
|
||||||
if (getaddrinfo(ip, buf, &hints, &ptr)) {
|
|
||||||
throw(std::runtime_error("TCPSocket failed to access address info"));
|
|
||||||
}
|
|
||||||
|
|
||||||
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
|
||||||
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
throw(std::runtime_error("Failed to create a TCPSocket"));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool connected = false;
|
|
||||||
for(addrinfo *it = ptr; it; it = it->ai_next) {
|
|
||||||
if (!connect(sock, it->ai_addr, it->ai_addrlen)) {
|
|
||||||
connected = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
|
|
||||||
if (!connected) {
|
|
||||||
closesocket(sock);
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
throw(std::runtime_error("Failed to connect a TCPSocket"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPSocket::Close() {
|
|
||||||
closesocket(sock);
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TCPSocket::Send(const void* data, int len, int flags) {
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
throw(std::runtime_error("Failed to send, TCPSocket is invalid"));
|
|
||||||
}
|
|
||||||
int ret = send(sock, (const char*)data, len, flags);
|
|
||||||
if (ret == SOCKET_ERROR) {
|
|
||||||
Close();
|
|
||||||
throw(std::runtime_error("Failed to send, unknown error, TCPSocket automatically closed"));
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TCPSocket::Recv(void* data, int maxlen, int flags) {
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
throw(std::runtime_error("Failed to receive, TCPSocket is invalid"));
|
|
||||||
}
|
|
||||||
int ret = recv(sock, (char*)data, maxlen, flags);
|
|
||||||
if (ret == SOCKET_ERROR) {
|
|
||||||
Close();
|
|
||||||
throw(std::runtime_error("Failed to receive, unknown error, TCPSocket automatically closed"));
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TCPServerSocket definition
|
|
||||||
*/
|
|
||||||
|
|
||||||
TCPServerSocket::TCPServerSocket() {
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPServerSocket::TCPServerSocket(int port) {
|
|
||||||
Open(port);
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPServerSocket::~TCPServerSocket() {
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPServerSocket::Open(int port) {
|
|
||||||
addrinfo *ptr = nullptr, hints;
|
|
||||||
char buf[100];
|
|
||||||
sprintf(buf, "%d",port); //std compliant itoa()
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
|
||||||
hints.ai_flags = AI_PASSIVE;
|
|
||||||
|
|
||||||
if (getaddrinfo(nullptr, buf, &hints, &ptr)) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket failed to access address info"));
|
|
||||||
}
|
|
||||||
|
|
||||||
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
|
||||||
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
throw(std::runtime_error("Failed to create a TCPServerSocket"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bind(sock, ptr->ai_addr, ptr->ai_addrlen) == SOCKET_ERROR) {
|
|
||||||
closesocket(sock);
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
throw(std::runtime_error("Failed to bind TCPServerSocket"));
|
|
||||||
}
|
|
||||||
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPServerSocket::Close() {
|
|
||||||
closesocket(sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
int TCPServerSocket::Accept(TCPSocket* s, int uSeconds) {
|
|
||||||
if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket: listen() error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//file descriptor sets are to prevent blocking
|
|
||||||
fd_set readfds;
|
|
||||||
timeval tv = {0, uSeconds};
|
|
||||||
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(sock, &readfds);
|
|
||||||
|
|
||||||
if (select(0, &readfds, nullptr, nullptr, &tv) == SOCKET_ERROR) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket: select() error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//I don't want this to block
|
|
||||||
if (FD_ISSET(sock, &readfds)) {
|
|
||||||
if ((s->sock = accept(sock, nullptr, nullptr)) == INVALID_SOCKET) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket: accept() error"));
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
@@ -38,8 +38,6 @@ void SceneManager::Init() {
|
|||||||
|
|
||||||
configUtil.Load("rsc/config.cfg");
|
configUtil.Load("rsc/config.cfg");
|
||||||
|
|
||||||
NetworkInit();
|
|
||||||
|
|
||||||
//set the screen from the config file
|
//set the screen from the config file
|
||||||
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
|
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
|
||||||
if (configUtil.Boolean("screen.f")) {
|
if (configUtil.Boolean("screen.f")) {
|
||||||
@@ -68,7 +66,6 @@ void SceneManager::Proc() {
|
|||||||
|
|
||||||
void SceneManager::Quit() {
|
void SceneManager::Quit() {
|
||||||
UnloadScene();
|
UnloadScene();
|
||||||
NetworkQuit();
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +80,7 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
|
|||||||
//add scene creation calls here
|
//add scene creation calls here
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
case SceneList::TESTSYSTEMS:
|
case SceneList::TESTSYSTEMS:
|
||||||
activeScene = new TestSystems(&configUtil, &surfaceMgr, &socket);
|
activeScene = new TestSystems(&configUtil, &surfaceMgr);
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -98,7 +95,7 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
|
|||||||
activeScene = new InGame(&configUtil, &surfaceMgr);
|
activeScene = new InGame(&configUtil, &surfaceMgr);
|
||||||
break;
|
break;
|
||||||
case SceneList::LOBBY:
|
case SceneList::LOBBY:
|
||||||
activeScene = new Lobby(&configUtil, &surfaceMgr, &socket);
|
activeScene = new Lobby(&configUtil, &surfaceMgr);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
#include "config_utility.hpp"
|
#include "config_utility.hpp"
|
||||||
#include "surface_manager.hpp"
|
#include "surface_manager.hpp"
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include "SDL/SDL.h"
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
@@ -29,7 +28,6 @@ private:
|
|||||||
|
|
||||||
ConfigUtility configUtil;
|
ConfigUtility configUtil;
|
||||||
SurfaceManager surfaceMgr;
|
SurfaceManager surfaceMgr;
|
||||||
TCPSocket socket;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -8,13 +8,12 @@ using namespace std;
|
|||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr, TCPSocket* sock) {
|
TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
cout << "entering TestSystems" << endl;
|
cout << "entering TestSystems" << endl;
|
||||||
#endif
|
#endif
|
||||||
configUtil = cUtil;
|
configUtil = cUtil;
|
||||||
surfaceMgr = sMgr;
|
surfaceMgr = sMgr;
|
||||||
socket = sock;
|
|
||||||
|
|
||||||
playerCounter = currentPlayer = 0;
|
playerCounter = currentPlayer = 0;
|
||||||
|
|
||||||
@@ -28,7 +27,6 @@ TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr, TCPSocket*
|
|||||||
|
|
||||||
TestSystems::~TestSystems() {
|
TestSystems::~TestSystems() {
|
||||||
playerMgr.DeleteAll();
|
playerMgr.DeleteAll();
|
||||||
socket->Close();
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
cout << "leaving TestSystems" << endl;
|
cout << "leaving TestSystems" << endl;
|
||||||
#endif
|
#endif
|
||||||
@@ -165,6 +163,5 @@ void TestSystems::SwitchToPlayer(int index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TestSystems::SendMessage(std::string s) {
|
void TestSystems::SendMessage(std::string s) {
|
||||||
s += ';';
|
//
|
||||||
socket->Send(s.c_str(), s.length());
|
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include "config_utility.hpp"
|
#include "config_utility.hpp"
|
||||||
#include "surface_manager.hpp"
|
#include "surface_manager.hpp"
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include "player_manager.hpp"
|
#include "player_manager.hpp"
|
||||||
#include "delta.hpp"
|
#include "delta.hpp"
|
||||||
@@ -17,7 +16,7 @@
|
|||||||
class TestSystems : public BaseScene {
|
class TestSystems : public BaseScene {
|
||||||
public:
|
public:
|
||||||
//Public access members
|
//Public access members
|
||||||
TestSystems(ConfigUtility*, SurfaceManager*, TCPSocket*);
|
TestSystems(ConfigUtility*, SurfaceManager*);
|
||||||
virtual ~TestSystems();
|
virtual ~TestSystems();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -42,7 +41,6 @@ protected:
|
|||||||
//members
|
//members
|
||||||
ConfigUtility* configUtil;
|
ConfigUtility* configUtil;
|
||||||
SurfaceManager* surfaceMgr;
|
SurfaceManager* surfaceMgr;
|
||||||
TCPSocket* socket;
|
|
||||||
|
|
||||||
PlayerManager playerMgr;
|
PlayerManager playerMgr;
|
||||||
|
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
void NetworkInit() {
|
|
||||||
WSADATA wsaData; // if this doesn't work
|
|
||||||
//WSAData wsaData; // then try this instead
|
|
||||||
|
|
||||||
// MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
|
|
||||||
|
|
||||||
if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
|
|
||||||
throw(std::runtime_error("WSAStartup failed"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkQuit() {
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#ifndef NETWORK_HPP_
|
|
||||||
#define NETWORK_HPP_
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define _WIN32_WINNT 0x501
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
|
|
||||||
void NetworkInit();
|
|
||||||
void NetworkQuit();
|
|
||||||
|
|
||||||
class TCPSocket {
|
|
||||||
public:
|
|
||||||
TCPSocket();
|
|
||||||
TCPSocket(const char* ip, int port);
|
|
||||||
~TCPSocket();
|
|
||||||
|
|
||||||
/* param 1: ip address to connect to
|
|
||||||
* param 2: port to open the server socket on
|
|
||||||
*/
|
|
||||||
void Open(const char* ip, int port);
|
|
||||||
void Close();
|
|
||||||
|
|
||||||
/* Send() and Receive()
|
|
||||||
* param 1: data to be sent/received
|
|
||||||
* param 2: length/maxlength of the data
|
|
||||||
* param 3: flags to the internal function
|
|
||||||
* return:
|
|
||||||
* the amount of data sent/received (not necessarily the same value as len/maxlen)
|
|
||||||
*/
|
|
||||||
int Send(const void* data, int len, int flags = 0);
|
|
||||||
int Recv(void* data, int maxlen, int flags = 0);
|
|
||||||
private:
|
|
||||||
SOCKET sock;
|
|
||||||
friend class TCPServerSocket;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TCPServerSocket {
|
|
||||||
public:
|
|
||||||
TCPServerSocket();
|
|
||||||
TCPServerSocket(int port);
|
|
||||||
~TCPServerSocket();
|
|
||||||
|
|
||||||
void Open(int port);
|
|
||||||
void Close();
|
|
||||||
|
|
||||||
int Accept(TCPSocket*, int uSeconds = 0);
|
|
||||||
private:
|
|
||||||
SOCKET sock;
|
|
||||||
};
|
|
||||||
|
|
||||||
//TODO: Write the UDP systems
|
|
||||||
//
|
|
||||||
//class UDPRemote {
|
|
||||||
//public:
|
|
||||||
// UDPRemote();
|
|
||||||
// UDPRemote(const char* ip, int port);
|
|
||||||
//
|
|
||||||
// /* param 1: ip of the remote to connect to, null to clear
|
|
||||||
// * param 2: port of the remote to connect to
|
|
||||||
// */
|
|
||||||
// void Set(const char* ip, int port);
|
|
||||||
// //TODO: Get?
|
|
||||||
//private:
|
|
||||||
// sockaddr addr;
|
|
||||||
// friend class UDPSocket;
|
|
||||||
//};
|
|
||||||
//
|
|
||||||
//class UDPSocket {
|
|
||||||
//public:
|
|
||||||
// UDPSocket();
|
|
||||||
// UDPSocket(int port);
|
|
||||||
// ~UDPSocket();
|
|
||||||
//
|
|
||||||
// int Open(int port);
|
|
||||||
// void Close();
|
|
||||||
//
|
|
||||||
// int Send(const void* data, int len, UDPRemote* rem, int flags = 0);
|
|
||||||
// int Recv(void* data, int maxlen, UDPRemote* rem, int flags = 0);
|
|
||||||
//private:
|
|
||||||
// SOCKET sock;
|
|
||||||
//};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,184 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <cstdio>
|
|
||||||
|
|
||||||
/* TCPSocket definition
|
|
||||||
*/
|
|
||||||
|
|
||||||
TCPSocket::TCPSocket() {
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPSocket::TCPSocket(const char* ip, int port) {
|
|
||||||
Open(ip, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPSocket::~TCPSocket() {
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPSocket::Open(const char* ip, int port) {
|
|
||||||
addrinfo *ptr = nullptr, hints;
|
|
||||||
char buf[100];
|
|
||||||
sprintf(buf, "%d",port); //std compliant itoa()
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
|
|
||||||
hints.ai_family = AF_UNSPEC;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
|
||||||
|
|
||||||
if (getaddrinfo(ip, buf, &hints, &ptr)) {
|
|
||||||
throw(std::runtime_error("TCPSocket failed to access address info"));
|
|
||||||
}
|
|
||||||
|
|
||||||
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
|
||||||
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
throw(std::runtime_error("Failed to create a TCPSocket"));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool connected = false;
|
|
||||||
for(addrinfo *it = ptr; it; it = it->ai_next) {
|
|
||||||
if (!connect(sock, it->ai_addr, it->ai_addrlen)) {
|
|
||||||
connected = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
|
|
||||||
if (!connected) {
|
|
||||||
closesocket(sock);
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
throw(std::runtime_error("Failed to connect a TCPSocket"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPSocket::Close() {
|
|
||||||
closesocket(sock);
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TCPSocket::Send(const void* data, int len, int flags) {
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
throw(std::runtime_error("Failed to send, TCPSocket is invalid"));
|
|
||||||
}
|
|
||||||
int ret = send(sock, (const char*)data, len, flags);
|
|
||||||
if (ret == SOCKET_ERROR) {
|
|
||||||
Close();
|
|
||||||
throw(std::runtime_error("Failed to send, unknown error, TCPSocket automatically closed"));
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TCPSocket::Recv(void* data, int maxlen, int flags) {
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
throw(std::runtime_error("Failed to receive, TCPSocket is invalid"));
|
|
||||||
}
|
|
||||||
int ret = recv(sock, (char*)data, maxlen, flags);
|
|
||||||
if (ret == SOCKET_ERROR) {
|
|
||||||
Close();
|
|
||||||
throw(std::runtime_error("Failed to receive, unknown error, TCPSocket automatically closed"));
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TCPServerSocket definition
|
|
||||||
*/
|
|
||||||
|
|
||||||
TCPServerSocket::TCPServerSocket() {
|
|
||||||
sock = INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPServerSocket::TCPServerSocket(int port) {
|
|
||||||
Open(port);
|
|
||||||
}
|
|
||||||
|
|
||||||
TCPServerSocket::~TCPServerSocket() {
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPServerSocket::Open(int port) {
|
|
||||||
addrinfo *ptr = nullptr, hints;
|
|
||||||
char buf[100];
|
|
||||||
sprintf(buf, "%d",port); //std compliant itoa()
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
|
||||||
hints.ai_flags = AI_PASSIVE;
|
|
||||||
|
|
||||||
if (getaddrinfo(nullptr, buf, &hints, &ptr)) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket failed to access address info"));
|
|
||||||
}
|
|
||||||
|
|
||||||
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
|
||||||
|
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
throw(std::runtime_error("Failed to create a TCPServerSocket"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bind(sock, ptr->ai_addr, ptr->ai_addrlen) == SOCKET_ERROR) {
|
|
||||||
closesocket(sock);
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
throw(std::runtime_error("Failed to bind TCPServerSocket"));
|
|
||||||
}
|
|
||||||
|
|
||||||
freeaddrinfo(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCPServerSocket::Close() {
|
|
||||||
closesocket(sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
int TCPServerSocket::Accept(TCPSocket* s, int uSeconds) {
|
|
||||||
if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket: listen() error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//file descriptor sets are to prevent blocking
|
|
||||||
fd_set readfds;
|
|
||||||
timeval tv = {0, uSeconds};
|
|
||||||
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(sock, &readfds);
|
|
||||||
|
|
||||||
if (select(0, &readfds, nullptr, nullptr, &tv) == SOCKET_ERROR) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket: select() error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//I don't want this to block
|
|
||||||
if (FD_ISSET(sock, &readfds)) {
|
|
||||||
if ((s->sock = accept(sock, nullptr, nullptr)) == INVALID_SOCKET) {
|
|
||||||
throw(std::runtime_error("TCPServerSocket: accept() error"));
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
*
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
*
|
|
||||||
* 3. This notice may not be removed or altered from any source
|
|
||||||
* distribution.
|
|
||||||
*/
|
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
+2
-36
@@ -2,16 +2,11 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void Server::Init() {
|
void Server::Init() {
|
||||||
NetworkInit();
|
|
||||||
|
|
||||||
config.Load("config.cfg");
|
config.Load("config.cfg");
|
||||||
servSock.Open(config.Int("port"));
|
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,42 +17,18 @@ void Server::Proc() {
|
|||||||
HandleOutput();
|
HandleOutput();
|
||||||
|
|
||||||
//debug
|
//debug
|
||||||
// running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::Quit() {
|
void Server::Quit() {
|
||||||
for (auto it : socketList) {
|
//
|
||||||
it->Close();
|
|
||||||
delete it;
|
|
||||||
}
|
|
||||||
servSock.Close();
|
|
||||||
NetworkQuit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::HandleInput() {
|
void Server::HandleInput() {
|
||||||
//accept new connections
|
//accept new connections
|
||||||
TCPSocket* sock = new TCPSocket;
|
|
||||||
if (servSock.Accept(sock)) {
|
|
||||||
socketList.push_back(sock);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
delete sock;
|
|
||||||
}
|
|
||||||
//accept updates from the clients
|
//accept updates from the clients
|
||||||
string input;
|
|
||||||
for_each(socketList.begin(), socketList.end(), [&input](TCPSocket* sock){ //why use for_each & lamdas?? to give logan a brain hemorrhage
|
|
||||||
char buffer[512];
|
|
||||||
memset(buffer, 0, 512);
|
|
||||||
sock->Recv(buffer, 512);
|
|
||||||
input += buffer;
|
|
||||||
});
|
|
||||||
//read the updates from the clients into internal containers
|
//read the updates from the clients into internal containers
|
||||||
if (input.size()) {
|
|
||||||
cout << "dumping input from the network" << endl;
|
|
||||||
cout << input << endl;
|
|
||||||
input.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::UpdateWorld() {
|
void Server::UpdateWorld() {
|
||||||
@@ -68,10 +39,5 @@ void Server::UpdateWorld() {
|
|||||||
|
|
||||||
void Server::HandleOutput() {
|
void Server::HandleOutput() {
|
||||||
//send all information to new connections
|
//send all information to new connections
|
||||||
//...
|
|
||||||
//selective updates to existing connectons
|
//selective updates to existing connectons
|
||||||
string s = "hello world";
|
|
||||||
for (auto it : socketList) {
|
|
||||||
it->Send(s.c_str(), s.length());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#define SERVER_HPP_
|
#define SERVER_HPP_
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
#include "config_utility.hpp"
|
||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
@@ -21,8 +20,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
bool running = false;
|
bool running = false;
|
||||||
ConfigUtility config;
|
ConfigUtility config;
|
||||||
TCPServerSocket servSock;
|
|
||||||
std::list<TCPSocket*> socketList;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
#include "network.hpp"
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -7,16 +5,5 @@ using namespace std;
|
|||||||
//receive any amount of info and print it
|
//receive any amount of info and print it
|
||||||
|
|
||||||
int main(int, char**) {
|
int main(int, char**) {
|
||||||
NetworkInit();
|
|
||||||
TCPSocket sock("127.0.0.1",2000);
|
|
||||||
char buffer[512];
|
|
||||||
|
|
||||||
while(true) {
|
|
||||||
memset(buffer, 0, 512);
|
|
||||||
if (sock.Recv(buffer, 512)) {
|
|
||||||
cout << buffer << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NetworkQuit();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user