diff options
Diffstat (limited to 'src/core/netconnection.cc')
-rw-r--r-- | src/core/netconnection.cc | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/src/core/netconnection.cc b/src/core/netconnection.cc index e3b4c69..b640595 100644 --- a/src/core/netconnection.cc +++ b/src/core/netconnection.cc @@ -17,7 +17,8 @@ namespace core NetConnection::NetConnection() { - timeout = core::application()->time(); + connection_timeout = core::application()->time(); + connection_state = Connecting; } NetConnection::~NetConnection() @@ -34,6 +35,7 @@ void NetConnection::connect(std::string const &to_host, int to_port) { connection_error = false; connection_fd = -1; + connection_state = Connecting; if (valid()) return; @@ -47,8 +49,6 @@ void NetConnection::connect(std::string const &to_host, int to_port) return; } - con_print << "Connecting to " << inet_ntoa(*((struct in_addr *)serverhostent->h_addr)) << ":" << to_port << "..." << std::endl; - // Get a socket file descriptor connection_fd = socket(PF_INET, SOCK_DGRAM, 0); if (connection_fd == -1) { @@ -69,7 +69,6 @@ void NetConnection::connect(std::string const &to_host, int to_port) return; } - connection_host = to_host; connection_port = to_port; connection_error = false; @@ -77,8 +76,12 @@ void NetConnection::connect(std::string const &to_host, int to_port) FD_ZERO(&clientset); FD_SET(fd(), &clientset); - timeout = core::application()->time(); + connection_timeout = application()->time(); + connection_keepalive = application()->time(); + game()->localplayer()->player_dirty = true; + + con_print << "Connecting to " << inet_ntoa(*((struct in_addr *)serverhostent->h_addr)) << ":" << to_port << "..." << std::endl; } void NetConnection::disconnect() @@ -96,6 +99,7 @@ void NetConnection::disconnect() connection_error = false; connection_host.clear(); connection_port = 0; + connection_state = Connecting; } bool NetConnection::has_messages() const @@ -124,7 +128,7 @@ void NetConnection::receive() memset(recvbuf, '\0', BLOCKSIZE); bytes_received = ::recv(connection_fd, recvbuf, BLOCKSIZE-1, 0); - timeout = core::application()->time(); + connection_timeout = core::application()->time(); if (bytes_received == 0) { con_print << "Disconnected."; @@ -168,12 +172,10 @@ 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"; + if (connection_timeout + NETTIMEOUT < core::application()->time()) { + con_error << "Connection timeout!\n"; abort(); } - */ return; } if (nb == -1) { @@ -213,8 +215,14 @@ void NetConnection::transmit() if (error() || invalid()) return; - - if (sendq.size() > FRAMESIZE) { + + if (!sendq.size()) { + if (connection_keepalive + NETTIMEOUT /2 < application()->time()) { + sendq.assign("ping\n"); + } else { + return; + } + } else if (sendq.size() > FRAMESIZE) { con_warn << "Outgoing message exceeds " << FRAMESIZE << " bytes!\n"; //sendq.clear(); //return; @@ -234,14 +242,16 @@ void NetConnection::transmit() // assert (bytes_sent <= sendbuf.size()); sendq.erase(0, bytes_sent); } - - sendq.clear(); + + connection_keepalive = application()->time(); } // parse incoming client messages /** * The following incoming messages are parsed; * + * connect + * disconnect * msg info <text> * msg public <name> <text> * die @@ -271,6 +281,19 @@ void NetConnection::parse_incoming_message(const std::string & message) } } + } else if (command == "connect") { + + connection_state = Connected; + con_print << "Connected." << std::endl; + return; + + } else if (command == "disconnect") { + + con_error << "Server disconnected!" << std::endl; + abort(); + + } else if (command == "ping") { + } else if (command == "die") { unsigned int id; msgstream >> id; @@ -312,9 +335,9 @@ void NetConnection::parse_incoming_message(const std::string & message) } else entity->recieve_server_update(msgstream); } - return; } else if (command == "pif") { + connection()->localplayer()->recieve_server_update(msgstream); } |