Skip to content

Commit

Permalink
Network|Fixed: Basic network communications via de::Socket
Browse files Browse the repository at this point in the history
Also, cleaned up console messages printed by the network subsystem
so that they are more user-friendly.
  • Loading branch information
skyjake committed Mar 27, 2012
1 parent 5aa93c3 commit acea7ab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
56 changes: 27 additions & 29 deletions doomsday/engine/portable/src/sys_network.c
Expand Up @@ -155,7 +155,7 @@ boolean N_InitService(boolean inServerMode)
{
port = (!nptIPPort ? defaultTCPPort : nptIPPort);

Con_Message("N_InitService: Listening TCP socket on port %i.\n", port);
Con_Message("Listening on TCP port %i.\n", port);

// Open a listening TCP socket. It will accept client connections.
if(!(serverSock = LegacyNetwork_OpenServerSocket(port)))
Expand Down Expand Up @@ -347,9 +347,7 @@ static boolean N_RegisterNewSocket(int sock)
// Add this socket to the set of client sockets.
LegacyNetwork_SocketSet_Add(sockSet, sock);

#ifdef _DEBUG
Con_Message("N_RegisterNewSocket: Socket %i as node %i.\n", sock, i);
#endif
DEBUG_VERBOSE2_Message(("N_RegisterNewSocket: Socket #%i registered as node %i.\n", sock, i));

found = true;
}
Expand Down Expand Up @@ -379,7 +377,7 @@ static boolean N_JoinNode(nodeid_t id, /*Uint16 port,*/ const char *name)
{
char buf[80];
N_IPToString(buf, &node->addr);
Con_Message("N_JoinNode: Node %i listens at %s.\n", id, buf);
Con_Message("Node %i listens at %s.\n", id, buf);
}

// Convert the network node into a real client node.
Expand Down Expand Up @@ -437,7 +435,7 @@ void N_ClientHandleResponseToInfoQuery(int nodeId, const byte *data, int size)
else
{
located.valid = false;
Con_Message("N_ClientHandleResponseToInfoQuery: Reply from %s (port %i) was invalid.\n",
Con_Message("Reply from %s (port %i) was invalid.\n",
svNode->addr.host, svNode->addr.port);
}

Expand Down Expand Up @@ -470,14 +468,14 @@ boolean N_LookForHosts(const char *address, int port, expectedresponder_t respon
svNode->sock = LegacyNetwork_Open(located.addr.host, located.addr.port);
if(!svNode->sock)
{
Con_Message("N_LookForHosts: No reply from %s (port %i).\n", address, port);
Con_Message("No reply from %s (port %i).\n", address, port);
memset(svNode, 0, sizeof(*svNode));
return false;
}

// Send an INFO query.
LegacyNetwork_Send(svNode->sock, "Info?", 5);
Con_Message("N_LookForHosts: Sent info query to %s (port %i).\n", address, port);
Con_Message("Sent info query to %s (port %i).\n", address, port);

svNode->expectedResponder = (responder? responder : N_ClientHandleResponseToInfoQuery);
return true;
Expand All @@ -493,7 +491,7 @@ void N_ClientHandleResponseToJoin(int nodeId, const byte* data, int size)

if(size < 5 || strncmp(buf, "Enter", 5))
{
Con_Message("N_Connect: Server refused connection (received %i bytes).\n", size);
Con_Message("Server refused connection (received %i bytes).\n", size);
N_Disconnect();
return;
}
Expand Down Expand Up @@ -548,7 +546,7 @@ boolean N_Connect(int index)
if(!(svNode->sock = LegacyNetwork_Open(host->addr.host, host->addr.port)))
{
N_IPToString(buf, &host->addr);
Con_Message("N_Connect: No reply from %s.\n", buf);
Con_Message("No reply from %s.\n", buf);
return false;
}
memcpy(&svNode->addr, &located.addr, sizeof(ipaddress_t));
Expand Down Expand Up @@ -603,20 +601,20 @@ boolean N_ServerOpen(void)
{
if(!isDedicated)
{
Con_Message("N_ServerOpen: Server can only be started in dedicated mode! (run with -dedicated)\n");
Con_Message("Server can only be started in dedicated mode! (run with -dedicated)\n");
return false;
}

if(!N_IsAvailable())
return false;
/*if(!N_IsAvailable())
return false;*/

Demo_StopPlayback();

// Let's make sure the correct service provider is initialized
// in server mode.
if(!N_InitService(true))
{
Con_Message("N_ServerOpen: Failed to initialize server mode.\n");
Con_Message("Failed to initialize server mode.\n");
return false;
}

Expand Down Expand Up @@ -691,9 +689,9 @@ static boolean N_ServerHandleNodeRequest(nodeid_t node, const char *command, int
Str_Init(&msg);
Str_Appendf(&msg, "Info\n");
Sv_InfoToString(&info, &msg);
#ifdef _DEBUG
Con_Message("N_ServerHandleNodeRequest: Sending: %s\n", Str_Text(&msg));
#endif

DEBUG_VERBOSE_Message(("N_ServerHandleNodeRequest: Sending: %s\n", Str_Text(&msg)));

LegacyNetwork_Send(sock, Str_Text(&msg), (int) Str_Length(&msg));
Str_Free(&msg);
}
Expand Down Expand Up @@ -762,22 +760,22 @@ void N_ServerListenUnjoinedNodes(void)
netnode_t* node = netNodes + i;
if(node->hasJoined || !node->sock) continue;

if(LegacyNetwork_IsDisconnected(node->sock))
{
// Close this socket & node.
Con_Message("N_ServerListenUnjoinedNodes: Connection closed on node %i.\n", i);
N_TerminateNode(i);
continue;
}

// Does this socket have got any activity?
if(LegacyNetwork_BytesReady(node->sock))
// Does this socket have incoming messages?
while(node->sock && LegacyNetwork_BytesReady(node->sock))
{
int size = 0;
byte* message = LegacyNetwork_Receive(node->sock, &size);
N_ServerHandleNodeRequest(i, (const char*) message, size);
LegacyNetwork_FreeBuffer(message);
}

if(node->sock && LegacyNetwork_IsDisconnected(node->sock))
{
// Close this socket & node.
Con_Message("Connection to client closed on node %i.\n", i);
N_TerminateNode(i);
continue;
}
}
}
}
Expand Down Expand Up @@ -814,14 +812,14 @@ void N_ClientListenUnjoined(void)
}
else
{
Con_Message("N_ClientListenUnjoinNodes: Unexpected message from server (%i bytes), ignoring.\n", size);
Con_Message("Unexpected message from server (%i bytes), ignoring.\n", size);
}
LegacyNetwork_FreeBuffer(data);
}
else
{
// An error!
Con_Message("N_ClientListenUnjoinedNodes: Connection closed.\n");
Con_Message("Connection closed.\n");
N_Disconnect();
}
}
Expand Down
10 changes: 5 additions & 5 deletions doomsday/libdeng2/src/legacy/legacynetwork.cpp
Expand Up @@ -220,15 +220,15 @@ bool LegacyNetwork::checkSetForActivity(int set)

foreach(Socket* sock, d->sets[set].members)
{
if(!sock->isOpen())
if(sock->hasIncoming())
{
// Closed sockets as reported as activity so that they can be removed
// from the set by the caller.
// There are incoming messages ready for reading.
return true;
}
if(sock->hasIncoming())
if(!sock->isOpen())
{
// There are incoming messages ready for reading.
// Closed sockets as reported as activity so that they can be removed
// from the set by the caller.
return true;
}
}
Expand Down
8 changes: 6 additions & 2 deletions doomsday/libdeng2/src/net/socket.cpp
Expand Up @@ -309,6 +309,9 @@ struct Socket::Instance
Reader reader(receivedBytes);
reader >> incomingHeader;
receptionState = ReceivingPayload;

// Remove the read bytes from the buffer.
receivedBytes.remove(0, reader.offset());
}
catch(const de::Error&)
{
Expand Down Expand Up @@ -393,6 +396,9 @@ Socket::Socket(QTcpSocket* existingSocket)
d = new Instance;
d->socket = existingSocket;
initialize();

// Maybe we missed an earlier signal, since we are only now getting ownership.
readIncomingBytes();
}

Socket::~Socket()
Expand Down Expand Up @@ -545,8 +551,6 @@ dsize Socket::bytesBuffered() const

void Socket::bytesWereWritten(qint64 bytes)
{
qDebug() << "Socket:" << bytes << "were written to" << d->socket->peerAddress().toString();

d->bytesToBeWritten -= bytes;
DENG2_ASSERT(d->bytesToBeWritten >= 0);
}

0 comments on commit acea7ab

Please sign in to comment.