2007-01-02 17:34:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-02-23 08:37:33 +00:00
|
|
|
/**
|
|
|
|
* @file tcp.cpp Basic functions to receive and send TCP packets.
|
|
|
|
*/
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
|
|
|
#include "../../stdafx.h"
|
|
|
|
#include "../../debug.h"
|
|
|
|
#include "../../openttd.h"
|
|
|
|
#include "../../variables.h"
|
|
|
|
|
2008-05-30 18:20:26 +00:00
|
|
|
#include "../network_internal.h"
|
2007-01-04 21:21:14 +00:00
|
|
|
#include "packet.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "tcp.h"
|
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2008-12-23 20:52:27 +00:00
|
|
|
#include "../../oldpool_func.h"
|
2008-01-13 01:21:35 +00:00
|
|
|
|
2008-12-23 20:52:27 +00:00
|
|
|
/** Make very sure the preconditions given in network_type.h are actually followed */
|
|
|
|
assert_compile(MAX_CLIENT_SLOTS == (MAX_CLIENT_SLOTS >> NCI_BITS_PER_POOL_BLOCK) << NCI_BITS_PER_POOL_BLOCK);
|
|
|
|
assert_compile(MAX_CLIENT_SLOTS > MAX_CLIENTS);
|
2007-01-12 20:19:49 +00:00
|
|
|
|
2008-12-23 20:52:27 +00:00
|
|
|
typedef ClientIndex NetworkClientSocketID;
|
|
|
|
DEFINE_OLD_POOL_GENERIC(NetworkClientSocket, NetworkClientSocket);
|
2007-01-12 20:19:49 +00:00
|
|
|
|
2008-12-23 20:52:27 +00:00
|
|
|
NetworkClientSocket::NetworkClientSocket(ClientID client_id)
|
|
|
|
{
|
|
|
|
this->sock = INVALID_SOCKET;
|
|
|
|
this->client_id = client_id;
|
2007-01-12 20:19:49 +00:00
|
|
|
this->status = STATUS_INACTIVE;
|
|
|
|
}
|
|
|
|
|
2008-12-23 20:52:27 +00:00
|
|
|
NetworkClientSocket::~NetworkClientSocket()
|
2007-02-01 23:50:15 +00:00
|
|
|
{
|
2008-12-23 20:52:27 +00:00
|
|
|
if (this->sock != INVALID_SOCKET) closesocket(this->sock);
|
2007-02-01 23:50:15 +00:00
|
|
|
this->writable = false;
|
|
|
|
this->has_quit = true;
|
|
|
|
|
|
|
|
/* Free all pending and partially received packets */
|
|
|
|
while (this->packet_queue != NULL) {
|
|
|
|
Packet *p = this->packet_queue->next;
|
|
|
|
delete this->packet_queue;
|
|
|
|
this->packet_queue = p;
|
|
|
|
}
|
|
|
|
delete this->packet_recv;
|
|
|
|
this->packet_recv = NULL;
|
|
|
|
|
|
|
|
while (this->command_queue != NULL) {
|
|
|
|
CommandPacket *p = this->command_queue->next;
|
|
|
|
free(this->command_queue);
|
|
|
|
this->command_queue = p;
|
|
|
|
}
|
2008-12-23 20:52:27 +00:00
|
|
|
|
|
|
|
this->sock = INVALID_SOCKET;
|
|
|
|
this->client_id = INVALID_CLIENT_ID;
|
|
|
|
this->status = STATUS_INACTIVE;
|
2007-02-01 23:50:15 +00:00
|
|
|
}
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
/**
|
|
|
|
* Functions to help NetworkRecv_Packet/NetworkSend_Packet a bit
|
|
|
|
* A socket can make errors. When that happens this handles what to do.
|
|
|
|
* For clients: close connection and drop back to main-menu
|
|
|
|
* For servers: close connection and that is it
|
|
|
|
* @return the new status
|
2007-01-12 20:19:49 +00:00
|
|
|
* TODO: needs to be splitted when using client and server socket packets
|
2007-01-02 17:34:03 +00:00
|
|
|
*/
|
2008-12-23 09:47:42 +00:00
|
|
|
NetworkRecvStatus NetworkClientSocket::CloseConnection()
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
|
|
|
/* Clients drop back to the main menu */
|
|
|
|
if (!_network_server && _networking) {
|
|
|
|
_switch_mode = SM_MENU;
|
|
|
|
_networking = false;
|
2008-04-18 10:16:51 +00:00
|
|
|
extern StringID _switch_mode_errorstr;
|
2007-01-02 17:34:03 +00:00
|
|
|
_switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION;
|
|
|
|
|
|
|
|
return NETWORK_RECV_STATUS_CONN_LOST;
|
|
|
|
}
|
|
|
|
|
2008-12-28 21:45:41 +00:00
|
|
|
NetworkCloseClient(this);
|
2007-01-02 17:34:03 +00:00
|
|
|
return NETWORK_RECV_STATUS_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function puts the packet in the send-queue and it is send as
|
|
|
|
* soon as possible. This is the next tick, or maybe one tick later
|
|
|
|
* if the OS-network-buffer is full)
|
|
|
|
* @param packet the packet to send
|
|
|
|
*/
|
2008-12-23 09:47:42 +00:00
|
|
|
void NetworkClientSocket::Send_Packet(Packet *packet)
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
|
|
|
Packet *p;
|
|
|
|
assert(packet != NULL);
|
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
packet->PrepareToSend();
|
2007-01-02 17:34:03 +00:00
|
|
|
|
|
|
|
/* Locate last packet buffered for the client */
|
2007-02-01 23:50:15 +00:00
|
|
|
p = this->packet_queue;
|
2007-01-02 17:34:03 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
/* No packets yet */
|
2007-02-01 23:50:15 +00:00
|
|
|
this->packet_queue = packet;
|
2007-01-02 17:34:03 +00:00
|
|
|
} else {
|
|
|
|
/* Skip to the last packet */
|
|
|
|
while (p->next != NULL) p = p->next;
|
|
|
|
p->next = packet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends all the buffered packets out for this client. It stops when:
|
|
|
|
* 1) all packets are send (queue is empty)
|
|
|
|
* 2) the OS reports back that it can not send any more
|
|
|
|
* data right now (full network-buffer, it happens ;))
|
|
|
|
* 3) sending took too long
|
|
|
|
*/
|
2008-12-23 09:47:42 +00:00
|
|
|
bool NetworkClientSocket::Send_Packets()
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
|
|
|
ssize_t res;
|
|
|
|
Packet *p;
|
|
|
|
|
|
|
|
/* We can not write to this socket!! */
|
2007-02-01 23:50:15 +00:00
|
|
|
if (!this->writable) return false;
|
|
|
|
if (!this->IsConnected()) return false;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 23:50:15 +00:00
|
|
|
p = this->packet_queue;
|
2007-01-02 17:34:03 +00:00
|
|
|
while (p != NULL) {
|
2007-02-01 23:50:15 +00:00
|
|
|
res = send(this->sock, (const char*)p->buffer + p->pos, p->size - p->pos, 0);
|
2007-01-02 17:34:03 +00:00
|
|
|
if (res == -1) {
|
|
|
|
int err = GET_LAST_ERROR();
|
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
|
/* Something went wrong.. close client! */
|
|
|
|
DEBUG(net, 0, "send failed with error %d", err);
|
2007-02-01 23:50:15 +00:00
|
|
|
this->CloseConnection();
|
2007-01-02 17:34:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
/* Client/server has left us :( */
|
2007-02-01 23:50:15 +00:00
|
|
|
this->CloseConnection();
|
2007-01-02 17:34:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->pos += res;
|
|
|
|
|
|
|
|
/* Is this packet sent? */
|
|
|
|
if (p->pos == p->size) {
|
|
|
|
/* Go to the next packet */
|
2007-02-01 23:50:15 +00:00
|
|
|
this->packet_queue = p->next;
|
2007-02-01 22:30:35 +00:00
|
|
|
delete p;
|
2007-02-01 23:50:15 +00:00
|
|
|
p = this->packet_queue;
|
2007-01-02 17:34:03 +00:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2008-12-23 09:47:42 +00:00
|
|
|
Packet *NetworkClientSocket::Recv_Packet(NetworkRecvStatus *status)
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
|
|
|
ssize_t res;
|
|
|
|
Packet *p;
|
|
|
|
|
|
|
|
*status = NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
2007-02-01 23:50:15 +00:00
|
|
|
if (!this->IsConnected()) return NULL;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 23:50:15 +00:00
|
|
|
if (this->packet_recv == NULL) {
|
|
|
|
this->packet_recv = new Packet(this);
|
|
|
|
if (this->packet_recv == NULL) error("Failed to allocate packet");
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
2007-02-01 23:50:15 +00:00
|
|
|
p = this->packet_recv;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
|
|
|
/* Read packet size */
|
|
|
|
if (p->pos < sizeof(PacketSize)) {
|
|
|
|
while (p->pos < sizeof(PacketSize)) {
|
2007-01-12 20:19:49 +00:00
|
|
|
/* Read the size of the packet */
|
2007-02-01 23:50:15 +00:00
|
|
|
res = recv(this->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
|
2007-01-02 17:34:03 +00:00
|
|
|
if (res == -1) {
|
|
|
|
int err = GET_LAST_ERROR();
|
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
|
/* Something went wrong... (104 is connection reset by peer) */
|
|
|
|
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
|
2007-02-01 23:50:15 +00:00
|
|
|
*status = this->CloseConnection();
|
2007-01-02 17:34:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Connection would block, so stop for now */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
/* Client/server has left */
|
2007-02-01 23:50:15 +00:00
|
|
|
*status = this->CloseConnection();
|
2007-01-02 17:34:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p->pos += res;
|
|
|
|
}
|
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
/* Read the packet size from the received packet */
|
|
|
|
p->ReadRawPacketSize();
|
2007-01-02 17:34:03 +00:00
|
|
|
|
|
|
|
if (p->size > SEND_MTU) {
|
2007-02-01 23:50:15 +00:00
|
|
|
*status = this->CloseConnection();
|
2007-01-02 17:34:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read rest of packet */
|
|
|
|
while (p->pos < p->size) {
|
2007-02-01 23:50:15 +00:00
|
|
|
res = recv(this->sock, (char*)p->buffer + p->pos, p->size - p->pos, 0);
|
2007-01-02 17:34:03 +00:00
|
|
|
if (res == -1) {
|
|
|
|
int err = GET_LAST_ERROR();
|
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
|
/* Something went wrong... (104 is connection reset by peer) */
|
|
|
|
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
|
2007-02-01 23:50:15 +00:00
|
|
|
*status = this->CloseConnection();
|
2007-01-02 17:34:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Connection would block */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
/* Client/server has left */
|
2007-02-01 23:50:15 +00:00
|
|
|
*status = this->CloseConnection();
|
2007-01-02 17:34:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->pos += res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare for receiving a new packet */
|
2007-02-01 23:50:15 +00:00
|
|
|
this->packet_recv = NULL;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 22:30:35 +00:00
|
|
|
p->PrepareToRead();
|
2007-01-02 17:34:03 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-12-23 09:47:42 +00:00
|
|
|
bool NetworkClientSocket::IsPacketQueueEmpty()
|
2007-02-01 23:50:15 +00:00
|
|
|
{
|
|
|
|
return this->packet_queue == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|