From 4671afe9004432879ff4cfb79d09569ada37eb62 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 10 May 2013 19:57:53 +1000 Subject: [PATCH] Basic connection between server and unit --- client/makefile | 6 +- client/network.cpp | 39 +++++++ client/network.hpp | 107 ++++++++++++++++++++ client/network_tcp.cpp | 184 ++++++++++++++++++++++++++++++++++ docs/server.txt | 45 ++++----- rsc/graphics/sprites/coa2.bmp | Bin 0 -> 73782 bytes server/delta.hpp | 45 +++++++++ server/main.cpp | 2 +- server/makefile | 9 +- server/network.cpp | 39 +++++++ server/network.hpp | 107 ++++++++++++++++++++ server/network_tcp.cpp | 184 ++++++++++++++++++++++++++++++++++ server/player.hpp | 25 +++++ server/player_manager.hpp | 4 + server/server.cpp | 34 +++++-- server/server.hpp | 5 + server/unit.cpp | 22 ++++ server/vector2.hpp | 76 ++++++++++++++ 18 files changed, 891 insertions(+), 42 deletions(-) create mode 100644 client/network.cpp create mode 100644 client/network.hpp create mode 100644 client/network_tcp.cpp create mode 100644 rsc/graphics/sprites/coa2.bmp create mode 100644 server/delta.hpp create mode 100644 server/network.cpp create mode 100644 server/network.hpp create mode 100644 server/network_tcp.cpp create mode 100644 server/player.hpp create mode 100644 server/player_manager.hpp create mode 100644 server/vector2.hpp diff --git a/client/makefile b/client/makefile index ac3c556..bdc723f 100644 --- a/client/makefile +++ b/client/makefile @@ -1,17 +1,17 @@ #config CXXFLAGS+=-std=c++11 -DDEBUG -LIB=-lmingw32 -lSDLmain -lSDL -lwsock32 -liphlpapi +LIB=-lmingw32 -lSDLmain -lSDL -lwsock32 -lWS2_32 #objects OBJDIR=obj -OBJ=$(addprefix $(OBJDIR)/,base_scene.o scene_manager.o main.o surface_manager.o image.o sprite_sheet.o player.o player_manager.o config_utility.o) +OBJ=$(addprefix $(OBJDIR)/,base_scene.o scene_manager.o surface_manager.o image.o sprite_sheet.o player.o player_manager.o config_utility.o network.o network_tcp.o) #output OUTDIR=out OUT=$(addprefix $(OUTDIR)/,a) #source -SRC=test_systems.cpp in_game.cpp +SRC=test_systems.cpp in_game.cpp main.cpp #targets all: $(OBJ) $(OUT) diff --git a/client/network.cpp b/client/network.cpp new file mode 100644 index 0000000..b4a2e29 --- /dev/null +++ b/client/network.cpp @@ -0,0 +1,39 @@ +/* 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 + +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(); +} diff --git a/client/network.hpp b/client/network.hpp new file mode 100644 index 0000000..7c8bffe --- /dev/null +++ b/client/network.hpp @@ -0,0 +1,107 @@ +/* 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 + +#define WIN32_LEAN_AND_MEAN +#define _WIN32_WINNT 0x501 +#include +#include + +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 diff --git a/client/network_tcp.cpp b/client/network_tcp.cpp new file mode 100644 index 0000000..93d0ad6 --- /dev/null +++ b/client/network_tcp.cpp @@ -0,0 +1,184 @@ +/* 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 +#include + +/* 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; +} diff --git a/docs/server.txt b/docs/server.txt index 2f15fa6..1ace583 100644 --- a/docs/server.txt +++ b/docs/server.txt @@ -24,36 +24,35 @@ This outline is only the most basic example of the server's operation possible, ------------------------- -class Client: +Player: ID --unique, incremental - socket --connection socket + position + motion + avatarName --make sure you've transfered the assets first - GetID - GetSocket - GetIP --wrapper for GetPeerAddress + Update(int) - --handle dropped connections by destroying this client - int Send(data, len); - int Recv(data, maxlen); + Set + Get - --close the socket, and cleanup any other stuff - CloseSocket() + GetPosition() + GetMotion() + GetAvatarName() +end - [stuff] - [things] +class PlayerManager: + maxPlayers + currentPlayers + ticker --incremental -class ClientManager: - Init(max) + Init(maxPlayers) Quit() - CheckNewClients() - Send(id, data, len) - SendAll(data, len) - Recv(id, data, maxlen) + UpdateAll(int) - Get(id) - Close(id) + NewPlayer(...) + GetPlayer(index) + DeletePlayer(...) - GetRaw() --the internal container - - --iteratable + DeleteAll() +end diff --git a/rsc/graphics/sprites/coa2.bmp b/rsc/graphics/sprites/coa2.bmp new file mode 100644 index 0000000000000000000000000000000000000000..bd7b3c0e68a27ce935b84bbda7a7f184e1556bcc GIT binary patch literal 73782 zcmeHPz0%}J5u9`JFAkIQz&wKs%ndvYe9X^|Lj6 z0)nMpeiqHFaD;hn0L^p;RAR(0+L~*(nVrgsIamLN_K(ZOZl(Th8olC7#^uar;&!kF zML^iHYn??G z9tV1qLK~qqzddu0k>mF`H_}Z&0JDO+87&PM`tr-Kw5>BhI_Uq6Xp62@5btSlARd^- z@{NVRlZzz_*hS2h_a1LK3hOs$TzmP6`2Z0Uu$S`+7PjjTYgQqa!U1Ms+UcPwL8%ig zT3#^XaJ7r#k^3jpPlcPAw-i{1LN@%@xLv3;ok0OBOpH4b?6g&UsT`P4OTc|PI6t9( z2K6hKLT7PE7IA4+M_`c!i$Q!k(69sxb#1CiII^PN&fe3#vGJm$mhK*~Q$Gb*ST>K6 z5(@%MiwZ907eoZdAU?%R{~^{&EdmL)iD+Q6KPU#a1fb}S$xRW;S_LC{OnhM;+Ek*@Fm#d zD<02cx_$xH{!2E3apg4skAMCT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_$HO0o;t zKNcVHEd3&?QyBDL$Orn>rI_5|5}F4cqFazGqB#JRh7SL=HdQ|pFDR&=sqy;dS6`f2 zFmdU}swO@=4fvQz8BTmoW9?) zFiwj)%!iWNfAv4_58eI~9IBXms($%jh=*O)f9IFyRiLK-1x4Y1=n|{_2l$5%-*HRw zKSRIuztCU!A1Zms0jHrIj=bp$i2LVTf90HgBT=f9`q&U_e zNOc0dDD+dn_J0!`=0B1C zW(pJiY2wo#>}~&Z=ILF!@V|-w?E2^WV=aPvyeH6k##n1BW|YXyC=+;w22`5Y)xzI@xjaAvjD1LX%<{AFFS+ z{!fnI^lcgR`TyDLANS|fq6UTA{2p*#eW?@?@SYTc_*aVhM${bX12^}yMnQ|SeH2Z# z6|(=zg)Q2y_sJTq9*K)DS+;xftuAInlS9F?>u0jpEQ}lVNXV84Pp;M0BM+AJqI)^; Ia)1u}4>E?7uK)l5 literal 0 HcmV?d00001 diff --git a/server/delta.hpp b/server/delta.hpp new file mode 100644 index 0000000..25423a0 --- /dev/null +++ b/server/delta.hpp @@ -0,0 +1,45 @@ +/* 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 DELTA_HPP_ +#define DELTA_HPP_ + +#include + +class Delta { +public: + Delta() { + time = tick = 0; + } + int Calculate() { + int c = clock(); + time = c - tick; + tick = c; + return time; + } + int GetDelta() const { + return time; + }; +private: + int time, tick; +}; + +#endif diff --git a/server/main.cpp b/server/main.cpp index 3e762ec..4e58cee 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -9,8 +9,8 @@ int main(int, char**) { #ifdef DEBUG cout << "Beginning server" << endl; #endif - Server app; try { + Server app; app.Init(); app.Proc(); app.Quit(); diff --git a/server/makefile b/server/makefile index c0d4c30..7534020 100644 --- a/server/makefile +++ b/server/makefile @@ -1,17 +1,17 @@ #config CXXFLAGS+=-std=c++11 -DDEBUG -LIB=-lmingw32 -lSDLmain -lSDL -lwsock32 -liphlpapi +LIB=-lwsock32 -lWS2_32 #objects OBJDIR=obj -OBJ=$(addprefix $(OBJDIR)/,main.o config_utility.o) +OBJ=$(addprefix $(OBJDIR)/,config_utility.o network.o network_tcp.o) #output OUTDIR=out -OUT=$(addprefix $(OUTDIR)/,a) +OUT=$(addprefix $(OUTDIR)/,server) #source -SRC=server.cpp +SRC=server.cpp main.cpp #targets all: $(OBJ) $(OUT) @@ -36,3 +36,4 @@ clean: rebuild: clean all unit: + $(CXX) $(CXXFLAGS) -o $(OUTDIR)\unit unit.cpp $(OBJ) $(LIB) \ No newline at end of file diff --git a/server/network.cpp b/server/network.cpp new file mode 100644 index 0000000..b4a2e29 --- /dev/null +++ b/server/network.cpp @@ -0,0 +1,39 @@ +/* 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 + +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(); +} diff --git a/server/network.hpp b/server/network.hpp new file mode 100644 index 0000000..7c8bffe --- /dev/null +++ b/server/network.hpp @@ -0,0 +1,107 @@ +/* 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 + +#define WIN32_LEAN_AND_MEAN +#define _WIN32_WINNT 0x501 +#include +#include + +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 diff --git a/server/network_tcp.cpp b/server/network_tcp.cpp new file mode 100644 index 0000000..93d0ad6 --- /dev/null +++ b/server/network_tcp.cpp @@ -0,0 +1,184 @@ +/* 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 +#include + +/* 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; +} diff --git a/server/player.hpp b/server/player.hpp new file mode 100644 index 0000000..de0c599 --- /dev/null +++ b/server/player.hpp @@ -0,0 +1,25 @@ +#ifndef PLAYER_HPP_ +#define PLAYER_HPP_ + +#include "vector2.hpp" + +#include + +//TODO + +class Player { +public: + Player(); + ~Player(); + + void Update(int); + + Vector2 GetPosition(); +private: + int clientID; + Vector2 position; + Vector2 motion; + std::string avatarName; +}; + +#endif diff --git a/server/player_manager.hpp b/server/player_manager.hpp new file mode 100644 index 0000000..f6f0356 --- /dev/null +++ b/server/player_manager.hpp @@ -0,0 +1,4 @@ +#ifndef PLAYERMANAGER_H_ +#define PLAYERMANAGER_H_ + +#endif diff --git a/server/server.cpp b/server/server.cpp index f14be06..b347687 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -1,16 +1,12 @@ #include "server.hpp" -#include "SDL/SDL.h" -#include "SDL_net/SDL_net.h" - #include #include using namespace std; Server::Server() { - running = true; - config.Load("config.cfg"); + running = false; } Server::~Server() { @@ -18,9 +14,12 @@ Server::~Server() { } void Server::Init() { - if (SDLNet_Init()) { - throw(runtime_error("Failed to init SDL_net")); - } + NetworkInit(); + + config.Load("config.cfg"); + servSock.Open(config.Int("port")); + + running = true; } void Server::Proc() { @@ -30,17 +29,25 @@ void Server::Proc() { HandleOutput(); //debug - running = false; +// running = false; } } void Server::Quit() { - SDLNet_Quit(); + for (auto it : sockVec) { + it->Close(); + delete it; + } + servSock.Close(); + NetworkQuit(); } void Server::HandleInput() { //accept new connections - //... + TCPSocket* sock = new TCPSocket; + if (servSock.Accept(sock)) { + sockVec.push_back(sock); + } //accept updates from the clients //... //read the updates from the clients into internal containers @@ -55,5 +62,10 @@ void Server::UpdateWorld() { void Server::HandleOutput() { //send all information to new connections + //... //selective updates to existing connectons + string s = "hello world"; + for (auto it : sockVec) { + it->Send(s.c_str(), s.length()); + } } diff --git a/server/server.hpp b/server/server.hpp index 25c2ad8..13f4736 100644 --- a/server/server.hpp +++ b/server/server.hpp @@ -2,6 +2,9 @@ #define SERVER_HPP_ #include "config_utility.hpp" +#include "network.hpp" + +#include class Server { public: @@ -18,6 +21,8 @@ public: private: bool running; ConfigUtility config; + TCPServerSocket servSock; + std::vector sockVec; }; #endif diff --git a/server/unit.cpp b/server/unit.cpp index e69de29..a87a571 100644 --- a/server/unit.cpp +++ b/server/unit.cpp @@ -0,0 +1,22 @@ +#include "network.hpp" + +#include + +using namespace std; + +//receive any amount of info and print it + +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; +} \ No newline at end of file diff --git a/server/vector2.hpp b/server/vector2.hpp new file mode 100644 index 0000000..1cd36aa --- /dev/null +++ b/server/vector2.hpp @@ -0,0 +1,76 @@ +/* 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 VECTOR2_HPP_ +#define VECTOR2_HPP_ + +#include +#include + +class Vector2 { +public: + double x, y; + Vector2() { + x = y = 0; + } + Vector2(double i, double j) { + x = i; y = j; + } + double Length() const { + return sqrt(x*x+y*y); + } + double SquaredLength() const { + return x*x+y*y; + } + double operator[](size_t i) { + if (i >= 2) + throw(std::runtime_error("Out of range")); + return *(&x+i); + } + //Arithmetic operators + Vector2 operator+(Vector2 v) const { return Vector2(x + v.x, y + v.y); } + Vector2 operator-(Vector2 v) const { return Vector2(x - v.x, y - v.y); } + Vector2 operator*(Vector2 v) const { return Vector2(x * v.x, y * v.y); } + Vector2 operator*(double d) const { return Vector2(x * d, y * d); } + + Vector2 operator/(Vector2 v) { + if (!v.x || !v.y) + throw(std::runtime_error("Divide by zero")); + return Vector2(x / v.x, y / v.y); + } + Vector2 operator/(double d) { + if (!d) + throw(std::runtime_error("Divide by zero")); + return Vector2(x / d, y / d); + } + + bool operator==(Vector2 v) { return (x == v.x && y == v.y); } + bool operator!=(Vector2 v) { return (x != v.x || y != v.y); } + + template Vector2 operator+=(T t) { return *this = *this + t; } + template Vector2 operator-=(T t) { return *this = *this - t; } + template Vector2 operator*=(T t) { return *this = *this * t; } + template Vector2 operator/=(T t) { return *this = *this / t; } + template bool operator==(T t) { return (x == t && y == t); } + template bool operator!=(T t) { return (x != t || y != t); } +}; + +#endif