tron@2186: /* $Id$ */ tron@2186: Darkvater@4826: #ifdef ENABLE_NETWORK Darkvater@4826: truelight@543: #include "stdafx.h" tron@1299: #include "debug.h" truelight@543: #include "network_data.h" tron@2163: #include "functions.h" tron@1820: #include "string.h" truelight@543: #include "table/strings.h" truelight@543: #include "network_client.h" truelight@543: #include "command.h" truelight@543: #include "callback_table.h" tron@2153: #include "variables.h" truelight@543: truelight@543: // This files handles the send/receive of all packets truelight@543: truelight@543: // Create a packet for sending truelight@543: Packet *NetworkSend_Init(PacketType type) truelight@543: { truelight@543: Packet *packet = malloc(sizeof(Packet)); truelight@543: // An error is inplace here, because it simply means we ran out of memory. truelight@543: if (packet == NULL) error("Failed to allocate Packet"); truelight@543: truelight@543: // Skip the size so we can write that in before sending the packet truelight@543: packet->size = sizeof(packet->size); truelight@543: packet->buffer[packet->size++] = type; truelight@543: packet->pos = 0; truelight@543: truelight@543: return packet; truelight@543: } truelight@543: truelight@543: // The next couple of functions make sure we can send truelight@543: // uint8, uint16, uint32 and uint64 endian-safe truelight@543: // over the network. The order it uses is: truelight@543: // truelight@543: // 1 2 3 4 truelight@543: // truelight@543: truelight@543: void NetworkSend_uint8(Packet *packet, uint8 data) truelight@543: { truelight@543: assert(packet->size < sizeof(packet->buffer) - sizeof(data)); tron@2644: packet->buffer[packet->size++] = data; truelight@543: } truelight@543: truelight@543: void NetworkSend_uint16(Packet *packet, uint16 data) truelight@543: { truelight@543: assert(packet->size < sizeof(packet->buffer) - sizeof(data)); tron@2140: packet->buffer[packet->size++] = GB(data, 0, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 8, 8); truelight@543: } truelight@543: truelight@543: void NetworkSend_uint32(Packet *packet, uint32 data) truelight@543: { truelight@543: assert(packet->size < sizeof(packet->buffer) - sizeof(data)); tron@2140: packet->buffer[packet->size++] = GB(data, 0, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 8, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 16, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 24, 8); truelight@543: } truelight@543: truelight@543: void NetworkSend_uint64(Packet *packet, uint64 data) truelight@543: { truelight@543: assert(packet->size < sizeof(packet->buffer) - sizeof(data)); tron@2140: packet->buffer[packet->size++] = GB(data, 0, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 8, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 16, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 24, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 32, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 40, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 48, 8); tron@2140: packet->buffer[packet->size++] = GB(data, 56, 8); truelight@543: } truelight@543: truelight@543: // Sends a string over the network. It sends out truelight@543: // the string + '\0'. No size-byte or something. truelight@543: void NetworkSend_string(Packet *packet, const char* data) truelight@543: { truelight@543: assert(data != NULL); truelight@543: assert(packet->size < sizeof(packet->buffer) - strlen(data) - 1); truelight@543: while ((packet->buffer[packet->size++] = *data++) != '\0') {} truelight@543: } truelight@543: truelight@543: // If PacketSize changes of size, you have to change the 2 packet->size truelight@543: // lines below matching the size of packet->size/PacketSize! truelight@543: // (line 'packet->buffer[0] = packet->size & 0xFF;' and below) truelight@543: assert_compile(sizeof(PacketSize) == 2); truelight@543: truelight@543: // This function puts the packet in the send-queue and it is send truelight@543: // as soon as possible truelight@543: // (that is: the next tick, or maybe one tick later if the truelight@543: // OS-network-buffer is full) truelight@716: void NetworkSend_Packet(Packet *packet, NetworkClientState *cs) truelight@543: { truelight@543: Packet *p; truelight@543: assert(packet != NULL); truelight@543: truelight@543: packet->pos = 0; truelight@543: packet->next = NULL; truelight@543: tron@2989: packet->buffer[0] = GB(packet->size, 0, 8); tron@2989: packet->buffer[1] = GB(packet->size, 8, 8); truelight@543: truelight@543: // Locate last packet buffered for the client truelight@543: p = cs->packet_queue; truelight@543: if (p == NULL) { truelight@543: // No packets yet truelight@543: cs->packet_queue = packet; truelight@543: } else { truelight@543: // Skip to the last packet truelight@543: while (p->next != NULL) p = p->next; truelight@543: p->next = packet; truelight@543: } truelight@543: } truelight@543: truelight@543: // Functions to help NetworkRecv_Packet/NetworkSend_Packet a bit truelight@543: // A socket can make errors. When that happens truelight@543: // this handles what to do. truelight@543: // For clients: close connection and drop back to main-menu truelight@543: // For servers: close connection and that is it tron@1095: static NetworkRecvStatus CloseConnection(NetworkClientState *cs) truelight@543: { truelight@716: NetworkCloseClient(cs); truelight@543: truelight@543: // Clients drop back to the main menu truelight@3547: if (!_network_server && _networking) { truelight@543: _switch_mode = SM_MENU; truelight@543: _networking = false; truelight@543: _switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION; truelight@543: truelight@543: return NETWORK_RECV_STATUS_CONN_LOST; truelight@543: } truelight@543: truelight@543: return NETWORK_RECV_STATUS_OKAY; truelight@543: } truelight@543: truelight@543: // Sends all the buffered packets out for this client truelight@543: // it stops when: truelight@543: // 1) all packets are send (queue is empty) truelight@543: // 2) the OS reports back that it can not send any more truelight@543: // data right now (full network-buffer, it happens ;)) truelight@543: // 3) sending took too long truelight@716: bool NetworkSend_Packets(NetworkClientState *cs) truelight@543: { truelight@543: ssize_t res; truelight@543: Packet *p; truelight@543: truelight@543: // We can not write to this socket!! truelight@543: if (!cs->writable) return false; truelight@543: if (cs->socket == INVALID_SOCKET) return false; truelight@543: truelight@543: p = cs->packet_queue; truelight@543: while (p != NULL) { truelight@543: res = send(cs->socket, p->buffer + p->pos, p->size - p->pos, 0); truelight@543: if (res == -1) { truelight@543: int err = GET_LAST_ERROR(); Darkvater@5568: if (err != EWOULDBLOCK) { // Something went wrong.. close client! Darkvater@5568: DEBUG(net, 0, "send failed with error %d", err); truelight@543: CloseConnection(cs); truelight@543: return false; truelight@543: } truelight@543: return true; truelight@543: } truelight@543: if (res == 0) { truelight@543: // Client/server has left us :( truelight@543: CloseConnection(cs); truelight@543: return false; truelight@543: } truelight@543: truelight@543: p->pos += res; truelight@543: truelight@543: // Is this packet sent? truelight@543: if (p->pos == p->size) { truelight@543: // Go to the next packet truelight@543: cs->packet_queue = p->next; truelight@543: free(p); truelight@543: p = cs->packet_queue; tron@4077: } else { truelight@543: return true; tron@4077: } truelight@543: } truelight@543: truelight@543: return true; truelight@543: } truelight@543: truelight@543: truelight@543: // Receiving commands truelight@543: // Again, the next couple of functions are endian-safe truelight@543: // see the comment around NetworkSend_uint8 for more info. truelight@903: uint8 NetworkRecv_uint8(NetworkClientState *cs, Packet *packet) truelight@543: { truelight@903: /* Don't allow reading from a closed socket */ Darkvater@4880: if (cs->has_quit) return 0; truelight@903: truelight@903: /* Check if variable is within packet-size */ truelight@903: if (packet->pos + 1 > packet->size) { truelight@903: CloseConnection(cs); truelight@903: return 0; truelight@903: } truelight@903: truelight@543: return packet->buffer[packet->pos++]; truelight@543: } truelight@543: truelight@903: uint16 NetworkRecv_uint16(NetworkClientState *cs, Packet *packet) truelight@543: { truelight@543: uint16 n; truelight@903: truelight@903: /* Don't allow reading from a closed socket */ Darkvater@4880: if (cs->has_quit) return 0; truelight@903: truelight@903: /* Check if variable is within packet-size */ truelight@903: if (packet->pos + 2 > packet->size) { truelight@903: CloseConnection(cs); truelight@903: return 0; truelight@903: } truelight@903: truelight@543: n = (uint16)packet->buffer[packet->pos++]; truelight@543: n += (uint16)packet->buffer[packet->pos++] << 8; truelight@543: return n; truelight@543: } truelight@543: truelight@903: uint32 NetworkRecv_uint32(NetworkClientState *cs, Packet *packet) truelight@543: { truelight@543: uint32 n; truelight@903: truelight@903: /* Don't allow reading from a closed socket */ Darkvater@4880: if (cs->has_quit) return 0; truelight@903: truelight@903: /* Check if variable is within packet-size */ truelight@903: if (packet->pos + 4 > packet->size) { truelight@903: CloseConnection(cs); truelight@903: return 0; truelight@903: } truelight@903: truelight@543: n = (uint32)packet->buffer[packet->pos++]; truelight@543: n += (uint32)packet->buffer[packet->pos++] << 8; truelight@543: n += (uint32)packet->buffer[packet->pos++] << 16; truelight@543: n += (uint32)packet->buffer[packet->pos++] << 24; truelight@543: return n; truelight@543: } truelight@543: truelight@903: uint64 NetworkRecv_uint64(NetworkClientState *cs, Packet *packet) truelight@543: { truelight@543: uint64 n; truelight@903: truelight@903: /* Don't allow reading from a closed socket */ Darkvater@4880: if (cs->has_quit) return 0; truelight@903: truelight@903: /* Check if variable is within packet-size */ truelight@903: if (packet->pos + 8 > packet->size) { truelight@903: CloseConnection(cs); truelight@903: return 0; truelight@903: } truelight@903: truelight@543: n = (uint64)packet->buffer[packet->pos++]; truelight@543: n += (uint64)packet->buffer[packet->pos++] << 8; truelight@543: n += (uint64)packet->buffer[packet->pos++] << 16; truelight@543: n += (uint64)packet->buffer[packet->pos++] << 24; truelight@543: n += (uint64)packet->buffer[packet->pos++] << 32; truelight@543: n += (uint64)packet->buffer[packet->pos++] << 40; truelight@543: n += (uint64)packet->buffer[packet->pos++] << 48; truelight@543: n += (uint64)packet->buffer[packet->pos++] << 56; truelight@543: return n; truelight@543: } truelight@543: truelight@543: // Reads a string till it finds a '\0' in the stream Darkvater@3457: void NetworkRecv_string(NetworkClientState *cs, Packet *p, char *buffer, size_t size) truelight@543: { Darkvater@3457: PacketSize pos; Darkvater@3456: char *bufp = buffer; truelight@903: truelight@903: /* Don't allow reading from a closed socket */ Darkvater@4880: if (cs->has_quit) return; truelight@903: truelight@543: pos = p->pos; truelight@543: while (--size > 0 && pos < p->size && (*buffer++ = p->buffer[pos++]) != '\0') {} Darkvater@3457: Darkvater@3457: if (size == 0 || pos == p->size) { truelight@543: *buffer = '\0'; truelight@543: // If size was sooner to zero then the string in the stream truelight@543: // skip till the \0, so the packet can be read out correctly for the rest Darkvater@3457: while (pos < p->size && p->buffer[pos] != '\0') pos++; Darkvater@3457: pos++; truelight@543: } truelight@543: p->pos = pos; Darkvater@3456: Darkvater@3456: str_validate(bufp); truelight@543: } truelight@543: truelight@543: // If PacketSize changes of size, you have to change the 2 packet->size truelight@543: // lines below matching the size of packet->size/PacketSize! truelight@543: // (the line: 'p->size = (uint16)p->buffer[0];' and below) truelight@543: assert_compile(sizeof(PacketSize) == 2); truelight@543: truelight@716: Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status) truelight@543: { truelight@543: ssize_t res; truelight@543: Packet *p; truelight@543: truelight@543: *status = NETWORK_RECV_STATUS_OKAY; truelight@543: truelight@543: if (cs->socket == INVALID_SOCKET) return NULL; truelight@543: truelight@543: if (cs->packet_recv == NULL) { truelight@543: cs->packet_recv = malloc(sizeof(Packet)); truelight@543: if (cs->packet_recv == NULL) error("Failed to allocate packet"); truelight@543: // Set pos to zero! truelight@543: cs->packet_recv->pos = 0; truelight@543: cs->packet_recv->size = 0; // Can be ommited, just for safety reasons truelight@543: } truelight@543: truelight@543: p = cs->packet_recv; truelight@543: truelight@543: // Read packet size truelight@543: if (p->pos < sizeof(PacketSize)) { truelight@543: while (p->pos < sizeof(PacketSize)) { truelight@543: // Read the size of the packet truelight@543: res = recv(cs->socket, p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0); truelight@543: if (res == -1) { truelight@543: int err = GET_LAST_ERROR(); truelight@543: if (err != EWOULDBLOCK) { Darkvater@5568: /* Something went wrong... (104 is connection reset by peer) */ Darkvater@5568: if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); truelight@543: *status = CloseConnection(cs); truelight@543: return NULL; truelight@543: } truelight@543: // Connection would block, so stop for now truelight@543: return NULL; truelight@543: } truelight@543: if (res == 0) { Darkvater@5568: // Client/server has left truelight@543: *status = CloseConnection(cs); truelight@543: return NULL; truelight@543: } truelight@543: p->pos += res; truelight@543: } truelight@543: truelight@543: p->size = (uint16)p->buffer[0]; truelight@543: p->size += (uint16)p->buffer[1] << 8; truelight@543: truelight@543: if (p->size > SEND_MTU) { truelight@543: *status = CloseConnection(cs); truelight@543: return NULL; truelight@543: } truelight@543: } truelight@543: truelight@543: // Read rest of packet truelight@543: while (p->pos < p->size) { truelight@543: res = recv(cs->socket, p->buffer + p->pos, p->size - p->pos, 0); truelight@543: if (res == -1) { truelight@543: int err = GET_LAST_ERROR(); truelight@543: if (err != EWOULDBLOCK) { Darkvater@5568: /* Something went wrong... (104 is connection reset by peer) */ Darkvater@5568: if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); truelight@543: *status = CloseConnection(cs); truelight@543: return NULL; truelight@543: } truelight@543: // Connection would block truelight@543: return NULL; truelight@543: } truelight@543: if (res == 0) { Darkvater@5568: // Client/server has left truelight@543: *status = CloseConnection(cs); truelight@543: return NULL; truelight@543: } truelight@543: truelight@543: p->pos += res; truelight@543: } truelight@543: truelight@543: // We have a complete packet, return it! truelight@543: p->pos = 2; truelight@543: p->next = NULL; // Should not be needed, but who knows... truelight@543: truelight@543: // Prepare for receiving a new packet truelight@543: cs->packet_recv = NULL; truelight@543: truelight@543: return p; truelight@543: } truelight@543: truelight@543: // Add a command to the local command queue truelight@716: void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp) truelight@543: { tron@4077: CommandPacket* new_cp = malloc(sizeof(*new_cp)); truelight@543: truelight@543: *new_cp = *cp; truelight@543: tron@4077: if (cs->command_queue == NULL) { truelight@543: cs->command_queue = new_cp; tron@4077: } else { truelight@543: CommandPacket *c = cs->command_queue; truelight@543: while (c->next != NULL) c = c->next; truelight@543: c->next = new_cp; truelight@543: } truelight@543: } truelight@543: truelight@543: // Prepare a DoCommand to be send over the network tron@1977: void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) truelight@543: { truelight@543: CommandPacket *c = malloc(sizeof(CommandPacket)); truelight@543: byte temp_callback; truelight@543: truelight@543: c->player = _local_player; truelight@543: c->next = NULL; truelight@543: c->tile = tile; truelight@543: c->p1 = p1; truelight@543: c->p2 = p2; truelight@543: c->cmd = cmd; truelight@543: c->callback = 0; truelight@543: truelight@543: temp_callback = 0; truelight@543: truelight@543: while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback) truelight@543: temp_callback++; truelight@543: if (temp_callback == _callback_table_count) { Darkvater@5568: DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); truelight@543: temp_callback = 0; /* _callback_table[0] == NULL */ truelight@543: } truelight@543: truelight@543: if (_network_server) { truelight@543: // We are the server, so set the command to be executed next possible frame truelight@543: c->frame = _frame_counter_max + 1; truelight@543: } else { truelight@543: c->frame = 0; // The client can't tell which frame, so just make it 0 truelight@543: } truelight@543: tron@1820: ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text)); truelight@543: truelight@543: if (_network_server) { truelight@543: // If we are the server, we queue the command in our 'special' queue. truelight@543: // In theory, we could execute the command right away, but then the miham@826: // client on the server can do everything 1 tick faster than others. truelight@543: // So to keep the game fair, we delay the command with 1 tick truelight@543: // which gives about the same speed as most clients. truelight@716: NetworkClientState *cs; truelight@543: truelight@543: // And we queue it for delivery to the clients truelight@543: FOR_ALL_CLIENTS(cs) { tron@4077: if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c); truelight@543: } truelight@543: truelight@543: // Only the server gets the callback, because clients should not get them truelight@543: c->callback = temp_callback; truelight@543: if (_local_command_queue == NULL) { truelight@543: _local_command_queue = c; truelight@543: } else { truelight@543: // Find last packet truelight@543: CommandPacket *cp = _local_command_queue; truelight@543: while (cp->next != NULL) cp = cp->next; truelight@543: cp->next = c; truelight@543: } truelight@543: truelight@543: return; truelight@543: } truelight@543: truelight@543: // Clients send their command to the server and forget all about the packet truelight@543: c->callback = temp_callback; truelight@543: SEND_COMMAND(PACKET_CLIENT_COMMAND)(c); truelight@543: } truelight@543: truelight@543: // Execute a DoCommand we received from the network truelight@543: void NetworkExecuteCommand(CommandPacket *cp) truelight@543: { truelight@543: _current_player = cp->player; tron@1820: _cmd_text = cp->text; truelight@543: /* cp->callback is unsigned. so we don't need to do lower bounds checking. */ truelight@543: if (cp->callback > _callback_table_count) { Darkvater@5568: DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback); truelight@543: cp->callback = 0; truelight@543: } truelight@543: DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND); truelight@543: } truelight@543: truelight@543: #endif /* ENABLE_NETWORK */