diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/gameserver.cc | 3 | ||||
-rw-r--r-- | src/core/gameserver.h | 2 | ||||
-rw-r--r-- | src/core/netclient.cc | 30 | ||||
-rw-r--r-- | src/core/netclient.h | 5 | ||||
-rw-r--r-- | src/core/netconnection.cc | 18 | ||||
-rw-r--r-- | src/core/netconnection.h | 2 | ||||
-rw-r--r-- | src/core/netserver.cc | 2 |
7 files changed, 27 insertions, 35 deletions
diff --git a/src/core/gameserver.cc b/src/core/gameserver.cc index add0ca9..dadff8f 100644 --- a/src/core/gameserver.cc +++ b/src/core/gameserver.cc @@ -25,6 +25,7 @@ GameServer::GameServer() : GameInterface() con_print << "Initializing game server...\n"; server_instance = this; server_network = 0; + server_maxplayerid = 1; server_module = Module::preload(); if (!server_module) { @@ -178,6 +179,8 @@ void GameServer::exec(Player *player, std::string const & cmdline) void GameServer::player_connect(Player *player) { + player->player_id = server_maxplayerid++; + std::string message(player->name()); message.append(" connects."); broadcast(message, player); diff --git a/src/core/gameserver.h b/src/core/gameserver.h index fd2021e..2e492c8 100644 --- a/src/core/gameserver.h +++ b/src/core/gameserver.h @@ -69,6 +69,8 @@ private: Module *server_module; static GameServer *server_instance; NetServer *server_network; + + unsigned int server_maxplayerid; }; inline GameServer *server() { return GameServer::instance(); } diff --git a/src/core/netclient.cc b/src/core/netclient.cc index dba6e2b..c7186f5 100644 --- a/src/core/netclient.cc +++ b/src/core/netclient.cc @@ -19,45 +19,31 @@ NetClient::NetClient(std::string host, int port) : client_error = true; client_state = Connecting; - con_print << host << ":" << port << " connects." << std::endl; + con_print << host << ":" << port << " connected." << std::endl; - // Get a socket file descriptor - client_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (client_fd == -1) { - con_warn << "Network client socket() failed!" << std::endl; - abort(); - return; - } - client_host = host; client_port = port; client_addr.sin_family = AF_INET; client_addr.sin_port = htons(port); client_addr.sin_addr.s_addr = inet_addr(host.c_str()); + memset(client_addr.sin_zero, '\0', sizeof(client_addr.sin_zero)); + if (client_addr.sin_addr.s_addr == INADDR_NONE) { con_warn << "Network invalid client address " << host << "!" << std::endl; abort(); return; } - memset(client_addr.sin_zero, '\0', sizeof(client_addr.sin_zero)); - - std::ostringstream osstream; - client_player.player_id = client_fd; - + sendq.clear(); messageblock.clear(); - + client_error = false; } NetClient::~NetClient() { - con_print << host() << ":" << port() << " disconnects." << std::endl; - - if (client_fd) { - ::close(client_fd); - } + con_print << host() << ":" << port() << " disconnected." << std::endl; } void NetClient::abort() @@ -124,7 +110,7 @@ void NetClient::send(std::string const &msg) sendq.append(msg); } -void NetClient::transmit() +void NetClient::transmit(int serverfd) { if (sendq.size() >= BLOCKSIZE) { con_warn << "Outgoing message exceeds " << BLOCKSIZE << " bytes!\n"; @@ -135,7 +121,7 @@ void NetClient::transmit() ssize_t bytes_sent = 0; while (sendq.size() && !error()) { - bytes_sent = sendto(client_fd, sendq.c_str(), sendq.size(), 0, + bytes_sent = sendto(serverfd, sendq.c_str(), sendq.size(), 0, (struct sockaddr *)&client_addr, sizeof(client_addr)); if (bytes_sent < 0) { diff --git a/src/core/netclient.h b/src/core/netclient.h index 2fde530..1a7c716 100644 --- a/src/core/netclient.h +++ b/src/core/netclient.h @@ -55,12 +55,10 @@ public: void retreive(std::string & message); /// transmit buffered outgoing data - void transmit(); + void transmit(int serverfd); inline bool error() const { return client_error; } - inline int fd() const { return client_fd; } - void abort(); enum State {Connecting=0, Connected=1}; @@ -75,7 +73,6 @@ private: int client_port; Player client_player; bool client_error; - int client_fd; std::string messageblock; std::deque<std::string> recvq; diff --git a/src/core/netconnection.cc b/src/core/netconnection.cc index 379df14..e3b4c69 100644 --- a/src/core/netconnection.cc +++ b/src/core/netconnection.cc @@ -17,6 +17,7 @@ namespace core NetConnection::NetConnection() { + timeout = core::application()->time(); } NetConnection::~NetConnection() @@ -61,20 +62,13 @@ void NetConnection::connect(std::string const &to_host, int to_port) server_addr.sin_port = htons(to_port); // FIXME inet_addr can still fail server_addr.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *)serverhostent->h_addr))); + memset(server_addr.sin_zero, '\0', sizeof server_addr.sin_zero); if (server_addr.sin_addr.s_addr == INADDR_NONE) { con_error << "Network invalid address " << to_host << "!" << std::endl; abort(); return; } - memset(server_addr.sin_zero, '\0', sizeof server_addr.sin_zero); - /* - if (::connect(connection_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) != 0) { - //con_error << "Network connect() failed!" << std::endl; - abort(); - return; - } - */ connection_host = to_host; connection_port = to_port; @@ -83,6 +77,7 @@ void NetConnection::connect(std::string const &to_host, int to_port) FD_ZERO(&clientset); FD_SET(fd(), &clientset); + timeout = core::application()->time(); game()->localplayer()->player_dirty = true; } @@ -129,6 +124,7 @@ void NetConnection::receive() memset(recvbuf, '\0', BLOCKSIZE); bytes_received = ::recv(connection_fd, recvbuf, BLOCKSIZE-1, 0); + timeout = core::application()->time(); if (bytes_received == 0) { con_print << "Disconnected."; @@ -172,6 +168,12 @@ void NetConnection::frame(float seconds) int nb = select(fd()+1, &readset, NULL, NULL, &timeout); if (nb == 0) { + /* + if (timout + TIMEOUT < core::application()->time()) { + con_error << "Connection time out\n"; + abort(); + } + */ return; } if (nb == -1) { diff --git a/src/core/netconnection.h b/src/core/netconnection.h index f378afb..dca85c7 100644 --- a/src/core/netconnection.h +++ b/src/core/netconnection.h @@ -92,6 +92,8 @@ private: int connection_port; struct sockaddr_in server_addr; char recvbuf[BLOCKSIZE]; + + float timeout; }; } diff --git a/src/core/netserver.cc b/src/core/netserver.cc index 9f2fea6..a753759 100644 --- a/src/core/netserver.cc +++ b/src/core/netserver.cc @@ -126,7 +126,7 @@ void NetServer::reap() void NetServer::transmit() { for (std::list<NetClient *>::iterator it = clients.begin(); it != clients.end(); it++) { - (*it)->transmit(); + (*it)->transmit(fd()); } } |