Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-03-21 19:25:11 +0000
committerStijn Buys <ingar@osirion.org>2008-03-21 19:25:11 +0000
commit2314c27dd650dc02c0b5bdd3bef75818393a9ab5 (patch)
tree231815c866a1330338d976480284250e99207554 /src/core/netclient.cc
parent7e99fac4552b402034e5fc3e833cbe8c274f95ce (diff)
switched to UDP networking
Diffstat (limited to 'src/core/netclient.cc')
-rw-r--r--src/core/netclient.cc87
1 files changed, 64 insertions, 23 deletions
diff --git a/src/core/netclient.cc b/src/core/netclient.cc
index 0f56bb9..32e0f4b 100644
--- a/src/core/netclient.cc
+++ b/src/core/netclient.cc
@@ -8,31 +8,61 @@
#include <sstream>
#include "sys/sys.h"
-#include "net/net.h"
-#include "core/netclient.h"
+#include "core/net.h"
namespace core
{
-NetClient::NetClient(int clientfd, std::string host, int port) :
- net::TCPClient(clientfd),
+NetClient::NetClient(std::string host, int port) :
client_host(host)
{
- std::ostringstream osstream;
- //osstream << "host << ":" << port;
- osstream << "Player" << clientfd;
- client_player.player_name = osstream.str();
- client_player.player_id = (unsigned int) clientfd;
+ client_error = true;
+ client_state = Connecting;
+
+ con_print << host << ":" << port << " connects." << std::endl;
+ // Get a socket file descriptor
+ client_fd = socket(PF_INET, SOCK_DGRAM, 0);
+ if (client_fd == -1) {
+ con_warn << "Network client socket() failed!" << std::endl;
+ abort();
+ return;
+ }
+
client_host = host;
client_port = port;
- sendq.clear();
+ client_addr.sin_family = AF_INET;
+ client_addr.sin_port = htons(port);
+ client_addr.sin_addr.s_addr = inet_addr(host.c_str());
+ 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.erase();
messageblock.clear();
+
+ client_error = false;
}
NetClient::~NetClient()
{
+ con_print << host() << ":" << port() << " disconnects." << std::endl;
+
+ if (client_fd) {
+ ::close(client_fd);
+ }
+}
+
+void NetClient::abort()
+{
+ client_error = true;
}
std::string NetClient::host() const
@@ -64,10 +94,10 @@ void NetClient::retreive(std::string & message) {
}
// receive data and decode it into lines
-void NetClient::receive()
+void NetClient::receive(char *data)
{
std::string datablock;
- net::TCPClient::receive(datablock);
+ datablock.assign(data);
while(datablock.size() > 0 ) {
// scan the datablock for enters
@@ -75,19 +105,18 @@ void NetClient::receive()
// TODO detect "begin binary block" message for zlib compression
if (messageblock.size() > 0 ) {
recvq.push_back(messageblock);
- messageblock.clear();
+ messageblock.erase();
}
} else {
- if (messageblock.size() < net::FRAMESIZE) {
+ if (messageblock.size() < FRAMESIZE) {
messageblock.append(datablock.substr(0,1));
} else {
- con_warn << "NetClient " << fd() << ": message block overflow" << std::endl;
- messageblock.clear();
+ con_warn << "Incoming message exceeds " << FRAMESIZE << " bytes!\n";
+ messageblock.erase();
}
}
datablock.erase(0,1);
}
- datablock.clear();
}
void NetClient::send(std::string const &msg)
@@ -97,14 +126,26 @@ void NetClient::send(std::string const &msg)
void NetClient::transmit()
{
- while (sendq.size() && valid() && !error()) {
- TCPClient::send(sendq.substr(0, net::FRAMESIZE-1));
- if (sendq.size() < net::FRAMESIZE) {
- sendq.clear();
- } else {
- sendq.erase(0, net::FRAMESIZE-1);
+ if (sendq.size() >= BLOCKSIZE) {
+ con_warn << "Outgoing message exceeds " << BLOCKSIZE << " bytes!\n";
+ sendq.clear();
+ return;
+ }
+
+ ssize_t bytes_sent = 0;
+
+ while (sendq.size() && !error()) {
+ bytes_sent = sendto(client_fd, sendq.c_str(), sendq.size(), 0,
+ (struct sockaddr *)&client_addr, sizeof(client_addr));
+
+ if (bytes_sent < 0) {
+ abort();
+ return;
}
+
+ sendq.erase(0, bytes_sent);
}
+ sendq.clear();
}
}