File diff r17621:78f5210393b8 → r17622:d9f6f6845b5d
src/network/core/tcp.cpp
Show inline comments
 
@@ -9,24 +9,28 @@
 

	
 
/**
 
 * @file tcp.cpp Basic functions to receive and send TCP packets.
 
 */
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
#include "../../stdafx.h"
 
#include "../../debug.h"
 

	
 
#include "tcp.h"
 

	
 
/**
 
 * Construct a socket handler for a TCP connection.
 
 * @param s The just opened TCP connection.
 
 */
 
NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) :
 
		NetworkSocketHandler(),
 
		packet_queue(NULL), packet_recv(NULL),
 
		sock(s), writable(false)
 
{
 
}
 

	
 
NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
 
{
 
	this->CloseConnection();
 

	
 
	if (this->sock != INVALID_SOCKET) closesocket(this->sock);
 
@@ -129,26 +133,25 @@ SendPacketsState NetworkTCPSocketHandler
 
			delete p;
 
			p = this->packet_queue;
 
		} else {
 
			return SPS_PARTLY_SENT;
 
		}
 
	}
 

	
 
	return SPS_ALL_SENT;
 
}
 

	
 
/**
 
 * Receives a packet for the given client
 
 * @param status the variable to store the status into
 
 * @return the received packet (or NULL when it didn't receive one)
 
 * @return The received packet (or NULL when it didn't receive one)
 
 */
 
Packet *NetworkTCPSocketHandler::ReceivePacket()
 
{
 
	ssize_t res;
 

	
 
	if (!this->IsConnected()) return NULL;
 

	
 
	if (this->packet_recv == NULL) {
 
		this->packet_recv = new Packet(this);
 
	}
 

	
 
	Packet *p = this->packet_recv;
 
@@ -210,25 +213,25 @@ Packet *NetworkTCPSocketHandler::Receive
 
	}
 

	
 
	/* Prepare for receiving a new packet */
 
	this->packet_recv = NULL;
 

	
 
	p->PrepareToRead();
 
	return p;
 
}
 

	
 
/**
 
 * Check whether this socket can send or receive something.
 
 * @return \c true when there is something to receive.
 
 * @note Sets #writeable if more data can be sent.
 
 * @note Sets #writable if more data can be sent.
 
 */
 
bool NetworkTCPSocketHandler::CanSendReceive()
 
{
 
	fd_set read_fd, write_fd;
 
	struct timeval tv;
 

	
 
	FD_ZERO(&read_fd);
 
	FD_ZERO(&write_fd);
 

	
 
	FD_SET(this->sock, &read_fd);
 
	FD_SET(this->sock, &write_fd);