truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" dominik@105: #include "gui.h" truelight@0: #include "command.h" truelight@0: #include "player.h" darkvater@144: #include "console.h" truelight@0: truelight@0: #if defined(WIN32) truelight@0: # include truelight@0: # include truelight@0: truelight@0: # pragma comment (lib, "ws2_32.lib") truelight@0: # define ENABLE_NETWORK truelight@0: # define GET_LAST_ERROR() WSAGetLastError() truelight@0: # define EWOULDBLOCK WSAEWOULDBLOCK truelight@0: #endif truelight@0: truelight@0: #if defined(UNIX) truelight@0: // Make compatible with WIN32 names truelight@0: # define SOCKET int truelight@0: # define INVALID_SOCKET -1 truelight@0: // we need different defines for MorphOS and AmigaOS truelight@0: #if !defined(__MORPHOS__) && !defined(__AMIGA__) truelight@0: # define ioctlsocket ioctl truelight@0: # define closesocket close truelight@0: # define GET_LAST_ERROR() errno truelight@0: #endif truelight@0: // Need this for FIONREAD on solaris truelight@0: # define BSD_COMP truelight@0: # include truelight@0: # include truelight@0: truelight@0: // Socket stuff truelight@0: # include truelight@0: # include truelight@0: # include truelight@0: # include truelight@0: # include truelight@0: // NetDB truelight@0: # include truelight@0: truelight@0: # ifndef TCP_NODELAY truelight@0: # define TCP_NODELAY 0x0001 truelight@0: # endif truelight@0: truelight@0: #endif truelight@0: truelight@0: truelight@0: #if defined(__MORPHOS__) || defined(__AMIGA__) truelight@0: # include truelight@0: # include // required for Open/CloseLibrary() truelight@0: # if defined(__MORPHOS__) truelight@0: # include // FION#? defines truelight@0: # else // __AMIGA__ truelight@0: # include truelight@0: # endif truelight@0: truelight@0: // make source compatible with bsdsocket.library functions truelight@0: # define closesocket(s) CloseSocket(s) truelight@0: # define GET_LAST_ERROR() Errno() truelight@0: # define ioctlsocket(s,request,status) IoctlSocket((LONG)s,(ULONG)request,(char*)status) truelight@0: truelight@0: struct Library *SocketBase = NULL; darkvater@144: darkvater@173: #if !defined(__MORPHOS__) darkvater@144: // usleep() implementation darkvater@144: #include darkvater@144: #include darkvater@144: darkvater@144: struct Device *TimerBase = NULL; darkvater@144: struct MsgPort *TimerPort = NULL; darkvater@144: struct timerequest *TimerRequest = NULL; darkvater@173: #endif darkvater@144: truelight@0: #endif /* __MORPHOS__ || __AMIGA__ */ truelight@0: truelight@0: truelight@0: #define SEND_MTU 1460 truelight@0: truelight@0: #if defined(ENABLE_NETWORK) truelight@0: truelight@0: // sent from client -> server whenever the client wants to exec a command. truelight@0: // send from server -> client when another player execs a command. truelight@0: typedef struct CommandPacket { truelight@0: byte packet_length; truelight@0: byte packet_type; truelight@0: uint16 cmd; truelight@0: uint32 p1,p2; truelight@0: TileIndex tile; truelight@0: byte player;// player id, this is checked by the server. truelight@0: byte when; // offset from the current max_frame value minus 1. this is set by the server. truelight@0: uint32 dp[8]; truelight@0: } CommandPacket; truelight@0: truelight@0: assert_compile(sizeof(CommandPacket) == 16 + 32); truelight@0: truelight@0: #define COMMAND_PACKET_BASE_SIZE (sizeof(CommandPacket) - 8 * sizeof(uint32)) truelight@0: truelight@0: // sent from server -> client periodically to tell the client about the current tick in the server truelight@0: // and how far the client may progress. truelight@0: typedef struct SyncPacket { truelight@0: byte packet_length; truelight@0: byte packet_type; truelight@0: byte frames; // how many more frames may the client execute? this is relative to the old value of max. truelight@0: byte server; // where is the server currently executing? this is negatively relative to the old value of max. truelight@0: uint32 random_seed_1; // current random state at server. used to detect out of sync. truelight@0: uint32 random_seed_2; truelight@0: } SyncPacket; truelight@0: truelight@0: assert_compile(sizeof(SyncPacket) == 12); truelight@0: truelight@0: // sent from server -> client as an acknowledgement that the server received the command. truelight@0: // the command will be executed at the current value of "max". truelight@0: typedef struct AckPacket { truelight@0: byte packet_length; truelight@0: byte packet_type; truelight@0: } AckPacket; truelight@0: truelight@185: typedef struct ReadyPacket { truelight@185: byte packet_length; truelight@185: byte packet_type; truelight@185: } ReadyPacket; truelight@185: truelight@0: typedef struct FilePacketHdr { truelight@0: byte packet_length; truelight@0: byte packet_type; truelight@0: byte unused[2]; truelight@0: } FilePacketHdr; truelight@0: truelight@0: assert_compile(sizeof(FilePacketHdr) == 4); truelight@0: truelight@0: // sent from server to client when the client has joined. truelight@0: typedef struct WelcomePacket { truelight@0: byte packet_length; truelight@0: byte packet_type; truelight@0: byte unused[2]; truelight@0: } WelcomePacket; truelight@0: truelight@0: typedef struct Packet Packet; truelight@0: struct Packet { truelight@0: Packet *next; // this one has to be the first element. truelight@0: uint siz; truelight@0: byte buf[SEND_MTU]; // packet payload truelight@0: }; truelight@0: truelight@0: typedef struct ClientState { truelight@0: int socket; truelight@0: bool inactive; // disable sending of commands/syncs to client truelight@0: bool writable; truelight@185: bool ready; truelight@185: uint timeout; truelight@0: uint xmitpos; truelight@0: truelight@0: uint eaten; truelight@0: Packet *head, **last; truelight@0: truelight@0: uint buflen; // receive buffer len truelight@0: byte buf[256]; // receive buffer truelight@0: } ClientState; truelight@0: truelight@0: truelight@0: static uint _not_packet; truelight@0: truelight@0: typedef struct QueuedCommand QueuedCommand; truelight@0: struct QueuedCommand { truelight@0: QueuedCommand *next; truelight@0: CommandPacket cp; truelight@0: CommandCallback *callback; truelight@0: uint32 cmd; truelight@0: int32 frame; truelight@0: }; truelight@0: truelight@0: typedef struct CommandQueue CommandQueue; truelight@0: struct CommandQueue { truelight@0: QueuedCommand *head, **last; truelight@0: }; truelight@0: truelight@0: #define MAX_CLIENTS (MAX_PLAYERS + 1) truelight@0: truelight@0: // packets waiting to be executed, for each of the players. truelight@0: // this list is sorted in frame order, so the item on the front will be executed first. truelight@0: static CommandQueue _command_queue; truelight@0: truelight@0: // in the client, this is the list of commands that have not yet been acked. truelight@0: // when it is acked, it will be moved to the appropriate position at the end of the player queue. truelight@0: static CommandQueue _ack_queue; truelight@0: truelight@0: static ClientState _clients[MAX_CLIENTS]; truelight@0: static int _num_clients; truelight@0: truelight@0: // keep a history of the 16 most recent seeds to be able to capture out of sync errors. truelight@0: static uint32 _my_seed_list[16][2]; truelight@185: static bool _network_ready_sent; darkvater@187: static uint16 _network_ready_ahead = 1; darkvater@187: static uint16 _network_client_timeout; truelight@0: truelight@0: typedef struct FutureSeeds { truelight@0: int32 frame; truelight@0: uint32 seed[2]; truelight@0: } FutureSeeds; truelight@0: truelight@0: // remember some future seeds that the server sent to us. truelight@0: static FutureSeeds _future_seed[8]; darkvater@175: static uint _num_future_seed; truelight@0: dominik@105: static SOCKET _listensocket; // tcp socket truelight@0: dominik@105: static SOCKET _udp_client_socket; // udp server socket dominik@105: static SOCKET _udp_server_socket; // udp client socket truelight@0: truelight@0: typedef struct UDPPacket { truelight@0: byte command_code; truelight@0: byte data_len; truelight@0: byte command_check; truelight@0: byte data[255]; truelight@0: } UDPPacket; truelight@0: truelight@0: enum { truelight@0: NET_UDPCMD_SERVERSEARCH = 1, truelight@0: NET_UDPCMD_SERVERACTIVE, dominik@105: NET_UDPCMD_GETSERVERINFO, dominik@105: NET_UDPCMD_SERVERINFO, truelight@0: }; truelight@0: dominik@105: void NetworkUDPSend(bool client, struct sockaddr_in recv,struct UDPPacket packet); truelight@0: dominik@105: uint32 _network_ip_list[10]; // network ip list truelight@0: truelight@0: // this is set to point to the savegame truelight@0: static byte *_transmit_file; truelight@0: static size_t _transmit_file_size; truelight@0: truelight@0: static FILE *_recv_file; truelight@0: truelight@193: typedef struct NetworkGameInfo { darkvater@172: char server_name[40]; // name of the game darkvater@172: char server_revision[8]; // server game version darkvater@172: byte server_lang; // langid darkvater@172: byte players_max; // max players allowed on server darkvater@172: byte players_on; // current count of players on server darkvater@172: uint16 game_date; // current date darkvater@172: char game_password[10]; // should fit ... 10 chars darkvater@172: char map_name[40]; // map which is played ["random" for a randomized map] darkvater@172: uint map_width; // map width / 8 darkvater@172: uint map_height; // map height / 8 darkvater@172: byte map_set; // graphical set dominik@105: } NetworkGameInfo; dominik@105: dominik@105: typedef struct NetworkGameList { dominik@105: NetworkGameInfo item; dominik@105: uint32 ip; dominik@105: uint16 port; dominik@105: char * _next; dominik@105: } NetworkGameList; dominik@105: dominik@105: static NetworkGameInfo _network_game; dominik@105: static NetworkGameList * _network_game_list = NULL; dominik@105: truelight@185: dominik@105: /* multi os compatible sleep function */ dominik@105: void CSleep(int milliseconds) { dominik@105: #if defined(WIN32) dominik@105: Sleep(milliseconds); dominik@105: #endif dominik@105: #if defined(UNIX) truelight@193: #if !defined(__BEOS__) && !defined(__MORPHOS__) && !defined(__AMIGAOS__) dominik@105: usleep(milliseconds*1000); dominik@105: #endif dominik@105: #ifdef __BEOS__ dominik@105: snooze(milliseconds*1000); dominik@105: #endif truelight@193: #if defined(__MORPHOS__) darkvater@173: usleep(milliseconds*1000); darkvater@173: #endif truelight@193: #if defined(__AMIGAOS__) && !defined(__MORPHOS__) truelight@193: { darkvater@144: ULONG signals; darkvater@144: ULONG TimerSigBit = 1 << TimerPort->mp_SigBit; darkvater@144: darkvater@144: // send IORequest darkvater@144: TimerRequest->tr_node.io_Command = TR_ADDREQUEST; darkvater@144: TimerRequest->tr_time.tv_secs = (milliseconds * 1000) / 1000000; darkvater@144: TimerRequest->tr_time.tv_micro = (milliseconds * 1000) % 1000000; darkvater@144: SendIO((struct IORequest *)TimerRequest); darkvater@144: darkvater@144: if ( !((signals = Wait(TimerSigBit|SIGBREAKF_CTRL_C)) & TimerSigBit) ) { darkvater@144: AbortIO((struct IORequest *)TimerRequest); darkvater@144: } darkvater@144: WaitIO((struct IORequest *)TimerRequest); darkvater@144: } darkvater@173: #endif // __AMIGAOS__ && !__MORPHOS__ dominik@105: #endif dominik@105: } dominik@105: truelight@0: ////////////////////////////////////////////////////////////////////// truelight@0: dominik@105: // ****************************** // darkvater@172: // * Network Error Handlers * // darkvater@172: // ****************************** // darkvater@172: truelight@185: static void NetworkHandleSaveGameError() truelight@185: { darkvater@172: _networking_sync = false; darkvater@172: _networking_queuing = true; darkvater@172: _switch_mode = SM_MENU; darkvater@172: _switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR; darkvater@172: } darkvater@172: truelight@185: static void NetworkHandleConnectionLost() truelight@185: { darkvater@172: _networking_sync = false; darkvater@172: _networking_queuing = true; darkvater@172: _switch_mode = SM_MENU; darkvater@172: _switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION; darkvater@172: } truelight@185: static void NetworkHandleDeSync() truelight@185: { truelight@185: DEBUG(net, 0) ("[NET] Fatal ERROR: network sync error at frame %i", _frame_counter); truelight@185: { truelight@185: int i; truelight@185: for (i=15; i>=0; i--) DEBUG(net,0) ("[NET] frame %i: [0]=%i, [1]=%i",_frame_counter-(i+1),_my_seed_list[i][0],_my_seed_list[i][1]); truelight@185: for (i=0; i<8; i++) DEBUG(net,0) ("[NET] frame %i: [0]=%i, [1]=%i",_frame_counter+i,_future_seed[i].seed[0],_future_seed[i].seed[1]); truelight@185: } truelight@185: _networking_sync = false; truelight@185: _networking_queuing = true; truelight@185: _switch_mode = SM_MENU; truelight@185: _switch_mode_errorstr = STR_NETWORK_ERR_DESYNC; darkvater@172: } darkvater@172: darkvater@172: // ****************************** // dominik@105: // * TCP Packets and Handlers * // dominik@105: // ****************************** // dominik@105: truelight@0: static QueuedCommand *AllocQueuedCommand(CommandQueue *nq) truelight@0: { truelight@0: QueuedCommand *qp = (QueuedCommand*)calloc(sizeof(QueuedCommand), 1); truelight@0: assert(qp); truelight@0: *nq->last = qp; truelight@0: nq->last = &qp->next; truelight@0: return qp; truelight@0: } truelight@0: truelight@0: // go through the player queues for each player and see if there are any pending commands truelight@0: // that should be executed this frame. if there are, execute them. truelight@0: void NetworkProcessCommands() truelight@0: { truelight@0: CommandQueue *nq; truelight@0: QueuedCommand *qp; truelight@0: truelight@0: // queue mode ? truelight@0: if (_networking_queuing) truelight@0: return; truelight@0: truelight@0: nq = &_command_queue; truelight@0: while ( (qp=nq->head) && (!_networking_sync || qp->frame <= _frame_counter)) { truelight@0: // unlink it. truelight@0: if (!(nq->head = qp->next)) nq->last = &nq->head; truelight@0: truelight@0: if (qp->frame < _frame_counter && _networking_sync) { truelight@185: DEBUG(net,0) ("error: !qp->cp.frame < _frame_counter, %d < %d\n", qp->frame, _frame_counter); truelight@0: } truelight@0: truelight@0: // run the command truelight@0: _current_player = qp->cp.player; truelight@0: memcpy(_decode_parameters, qp->cp.dp, (qp->cp.packet_length - COMMAND_PACKET_BASE_SIZE)); truelight@185: truelight@0: DoCommandP(qp->cp.tile, qp->cp.p1, qp->cp.p2, qp->callback, qp->cmd | CMD_DONT_NETWORK); truelight@0: free(qp); truelight@0: } truelight@0: truelight@0: if (!_networking_server) { truelight@0: // remember the random seed so we can check if we're out of sync. truelight@0: _my_seed_list[_frame_counter & 15][0] = _sync_seed_1; truelight@0: _my_seed_list[_frame_counter & 15][1] = _sync_seed_2; truelight@0: truelight@0: while (_num_future_seed) { truelight@0: assert(_future_seed[0].frame >= _frame_counter); truelight@0: if (_future_seed[0].frame != _frame_counter) break; darkvater@172: if (_future_seed[0].seed[0] != _sync_seed_1 ||_future_seed[0].seed[1] != _sync_seed_2) NetworkHandleDeSync(); truelight@0: memcpy_overlapping(_future_seed, _future_seed + 1, --_num_future_seed * sizeof(FutureSeeds)); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: // send a packet to a client truelight@0: static void SendBytes(ClientState *cs, void *bytes, uint len) truelight@0: { truelight@0: byte *b = (byte*)bytes; truelight@0: uint n; truelight@0: Packet *p; truelight@0: truelight@0: assert(len != 0); truelight@0: truelight@0: // see if there's space in the last packet? truelight@0: if (!cs->head || (p = (Packet*)cs->last, p->siz == sizeof(p->buf))) truelight@0: p = NULL; truelight@0: truelight@0: do { truelight@0: if (!p) { truelight@0: // need to allocate a new packet buffer. truelight@0: p = (Packet*)malloc(sizeof(Packet)); truelight@0: truelight@0: // insert at the end of the linked list. truelight@0: *cs->last = p; truelight@0: cs->last = &p->next; truelight@0: p->next = NULL; truelight@0: p->siz = 0; truelight@0: } truelight@0: truelight@0: // copy bytes to packet. truelight@0: n = minu(sizeof(p->buf) - p->siz, len); truelight@0: memcpy(p->buf + p->siz, b, n); truelight@0: p->siz += n; truelight@0: b += n; truelight@0: p = NULL; truelight@0: } while (len -= n); truelight@0: } truelight@0: truelight@0: // client: truelight@0: // add it to the client's ack queue, and send the command to the server truelight@0: // server: truelight@0: // add it to the server's player queue, and send it to all clients. truelight@0: void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) truelight@0: { truelight@0: int nump; truelight@0: QueuedCommand *qp; truelight@0: ClientState *cs; truelight@0: truelight@0: qp = AllocQueuedCommand(_networking_server ? &_command_queue : &_ack_queue); truelight@0: qp->cp.packet_type = 0; truelight@0: qp->cp.tile = tile; truelight@0: qp->cp.p1 = p1; truelight@0: qp->cp.p2 = p2; truelight@0: qp->cp.cmd = (uint16)cmd; truelight@0: qp->cp.player = _local_player; truelight@0: qp->cp.when = 0; truelight@0: qp->cmd = cmd; truelight@0: qp->callback = callback; truelight@0: truelight@0: // so the server knows when to execute it. truelight@0: qp->frame = _frame_counter_max; truelight@0: truelight@0: // calculate the amount of extra bytes. truelight@0: nump = 8; truelight@0: while ( nump != 0 && ((uint32*)_decode_parameters)[nump-1] == 0) nump--; truelight@0: qp->cp.packet_length = COMMAND_PACKET_BASE_SIZE + nump * sizeof(uint32); truelight@0: if (nump != 0) memcpy(qp->cp.dp, _decode_parameters, nump * sizeof(uint32)); truelight@0: truelight@0: #if defined(TTD_BIG_ENDIAN) truelight@0: // need to convert the command to little endian before sending it. truelight@0: { truelight@0: CommandPacket cp; truelight@0: cp = qp->cp; truelight@0: cp.cmd = TO_LE16(cp.cmd); truelight@0: cp.tile = TO_LE16(cp.tile); truelight@0: cp.p1 = TO_LE32(cp.p1); truelight@0: cp.p2 = TO_LE32(cp.p2); truelight@0: for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &cp, cp.packet_length); truelight@0: } truelight@0: #else truelight@0: // send it to the peers truelight@0: for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &qp->cp, qp->cp.packet_length); truelight@0: truelight@0: #endif truelight@0: } truelight@0: truelight@0: // client: truelight@0: // server sends a command from another player that we should execute. truelight@0: // put it in the command queue. truelight@0: // truelight@0: // server: truelight@0: // client sends a command that it wants to execute. truelight@0: // fill the when field so the client knows when to execute it. truelight@0: // put it in the appropriate player queue. truelight@0: // send it to all other clients. truelight@0: // send an ack packet to the actual client. truelight@0: truelight@0: static void HandleCommandPacket(ClientState *cs, CommandPacket *np) truelight@0: { truelight@0: QueuedCommand *qp; truelight@0: ClientState *c; truelight@0: AckPacket ap; truelight@0: truelight@185: DEBUG(net, 2) ("[NET] cmd size %d", np->packet_length); truelight@0: truelight@0: assert(np->packet_length >= COMMAND_PACKET_BASE_SIZE); truelight@0: truelight@0: // put it into the command queue truelight@0: qp = AllocQueuedCommand(&_command_queue); truelight@0: qp->cp = *np; truelight@0: truelight@0: qp->frame = _frame_counter_max; truelight@0: truelight@0: qp->callback = NULL; truelight@0: truelight@0: // extra params truelight@0: memcpy(&qp->cp.dp, np->dp, np->packet_length - COMMAND_PACKET_BASE_SIZE); truelight@0: truelight@0: ap.packet_type = 2; truelight@0: ap.packet_length = 2; truelight@0: truelight@0: // send it to the peers truelight@0: if (_networking_server) { truelight@0: for(c=_clients; c->socket != INVALID_SOCKET; c++) { truelight@0: if (c == cs) { truelight@0: SendBytes(c, &ap, 2); truelight@0: } else { truelight@0: if (!cs->inactive) SendBytes(c, &qp->cp, qp->cp.packet_length); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: // convert from little endian to big endian? truelight@0: #if defined(TTD_BIG_ENDIAN) truelight@0: qp->cp.cmd = TO_LE16(qp->cp.cmd); truelight@0: qp->cp.tile = TO_LE16(qp->cp.tile); truelight@0: qp->cp.p1 = TO_LE32(qp->cp.p1); truelight@0: qp->cp.p2 = TO_LE32(qp->cp.p2); truelight@0: #endif truelight@0: truelight@0: qp->cmd = qp->cp.cmd; truelight@0: } truelight@0: truelight@0: // sent from server -> client periodically to tell the client about the current tick in the server truelight@0: // and how far the client may progress. truelight@0: static void HandleSyncPacket(SyncPacket *sp) truelight@0: { truelight@0: uint32 s1,s2; truelight@0: truelight@0: _frame_counter_srv = _frame_counter_max - sp->server; truelight@0: _frame_counter_max += sp->frames; truelight@0: truelight@185: // reset network ready packet state truelight@185: _network_ready_sent = false; truelight@0: truelight@0: // queueing only? truelight@0: if (_networking_queuing || _frame_counter == 0) truelight@0: return; truelight@0: truelight@0: s1 = TO_LE32(sp->random_seed_1); truelight@0: s2 = TO_LE32(sp->random_seed_2); truelight@0: darkvater@187: DEBUG(net, 3) ("[NET] sync seeds: [1]=%i rnd[2]=%i", sp->random_seed_1, sp->random_seed_2); truelight@185: truelight@0: if (_frame_counter_srv <= _frame_counter) { truelight@0: // we are ahead of the server check if the seed is in our list. truelight@0: if (_frame_counter_srv + 16 > _frame_counter) { truelight@0: // the random seed exists in our array check it. darkvater@172: if (s1 != _my_seed_list[_frame_counter_srv & 0xF][0] || s2 != _my_seed_list[_frame_counter_srv & 0xF][1]) NetworkHandleDeSync(); truelight@0: } truelight@0: } else { truelight@0: // the server's frame has not been executed yet. store the server's seed in a list. truelight@0: if (_num_future_seed < lengthof(_future_seed)) { truelight@0: _future_seed[_num_future_seed].frame = _frame_counter_srv; truelight@0: _future_seed[_num_future_seed].seed[0] = s1; truelight@0: _future_seed[_num_future_seed].seed[1] = s2; truelight@0: _num_future_seed++; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: // sent from server -> client as an acknowledgement that the server received the command. truelight@0: // the command will be executed at the current value of "max". truelight@0: static void HandleAckPacket() truelight@0: { truelight@0: QueuedCommand *q; truelight@0: // move a packet from the ack queue to the end of this player's queue. truelight@0: q = _ack_queue.head; truelight@0: assert(q); truelight@0: if (!(_ack_queue.head = q->next)) _ack_queue.last = &_ack_queue.head; truelight@0: q->next = NULL; truelight@0: q->frame = _frame_counter_max; truelight@0: truelight@0: *_command_queue.last = q; truelight@0: _command_queue.last = &q->next; truelight@0: truelight@185: DEBUG(net, 2) ("[NET] ack"); truelight@0: } truelight@0: truelight@0: static void HandleFilePacket(FilePacketHdr *fp) truelight@0: { truelight@0: int n = fp->packet_length - sizeof(FilePacketHdr); truelight@0: if (n == 0) { truelight@0: assert(_networking_queuing); truelight@0: assert(!_networking_sync); truelight@0: // eof truelight@0: if (_recv_file) { fclose(_recv_file); _recv_file = NULL; } truelight@0: truelight@0: // attempt loading the game. truelight@0: _game_mode = GM_NORMAL; darkvater@172: if (SaveOrLoad("networkc.tmp", SL_LOAD) != SL_OK) { darkvater@172: NetworkCoreDisconnect(); darkvater@172: NetworkHandleSaveGameError(); darkvater@172: return; darkvater@172: } truelight@0: // sync to server. truelight@0: _networking_queuing = false; dominik@105: NetworkStartSync(false); truelight@0: truelight@0: if (_network_playas == 0) { truelight@0: // send a command to make a new player truelight@0: _local_player = 0; truelight@0: NetworkSendCommand(0, 0, 0, CMD_PLAYER_CTRL, NULL); darkvater@1: _local_player = OWNER_SPECTATOR; truelight@0: } else { truelight@0: // take control over an existing company truelight@0: if (DEREF_PLAYER(_network_playas-1)->is_active) truelight@0: _local_player = _network_playas-1; truelight@0: else darkvater@1: _local_player = OWNER_SPECTATOR; truelight@0: } truelight@0: truelight@0: } else { truelight@0: if(!_recv_file) { truelight@0: _recv_file = fopen("networkc.tmp", "wb"); truelight@0: if (!_recv_file) error("can't open savefile"); truelight@0: } truelight@0: fwrite( (char*)fp + sizeof(*fp), n, 1, _recv_file); truelight@0: } truelight@0: } truelight@0: truelight@0: static void CloseClient(ClientState *cs) truelight@0: { truelight@0: Packet *p, *next; truelight@0: truelight@185: DEBUG(net, 1) ("[NET][TCP] closed client connection"); truelight@0: truelight@0: assert(cs->socket != INVALID_SOCKET); truelight@0: truelight@0: closesocket(cs->socket); truelight@0: truelight@0: // free buffers truelight@0: for(p = cs->head; p; p=next) { truelight@0: next = p->next; truelight@0: free(p); truelight@0: } truelight@0: truelight@0: // copy up structs... truelight@0: while ((cs+1)->socket != INVALID_SOCKET) { truelight@0: *cs = *(cs+1); truelight@0: cs++; truelight@0: } truelight@0: cs->socket = INVALID_SOCKET; truelight@0: dominik@105: if (_networking_server) _network_game.players_on--; dominik@105: truelight@0: _num_clients--; truelight@0: } truelight@0: truelight@0: #define NETWORK_BUFFER_SIZE 4096 truelight@0: static bool ReadPackets(ClientState *cs) truelight@0: { truelight@0: byte network_buffer[NETWORK_BUFFER_SIZE]; truelight@0: uint pos,size; truelight@0: unsigned long recv_bytes; truelight@0: truelight@0: size = cs->buflen; truelight@0: truelight@0: for(;;) { truelight@0: if (size != 0) memcpy(network_buffer, cs->buf, size); truelight@0: truelight@0: recv_bytes = recv(cs->socket, (char*)network_buffer + size, sizeof(network_buffer) - size, 0); truelight@0: if ( recv_bytes == (unsigned long)-1) { truelight@0: int err = GET_LAST_ERROR(); truelight@0: if (err == EWOULDBLOCK) break; truelight@185: DEBUG(net, 0) ("[NET] recv() failed with error %d", err); truelight@0: CloseClient(cs); truelight@0: return false; truelight@0: } truelight@0: // no more bytes for now? truelight@0: if (recv_bytes == 0) truelight@0: break; truelight@0: truelight@0: size += recv_bytes; // number of bytes read. truelight@0: pos = 0; truelight@0: while (size >= 2) { truelight@0: byte *packet = network_buffer + pos; truelight@0: // whole packet not there yet? truelight@0: if (size < packet[0]) break; truelight@0: size -= packet[0]; truelight@0: pos += packet[0]; truelight@0: switch(packet[1]) { truelight@0: case 0: truelight@0: HandleCommandPacket(cs, (CommandPacket*)packet); truelight@0: break; truelight@0: case 1: truelight@0: assert(_networking_sync || _networking_queuing); truelight@0: assert(!_networking_server); truelight@0: HandleSyncPacket((SyncPacket*)packet); truelight@0: break; truelight@0: case 2: truelight@0: assert(!_networking_server); truelight@0: HandleAckPacket(); truelight@0: break; truelight@0: case 3: truelight@0: HandleFilePacket((FilePacketHdr*)packet); truelight@0: break; truelight@185: case 5: truelight@185: cs->ready=true; truelight@185: cs->timeout=_network_client_timeout; truelight@185: break; truelight@0: default: truelight@185: DEBUG (net,0) ("net: unknown packet type"); truelight@0: } truelight@0: } truelight@0: darkvater@175: assert(size < sizeof(cs->buf)); truelight@0: truelight@0: memcpy(cs->buf, network_buffer + pos, size); truelight@0: } truelight@0: truelight@0: cs->buflen = size; truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: truelight@0: static bool SendPackets(ClientState *cs) truelight@0: { truelight@0: Packet *p; truelight@0: int n; truelight@0: uint nskip = cs->eaten, nsent = nskip; truelight@0: truelight@0: // try sending as much as possible. truelight@0: for(p=cs->head; p ;p = p->next) { truelight@0: if (p->siz) { truelight@0: assert(nskip < p->siz); truelight@0: truelight@0: n = send(cs->socket, p->buf + nskip, p->siz - nskip, 0); truelight@0: if (n == -1) { truelight@0: int err = GET_LAST_ERROR(); truelight@0: if (err == EWOULDBLOCK) break; truelight@185: DEBUG(net, 0) ("[NET] send() failed with error %d", err); truelight@0: CloseClient(cs); truelight@0: return false; truelight@0: } truelight@0: nsent += n; truelight@0: // send was not able to send it all? then we assume that the os buffer is full and break. truelight@0: if (nskip + n != p->siz) truelight@0: break; truelight@0: nskip = 0; truelight@0: } truelight@0: } truelight@0: truelight@0: // nsent bytes in the linked list are not invalid. free as many buffers as possible. truelight@0: // don't actually free the last buffer. truelight@0: while (nsent) { truelight@0: p = cs->head; truelight@0: assert(p->siz != 0); truelight@0: truelight@0: // some bytes of the packet are still unsent. truelight@0: if ( (int)(nsent - p->siz) < 0) truelight@0: break; truelight@0: nsent -= p->siz; truelight@0: p->siz = 0; truelight@0: if (p->next) { truelight@0: cs->head = p->next; truelight@0: free(p); truelight@0: } truelight@0: } truelight@0: truelight@0: cs->eaten = nsent; truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: // transmit the file.. truelight@0: static void SendXmit(ClientState *cs) truelight@0: { truelight@0: uint pos, n; truelight@0: FilePacketHdr hdr; truelight@0: int p; truelight@0: truelight@0: // if too many unsent bytes left in buffer, don't send more. truelight@0: if (cs->head && cs->head->next) truelight@0: return; truelight@0: truelight@0: pos = cs->xmitpos - 1; truelight@0: truelight@0: p = 20; truelight@0: do { truelight@0: // compute size of data to xmit truelight@0: n = minu(_transmit_file_size - pos, 248); truelight@0: truelight@0: hdr.packet_length = n + sizeof(hdr); truelight@0: hdr.packet_type = 3; truelight@0: hdr.unused[0] = hdr.unused[1] = 0; truelight@0: SendBytes(cs, &hdr, sizeof(hdr)); truelight@0: truelight@0: if (n == 0) { truelight@0: pos = -1; // eof truelight@0: break; truelight@0: } truelight@0: SendBytes(cs, _transmit_file + pos, n); truelight@0: pos += n; truelight@0: } while (--p); truelight@0: truelight@0: cs->xmitpos = pos + 1; truelight@0: truelight@185: DEBUG(net, 2) ("[NET] client xmit at %d", pos + 1); truelight@0: } truelight@0: truelight@0: static ClientState *AllocClient(SOCKET s) truelight@0: { truelight@0: ClientState *cs; truelight@0: truelight@0: if (_num_clients == MAX_CLIENTS) truelight@0: return NULL; truelight@0: dominik@105: if (_networking_server) _network_game.players_on++; dominik@105: truelight@0: cs = &_clients[_num_clients++]; truelight@0: memset(cs, 0, sizeof(*cs)); truelight@0: cs->last = &cs->head; truelight@0: cs->socket = s; truelight@185: cs->timeout = _network_client_timeout; truelight@0: return cs; truelight@0: } truelight@0: truelight@185: void NetworkSendReadyPacket() truelight@185: { truelight@185: if (!_network_ready_sent) { truelight@185: ReadyPacket *rp = malloc(sizeof(rp)); truelight@185: ClientState *c = _clients; truelight@185: truelight@185: rp->packet_type = 5; truelight@185: rp->packet_length = sizeof(rp); truelight@185: SendBytes(c, rp, sizeof(rp)); truelight@193: _network_ready_sent = true; truelight@185: } truelight@185: } truelight@0: truelight@0: static void NetworkAcceptClients() truelight@0: { truelight@0: struct sockaddr_in sin; truelight@0: SOCKET s; truelight@0: ClientState *cs; truelight@0: #ifndef __MORPHOS__ truelight@0: int sin_len; truelight@0: #else truelight@0: LONG sin_len; // for some reason we need a 'LONG' under MorphOS truelight@0: #endif truelight@0: truelight@0: assert(_listensocket != INVALID_SOCKET); truelight@0: truelight@0: for(;;) { truelight@0: sin_len = sizeof(sin); truelight@0: s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len); truelight@0: if (s == INVALID_SOCKET) return; truelight@0: truelight@0: // set nonblocking mode for client socket truelight@0: { unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } truelight@0: truelight@185: DEBUG(net, 1) ("[NET] got client from %s", inet_ntoa(sin.sin_addr)); truelight@0: truelight@0: // set nodelay truelight@0: {int b = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b));} truelight@0: truelight@0: cs = AllocClient(s); truelight@0: if (cs == NULL) { truelight@0: // no more clients allowed? truelight@0: closesocket(s); truelight@0: continue; truelight@0: } truelight@0: truelight@0: if (_networking_sync) { truelight@0: // a new client has connected. it needs a snapshot. truelight@0: cs->inactive = true; truelight@0: } truelight@0: } truelight@0: truelight@0: // when a new client has joined. it needs different information depending on if it's at the game menu or in an active game. truelight@0: // Game menu: truelight@0: // - list of players already in the game (name, company name, face, color) truelight@0: // - list of game settings and patch settings truelight@0: // Active game: truelight@0: // - the state of the world (includes player name, company name, player face, player color) truelight@0: // - list of the patch settings truelight@0: truelight@0: // Networking can be in several "states". truelight@0: // * not sync - games don't need to be in sync, and frame counter is not synced. for example intro screen. all commands are executed immediately. truelight@0: // * sync - games are in sync truelight@0: } truelight@0: truelight@0: truelight@0: static void SendQueuedCommandsToNewClient(ClientState *cs) truelight@0: { truelight@0: // send the commands in the server queue to the new client. truelight@0: QueuedCommand *qp; truelight@0: SyncPacket sp; truelight@0: int32 frame; truelight@0: truelight@185: DEBUG(net, 2) ("[NET] sending queued commands to client"); truelight@0: truelight@0: sp.packet_length = sizeof(sp); truelight@0: sp.packet_type = 1; truelight@0: sp.random_seed_1 = sp.random_seed_2 = 0; truelight@0: sp.server = 0; truelight@0: truelight@0: frame = _frame_counter; truelight@0: truelight@0: for(qp=_command_queue.head; qp; qp = qp->next) { truelight@185: DEBUG(net, 4) ("[NET] sending cmd to be executed at %d (old %d)", qp->frame, frame); truelight@0: if (qp->frame > frame) { truelight@0: assert(qp->frame <= _frame_counter_max); truelight@0: sp.frames = qp->frame - frame; truelight@0: frame = qp->frame; truelight@0: SendBytes(cs, &sp, sizeof(sp)); truelight@0: } truelight@0: SendBytes(cs, &qp->cp, qp->cp.packet_length); truelight@0: } truelight@0: truelight@0: if (frame < _frame_counter_max) { truelight@185: DEBUG(net, 4) ("[NET] sending queued sync %d (%d)", _frame_counter_max, frame); truelight@0: sp.frames = _frame_counter_max - frame; truelight@0: SendBytes(cs, &sp, sizeof(sp)); truelight@0: } truelight@0: truelight@0: } truelight@0: dominik@105: // ************************** // dominik@105: // * TCP Networking * // dominik@105: // ************************** // dominik@105: darkvater@144: unsigned long NetworkResolveHost(const char *hostname) { darkvater@144: struct hostent* remotehost; darkvater@144: darkvater@144: if ((hostname[0]<0x30) || (hostname[0]>0x39)) { darkvater@144: // seems to be an hostname [first character is no number] darkvater@144: remotehost = gethostbyname(hostname); darkvater@144: if (remotehost == NULL) { truelight@185: DEBUG(net, 1) ("[NET][IP] cannot resolve %s", hostname); darkvater@144: return 0; darkvater@144: } else { truelight@185: DEBUG(net, 1) ("[NET][IP] resolved %s to %s",hostname, inet_ntoa(*(struct in_addr *) remotehost->h_addr_list[0])); darkvater@144: return inet_addr(inet_ntoa(*(struct in_addr *) remotehost->h_addr_list[0])); darkvater@144: } darkvater@144: } else { darkvater@144: // seems to be an ip [first character is a number] darkvater@144: return inet_addr(hostname); darkvater@144: } darkvater@144: darkvater@144: } darkvater@144: dominik@105: bool NetworkConnect(const char *hostname, int port) dominik@105: { dominik@105: SOCKET s; dominik@105: struct sockaddr_in sin; dominik@105: int b; dominik@105: truelight@185: DEBUG(net, 1) ("[NET][TCP] Connecting to %s %d", hostname, port); dominik@105: dominik@105: s = socket(AF_INET, SOCK_STREAM, 0); dominik@105: if (s == INVALID_SOCKET) error("socket() failed"); dominik@105: dominik@105: b = 1; dominik@105: setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)); truelight@193: dominik@105: sin.sin_family = AF_INET; darkvater@144: sin.sin_addr.s_addr = NetworkResolveHost(hostname); dominik@105: sin.sin_port = htons(port); dominik@105: dominik@105: if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) { dominik@105: NetworkClose(true); dominik@105: return false; dominik@105: } dominik@105: dominik@105: // set nonblocking mode for socket.. dominik@105: { unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } dominik@105: dominik@105: // in client mode, only the first client field is used. it's pointing to the server. dominik@105: AllocClient(s); dominik@105: dominik@105: // queue packets.. because we're waiting for the savegame. dominik@105: _networking_queuing = true; dominik@105: _frame_counter_max = 0; dominik@105: dominik@105: return true; dominik@105: } dominik@105: dominik@105: void NetworkListen() dominik@105: { truelight@193: dominik@105: SOCKET ls; dominik@105: struct sockaddr_in sin; dominik@105: int port; dominik@105: dominik@105: port = _network_server_port; dominik@105: truelight@185: DEBUG(net, 1) ("[NET][TCP] listening on port %d", port); dominik@105: dominik@105: ls = socket(AF_INET, SOCK_STREAM, 0); dominik@105: if (ls == INVALID_SOCKET) dominik@105: error("socket() on listen socket failed"); truelight@193: dominik@105: // reuse the socket dominik@105: { dominik@105: int reuse = 1; if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) dominik@105: error("setsockopt() on listen socket failed"); dominik@105: } dominik@105: dominik@105: // set nonblocking mode for socket dominik@105: { unsigned long blocking = 1; ioctlsocket(ls, FIONBIO, &blocking); } dominik@105: dominik@105: sin.sin_family = AF_INET; dominik@105: sin.sin_addr.s_addr = 0; dominik@105: sin.sin_port = htons(port); dominik@105: dominik@105: if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) dominik@105: error("bind() failed"); dominik@105: dominik@105: if (listen(ls, 1) != 0) dominik@105: error("listen() failed"); dominik@105: dominik@105: _listensocket = ls; dominik@105: } dominik@105: truelight@0: void NetworkReceive() truelight@0: { truelight@0: ClientState *cs; truelight@0: int n; truelight@0: fd_set read_fd, write_fd; truelight@0: struct timeval tv; truelight@193: truelight@0: FD_ZERO(&read_fd); truelight@0: FD_ZERO(&write_fd); truelight@0: truelight@0: for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { truelight@0: FD_SET(cs->socket, &read_fd); truelight@0: FD_SET(cs->socket, &write_fd); truelight@0: } truelight@0: truelight@0: // take care of listener port truelight@0: if (_networking_server) { truelight@0: FD_SET(_listensocket, &read_fd); truelight@0: } truelight@0: truelight@0: tv.tv_sec = tv.tv_usec = 0; // don't block at all. truelight@0: #if !defined(__MORPHOS__) && !defined(__AMIGA__) truelight@0: n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv); truelight@0: #else truelight@0: n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL); truelight@0: #endif truelight@185: if ((n == -1) && (!_networking_server)) NetworkHandleConnectionLost(); truelight@0: truelight@0: // accept clients.. truelight@0: if (_networking_server && FD_ISSET(_listensocket, &read_fd)) truelight@0: NetworkAcceptClients(); truelight@0: truelight@0: // read stuff from clients truelight@0: for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { truelight@0: cs->writable = !!FD_ISSET(cs->socket, &write_fd); truelight@0: if (FD_ISSET(cs->socket, &read_fd)) { truelight@0: if (!ReadPackets(cs)) truelight@0: cs--; truelight@0: } truelight@0: } truelight@0: truelight@0: // if we're a server, and any client needs a snapshot, create a snapshot and send all commands from the server queue to the client. truelight@0: if (_networking_server && _transmit_file == NULL) { truelight@0: bool didsave = false; truelight@0: truelight@0: for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { truelight@0: if (cs->inactive) { truelight@0: cs->inactive = false; truelight@0: // found a client waiting for a snapshot. make a snapshot. truelight@0: if (!didsave) { truelight@0: char filename[256]; truelight@0: sprintf(filename, "%snetwork.tmp", _path.autosave_dir); truelight@0: didsave = true; truelight@0: if (SaveOrLoad(filename, SL_SAVE) != SL_OK) error("network savedump failed"); truelight@0: _transmit_file = ReadFileToMem(filename, &_transmit_file_size, 500000); truelight@0: if (_transmit_file == NULL) error("network savedump failed to load"); truelight@0: } truelight@0: // and start sending the file.. truelight@0: cs->xmitpos = 1; truelight@0: truelight@0: // send queue of commands to client. truelight@0: SendQueuedCommandsToNewClient(cs); truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void NetworkSend() truelight@0: { truelight@0: ClientState *cs; truelight@0: void *free_xmit; truelight@185: uint16 count; truelight@185: bool ready_all; truelight@0: truelight@0: // send sync packets? truelight@0: if (_networking_server && _networking_sync && !_pause) { truelight@185: truelight@0: if (++_not_packet >= _network_sync_freq) { truelight@0: SyncPacket sp; truelight@0: uint new_max; truelight@0: darkvater@188: _network_ahead_frames = _network_sync_freq + 1; truelight@185: truelight@185: ready_all=false; truelight@185: truelight@185: while (!ready_all) { truelight@185: // check wether all clients are ready truelight@185: ready_all=true; truelight@185: count=0; truelight@185: for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { truelight@185: count++; truelight@185: ready_all = ready_all && (cs->ready || cs->inactive || (cs->xmitpos>0)); darkvater@188: if (!cs->ready) cs->timeout-=5; truelight@185: if (cs->timeout == 0) { truelight@185: SET_DPARAM16(0,count); truelight@185: ShowErrorMessage(-1,STR_NETWORK_ERR_TIMEOUT,0,0); truelight@185: CloseClient(cs); truelight@185: } truelight@185: } truelight@185: if (!ready_all) { truelight@185: NetworkReceive(); truelight@185: CSleep(5); truelight@185: } truelight@185: } truelight@193: truelight@0: _not_packet = 0; truelight@0: truelight@0: new_max = max(_frame_counter + (int)_network_ahead_frames, _frame_counter_max); truelight@193: darkvater@188: DEBUG(net,3) ("net: serv: sync max=%i, seed1=%i, seed2=%i",new_max,_sync_seed_1,_sync_seed_2); truelight@193: truelight@0: sp.packet_length = sizeof(sp); truelight@0: sp.packet_type = 1; truelight@0: sp.frames = new_max - _frame_counter_max; truelight@0: sp.server = _frame_counter_max - _frame_counter; truelight@0: sp.random_seed_1 = TO_LE32(_sync_seed_1); truelight@0: sp.random_seed_2 = TO_LE32(_sync_seed_2); truelight@0: _frame_counter_max = new_max; truelight@0: truelight@185: // send it to all the clients and mark them unready truelight@185: for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { truelight@185: cs->ready=false; truelight@185: SendBytes(cs, &sp, sizeof(sp)); truelight@185: } truelight@0: } truelight@0: } truelight@0: truelight@0: free_xmit = _transmit_file; truelight@0: truelight@0: // send stuff to all clients truelight@0: for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { truelight@0: if (cs->xmitpos) { truelight@0: if (cs->writable) truelight@0: SendXmit(cs); truelight@0: free_xmit = NULL; truelight@0: } truelight@0: if (cs->writable) { truelight@0: if (!SendPackets(cs)) cs--; truelight@0: } truelight@0: } truelight@0: truelight@0: // no clients left that xmit the file, free it. truelight@0: if (free_xmit) { truelight@0: _transmit_file = NULL; truelight@0: free(free_xmit); truelight@0: } truelight@0: } truelight@0: truelight@0: dominik@105: void NetworkInitialize() truelight@0: { truelight@0: ClientState *cs; darkvater@1: truelight@0: _command_queue.last = &_command_queue.head; truelight@0: _ack_queue.last = &_ack_queue.head; truelight@0: truelight@0: // invalidate all clients truelight@0: for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++) truelight@0: cs->socket = INVALID_SOCKET; dominik@105: dominik@105: } dominik@105: dominik@105: void NetworkClose(bool client) { dominik@105: dominik@105: ClientState *cs; dominik@105: // invalidate all clients dominik@105: dominik@105: for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++) if (cs->socket != INVALID_SOCKET) { dominik@105: CloseClient(cs); dominik@105: } dominik@105: dominik@105: if (!client) { dominik@105: // if in servermode --> close listener dominik@105: closesocket(_listensocket); dominik@105: _listensocket= INVALID_SOCKET; truelight@185: DEBUG(net, 1) ("[NET][TCP] closed listener on port %i", _network_server_port); dominik@105: } truelight@0: } truelight@0: truelight@0: void NetworkShutdown() truelight@0: { truelight@193: _networking_server = false; darkvater@169: _networking = false; darkvater@169: _networking_sync = false; darkvater@169: _frame_counter = 0; darkvater@169: _frame_counter_max = 0; darkvater@169: _frame_counter_srv = 0; truelight@0: } truelight@0: truelight@0: // switch to synced mode. dominik@105: void NetworkStartSync(bool fcreset) truelight@0: { truelight@185: DEBUG(net, 3) ("[NET][SYNC] switching to synced game mode"); truelight@0: _networking_sync = true; truelight@0: _frame_counter = 0; dominik@105: if (fcreset) { dominik@105: _frame_counter_max = 0; dominik@105: _frame_counter_srv = 0; dominik@105: } truelight@0: _num_future_seed = 0; truelight@0: _sync_seed_1 = _sync_seed_2 = 0; truelight@0: memset(_my_seed_list, 0, sizeof(_my_seed_list)); dominik@105: truelight@0: } truelight@0: darkvater@144: // ********************************* // darkvater@144: // * Network Core Console Commands * // darkvater@144: // ********************************* // darkvater@144: darkvater@144: static _iconsole_var * NetworkConsoleCmdConnect(byte argc, byte* argv[], byte argt[]) { darkvater@144: if (argc<2) return NULL; darkvater@144: if (argc==2) { darkvater@144: IConsolePrintF(_iconsole_color_default, "connecting to %s",argv[1]); darkvater@144: NetworkCoreConnectGame(argv[1],_network_server_port); darkvater@144: } else if (argc==3) { darkvater@144: IConsolePrintF(_iconsole_color_default, "connecting to %s on port %s",argv[1],argv[2]); darkvater@144: NetworkCoreConnectGame(argv[1],atoi(argv[2])); darkvater@187: } else if (argc==4) { darkvater@187: IConsolePrintF(_iconsole_color_default, "connecting to %s on port %s as player %s",argv[1],argv[2],argv[3]); darkvater@187: _network_playas = atoi(argv[3]); darkvater@187: NetworkCoreConnectGame(argv[1],atoi(argv[2])); darkvater@144: } darkvater@144: return NULL; darkvater@144: } darkvater@144: truelight@0: // ************************** // truelight@0: // * UDP Network Extensions * // truelight@0: // ************************** // truelight@0: dominik@105: void NetworkUDPListen(bool client) truelight@0: { truelight@0: SOCKET udp; truelight@0: struct sockaddr_in sin; dominik@105: int port; truelight@0: dominik@105: if (client) { port = _network_client_port; } else { port = _network_server_port; }; dominik@105: truelight@185: DEBUG(net, 1) ("[NET][UDP] listening on port %i", port); truelight@0: truelight@0: udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); truelight@193: dominik@105: // this disables network dominik@105: _network_available = !(udp == INVALID_SOCKET); truelight@193: truelight@0: // set nonblocking mode for socket truelight@0: { unsigned long blocking = 1; ioctlsocket(udp, FIONBIO, &blocking); } truelight@0: truelight@0: sin.sin_family = AF_INET; truelight@0: sin.sin_addr.s_addr = 0; truelight@0: sin.sin_port = htons(port); truelight@0: truelight@0: if (bind(udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) truelight@185: DEBUG(net, 1) ("[NET][UDP] error: bind failed on port %i", port); truelight@193: truelight@0: truelight@0: // enable broadcasting truelight@0: { unsigned long val=1; setsockopt(udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); } dominik@105: // allow reusing dominik@105: { unsigned long val=1; setsockopt(udp, SOL_SOCKET, SO_REUSEADDR, (char *) &val , sizeof(val)); } dominik@105: dominik@105: if (client) { _udp_client_socket = udp; } else { _udp_server_socket = udp; } ; truelight@0: truelight@0: } truelight@0: dominik@105: void NetworkUDPClose(bool client) { truelight@193: if (client) { truelight@185: DEBUG(net, 1) ("[NET][UDP] closed listener on port %i", _network_client_port); dominik@105: closesocket(_udp_client_socket); dominik@105: _udp_client_socket = INVALID_SOCKET; dominik@105: } else { truelight@185: DEBUG(net, 1) ("[NET][UDP] closed listener on port %i", _network_server_port); dominik@105: closesocket(_udp_server_socket); dominik@105: _udp_server_socket = INVALID_SOCKET; dominik@105: }; dominik@105: } dominik@105: dominik@105: void NetworkUDPReceive(bool client) { truelight@0: struct sockaddr_in client_addr; darkvater@173: #ifndef __MORPHOS__ truelight@0: int client_len; darkvater@173: #else darkvater@173: LONG client_len; // for some reason we need a 'LONG' under MorphOS darkvater@173: #endif truelight@0: int nbytes; truelight@0: struct UDPPacket packet; truelight@0: int packet_len; truelight@193: dominik@105: SOCKET udp; dominik@105: if (client) udp=_udp_client_socket; else udp=_udp_server_socket; dominik@105: truelight@0: packet_len = sizeof(packet); truelight@193: client_len = sizeof(client_addr); truelight@193: dominik@105: nbytes = recvfrom(udp, (char *) &packet, packet_len , 0, (struct sockaddr *) &client_addr, &client_len); truelight@0: if (nbytes>0) { truelight@0: if (packet.command_code==packet.command_check) switch (packet.command_code) { truelight@193: dominik@105: case NET_UDPCMD_SERVERSEARCH: dominik@105: if (!client) { dominik@105: packet.command_check=packet.command_code=NET_UDPCMD_SERVERINFO; dominik@105: memcpy(&packet.data,&_network_game,sizeof(_network_game)); dominik@105: packet.data_len=sizeof(_network_game); dominik@105: NetworkUDPSend(client,client_addr, packet); dominik@105: } dominik@105: break; dominik@105: case NET_UDPCMD_GETSERVERINFO: dominik@105: if (!client) { dominik@105: packet.command_check=packet.command_code=NET_UDPCMD_SERVERINFO; dominik@105: memcpy(&packet.data,&_network_game,sizeof(_network_game)); dominik@105: packet.data_len=sizeof(_network_game); dominik@105: NetworkUDPSend(client,client_addr, packet); truelight@0: } truelight@0: break; dominik@105: case NET_UDPCMD_SERVERINFO: dominik@105: if (client) { dominik@105: NetworkGameList * item; dominik@105: dominik@105: item = (NetworkGameList *) NetworkGameListAdd(); dominik@105: item -> ip = inet_addr(inet_ntoa(client_addr.sin_addr)); dominik@105: item -> port = ntohs(client_addr.sin_port); truelight@193: dominik@105: memcpy(item,&packet.data,packet.data_len); dominik@105: } dominik@105: break; truelight@0: } truelight@0: } truelight@0: } truelight@0: darkvater@81: truelight@0: dominik@105: void NetworkUDPBroadCast(bool client, struct UDPPacket packet) { truelight@0: int i=0, res; truelight@0: struct sockaddr_in out_addr; truelight@0: uint32 bcaddr; truelight@0: byte * bcptr; dominik@105: dominik@105: SOCKET udp; dominik@105: if (client) udp=_udp_client_socket; else udp=_udp_server_socket; dominik@105: truelight@0: while (_network_ip_list[i]!=0) { truelight@0: bcaddr=_network_ip_list[i]; truelight@0: out_addr.sin_family = AF_INET; dominik@105: if (client) { out_addr.sin_port = htons(_network_server_port); } else { out_addr.sin_port = htons(_network_client_port); }; truelight@0: bcptr = (byte *) &bcaddr; truelight@0: bcptr[3]=255; truelight@0: out_addr.sin_addr.s_addr = bcaddr; dominik@105: res=sendto(udp,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &out_addr,sizeof(out_addr)); truelight@185: if (res==-1) DEBUG(net, 1)("udp: broadcast error: %i",GET_LAST_ERROR()); truelight@0: i++; truelight@0: } truelight@193: truelight@0: } truelight@0: dominik@105: void NetworkUDPSend(bool client, struct sockaddr_in recv,struct UDPPacket packet) { dominik@105: dominik@105: SOCKET udp; dominik@105: if (client) udp=_udp_client_socket; else udp=_udp_server_socket; truelight@193: dominik@105: sendto(udp,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &recv,sizeof(recv)); truelight@0: } truelight@0: dominik@105: darkvater@175: bool NetworkUDPSearchGame(const byte ** _network_detected_serverip, unsigned short * _network_detected_serverport) { truelight@0: struct UDPPacket packet; truelight@0: int timeout=3000; truelight@193: dominik@105: NetworkGameListClear(); dominik@105: truelight@185: DEBUG(net, 0) ("[NET][UDP] searching server"); dominik@105: *_network_detected_serverip = "255.255.255.255"; dominik@105: *_network_detected_serverport = 0; truelight@193: truelight@0: packet.command_check=packet.command_code=NET_UDPCMD_SERVERSEARCH; truelight@0: packet.data_len=0; dominik@105: NetworkUDPBroadCast(true, packet); truelight@0: while (timeout>=0) { truelight@0: CSleep(100); truelight@0: timeout-=100; dominik@105: NetworkUDPReceive(true); dominik@105: dominik@105: if (_network_game_count>0) { dominik@105: NetworkGameList * item; dominik@105: item = (NetworkGameList *) NetworkGameListItem(0); dominik@105: *_network_detected_serverip=inet_ntoa(*(struct in_addr *) &item->ip); dominik@105: *_network_detected_serverport=item->port; dominik@105: timeout=-1; truelight@185: DEBUG(net, 0) ("[NET][UDP] server found on %s", *_network_detected_serverip); dominik@105: } truelight@193: truelight@0: } dominik@105: darkvater@175: return (*_network_detected_serverport>0); truelight@193: truelight@0: } truelight@0: truelight@0: dominik@105: // *************************** // dominik@105: // * New Network Core System * // dominik@105: // *************************** // dominik@105: dominik@105: void NetworkIPListInit() { dominik@105: struct hostent* he = NULL; dominik@105: char hostname[250]; dominik@105: uint32 bcaddr; dominik@105: int i=0; truelight@193: dominik@105: gethostname(hostname,250); truelight@185: DEBUG(net, 2) ("[NET][IP] init for host %s", hostname); dominik@105: he=gethostbyname((char *) hostname); truelight@110: truelight@110: if (he == NULL) { truelight@110: he = gethostbyname("localhost"); truelight@110: } truelight@193: truelight@110: if (he == NULL) { truelight@110: bcaddr = inet_addr("127.0.0.1"); truelight@110: he = gethostbyaddr(inet_ntoa(*(struct in_addr *) &bcaddr), sizeof(bcaddr), AF_INET); dominik@105: } dominik@105: dominik@105: if (he == NULL) { truelight@185: DEBUG(net, 2) ("[NET][IP] cannot resolve %s", hostname); dominik@105: } else { truelight@193: while(he->h_addr_list[i]) { dominik@105: bcaddr = inet_addr(inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); dominik@105: _network_ip_list[i]=bcaddr; truelight@185: DEBUG(net, 2) ("[NET][IP] add %s",inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); dominik@105: i++; dominik@105: } dominik@105: dominik@105: } dominik@105: _network_ip_list[i]=0; truelight@193: dominik@105: } dominik@105: dominik@105: /* *************************************************** */ dominik@105: dominik@105: void NetworkCoreInit() { dominik@105: truelight@185: DEBUG(net, 3) ("[NET][Core] init()"); dominik@105: _network_available=true; truelight@185: _network_client_timeout=3000; dominik@105: dominik@105: // [win32] winsock startup dominik@105: dominik@105: #if defined(WIN32) dominik@105: { dominik@105: WSADATA wsa; truelight@185: DEBUG(net, 3) ("[NET][Core] using windows socket library"); dominik@105: if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) { truelight@185: DEBUG(net, 3) ("[NET][Core] error: WSAStartup failed"); dominik@105: _network_available=false; dominik@105: } dominik@105: } truelight@193: #else dominik@105: dominik@105: // [morphos/amigaos] bsd-socket startup dominik@105: dominik@105: #if defined(__MORPHOS__) || defined(__AMIGA__) dominik@105: { dominik@105: DEBUG(misc,3) ("[NET][Core] using bsd socket library"); dominik@105: if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) { truelight@185: DEBUG(net, 3) ("[NET][Core] Couldn't open bsdsocket.library version 4."); dominik@105: _network_available=false; dominik@105: } darkvater@144: darkvater@173: #if !defined(__MORPHOS__) darkvater@173: // for usleep() implementation (only required for legacy AmigaOS builds) darkvater@144: if ( (TimerPort = CreateMsgPort()) ) { darkvater@144: if ( (TimerRequest = (struct timerequest *) CreateIORequest(TimerPort, sizeof(struct timerequest))) ) { darkvater@144: if ( OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest *) TimerRequest, 0) == 0 ) { darkvater@144: if ( !(TimerBase = TimerRequest->tr_node.io_Device) ) { truelight@193: // free ressources... truelight@185: DEBUG(net, 3) ("[NET][Core] Couldn't initialize timer."); darkvater@144: _network_available=false; darkvater@144: } darkvater@144: } darkvater@144: } darkvater@144: } truelight@193: #endif darkvater@173: dominik@105: } dominik@105: #else dominik@105: dominik@105: // [linux/macos] unix-socket startup dominik@105: truelight@185: DEBUG(net, 3) ("[NET][Core] using unix socket library"); dominik@105: dominik@105: #endif dominik@105: dominik@105: #endif dominik@105: dominik@105: dominik@105: if (_network_available) { truelight@185: DEBUG(net, 3) ("[NET][Core] OK: multiplayer available"); dominik@105: // initiate network ip list dominik@105: NetworkIPListInit(); darkvater@144: IConsoleCmdRegister("connect",NetworkConsoleCmdConnect); darkvater@187: IConsoleVarRegister("net_client_timeout",&_network_client_timeout,ICONSOLE_VAR_UINT16); darkvater@187: IConsoleVarRegister("net_ready_ahead",&_network_ready_ahead,ICONSOLE_VAR_UINT16); darkvater@188: IConsoleVarRegister("net_sync_freq",&_network_sync_freq,ICONSOLE_VAR_UINT16); dominik@105: } else { truelight@185: DEBUG(net, 3) ("[NET][Core] FAILED: multiplayer not available"); dominik@105: } dominik@105: } dominik@105: dominik@105: /* *************************************************** */ dominik@105: dominik@105: void NetworkCoreShutdown() { dominik@105: truelight@185: DEBUG(net, 3) ("[NET][Core] shutdown()"); dominik@105: dominik@105: #if defined(__MORPHOS__) || defined(__AMIGA__) truelight@193: { darkvater@144: // free allocated ressources truelight@193: #if !defined(__MORPHOS__) darkvater@144: if (TimerBase) { CloseDevice((struct IORequest *) TimerRequest); } darkvater@144: if (TimerRequest) { DeleteIORequest(TimerRequest); } darkvater@144: if (TimerPort) { DeleteMsgPort(TimerPort); } darkvater@173: #endif darkvater@144: dominik@105: if (SocketBase) { dominik@105: CloseLibrary(SocketBase); dominik@105: } dominik@105: } dominik@105: #endif dominik@105: dominik@105: dominik@105: #if defined(WIN32) dominik@105: { dominik@105: WSACleanup(); dominik@105: } dominik@105: #endif dominik@105: dominik@105: } dominik@105: dominik@105: /* *************************************************** */ dominik@105: darkvater@175: bool NetworkCoreConnectGame(const byte* b, unsigned short port) dominik@105: { dominik@105: if (!_network_available) return false; dominik@105: darkvater@175: if (strcmp(b,"auto")==0) { dominik@105: // do autodetect dominik@105: NetworkUDPSearchGame(&b, &port); darkvater@172: } dominik@105: dominik@105: if (port==0) { dominik@105: // autodetection failed dominik@105: if (_networking_override) NetworkLobbyShutdown(); dominik@105: ShowErrorMessage(-1, STR_NETWORK_ERR_NOSERVER, 0, 0); darkvater@172: _switch_mode_errorstr = STR_NETWORK_ERR_NOSERVER; dominik@105: return false; darkvater@172: } darkvater@172: dominik@105: NetworkInitialize(); dominik@105: _networking = NetworkConnect(b, port); dominik@105: if (_networking) { dominik@105: NetworkLobbyShutdown(); darkvater@172: } else { truelight@193: if (_networking_override) darkvater@172: NetworkLobbyShutdown(); truelight@193: dominik@105: ShowErrorMessage(-1, STR_NETWORK_ERR_NOCONNECTION,0,0); darkvater@172: _switch_mode_errorstr = STR_NETWORK_ERR_NOCONNECTION; darkvater@172: } dominik@105: return _networking; dominik@105: } dominik@105: dominik@105: /* *************************************************** */ dominik@105: dominik@105: bool NetworkCoreStartGame() dominik@105: { dominik@105: if (!_network_available) return false; dominik@105: NetworkLobbyShutdown(); dominik@105: NetworkInitialize(); dominik@105: NetworkListen(); dominik@105: NetworkUDPListen(false); dominik@105: _networking_server = true; dominik@105: _networking = true; dominik@105: NetworkGameFillDefaults(); // clears the network game info dominik@105: _network_game.players_on++; // the serverplayer is online dominik@105: return true; dominik@105: } dominik@105: dominik@105: /* *************************************************** */ dominik@105: dominik@105: void NetworkCoreDisconnect() dominik@105: { dominik@105: /* terminate server */ dominik@105: if (_networking_server) { dominik@105: NetworkUDPClose(false); dominik@105: NetworkClose(false); truelight@193: } dominik@105: dominik@105: /* terminate client connection */ dominik@105: else if (_networking) { dominik@105: NetworkClose(true); dominik@105: } truelight@193: dominik@105: NetworkShutdown(); dominik@105: } dominik@105: dominik@105: /* *************************************************** */ dominik@105: dominik@105: void NetworkCoreLoop(bool incomming) { dominik@105: dominik@105: dominik@105: if (incomming) { dominik@105: dominik@105: // incomming dominik@105: dominik@105: if ( _udp_client_socket != INVALID_SOCKET ) NetworkUDPReceive(true); dominik@105: if ( _udp_server_socket != INVALID_SOCKET ) NetworkUDPReceive(false); dominik@105: dominik@105: if (_networking) { dominik@105: NetworkReceive(); dominik@105: NetworkProcessCommands(); // to check if we got any new commands belonging to the current frame before we increase it. dominik@105: } dominik@105: dominik@105: } else { dominik@105: dominik@105: // outgoing dominik@105: darkvater@187: if ((_networking) && (!_networking_server) && (_frame_counter+_network_ready_ahead >= _frame_counter_max)) { truelight@185: // send the "i" am ready message to the server darkvater@188: // [_network_ready_ahead] frames before "i" reach the frame-limit truelight@185: NetworkSendReadyPacket(); truelight@185: } truelight@185: truelight@185: dominik@105: if (_networking) { dominik@105: NetworkSend(); dominik@105: } dominik@105: dominik@105: } dominik@105: dominik@105: } dominik@105: dominik@105: void NetworkLobbyInit() { truelight@185: DEBUG(net, 3) ("[NET][Lobby] init()"); dominik@105: NetworkUDPListen(true); dominik@105: } dominik@105: dominik@105: void NetworkLobbyShutdown() { truelight@185: DEBUG(net, 3) ("[NET][Lobby] shutdown()"); dominik@105: NetworkUDPClose(true); dominik@105: } dominik@105: dominik@105: dominik@105: // ******************************** // dominik@105: // * Network Game List Extensions * // dominik@105: // ******************************** // dominik@105: dominik@105: void NetworkGameListClear() { dominik@105: NetworkGameList * item; truelight@193: NetworkGameList * next; dominik@105: truelight@185: DEBUG(net, 4) ("[NET][G-List] cleared server list"); dominik@105: dominik@105: item = _network_game_list; dominik@105: while (item != NULL) { dominik@105: next = (NetworkGameList *) item -> _next; dominik@105: free (item); dominik@105: item = next; dominik@105: } dominik@105: _network_game_list=NULL; dominik@105: _network_game_count=0; dominik@105: } dominik@105: dominik@105: char * NetworkGameListAdd() { dominik@105: NetworkGameList * item; truelight@193: NetworkGameList * before; dominik@105: truelight@185: DEBUG(net, 4) ("[NET][G-List] added server to list"); dominik@105: dominik@105: item = _network_game_list; dominik@105: before = item; dominik@105: while (item != NULL) { dominik@105: before = item; dominik@105: item = (NetworkGameList *) item -> _next; dominik@105: } dominik@105: item = malloc(sizeof(NetworkGameList)); dominik@105: item -> _next = NULL; dominik@105: if (before == NULL) { dominik@105: _network_game_list = item; dominik@105: } else { dominik@105: before -> _next = (char *) item; dominik@105: } dominik@105: _network_game_count++; dominik@105: return (char *) item; dominik@105: } dominik@105: dominik@105: void NetworkGameListFromLAN() { dominik@105: struct UDPPacket packet; truelight@185: DEBUG(net, 2) ("[NET][G-List] searching server over lan"); dominik@105: NetworkGameListClear(); dominik@105: packet.command_check=packet.command_code=NET_UDPCMD_SERVERSEARCH; dominik@105: packet.data_len=0; dominik@105: NetworkUDPBroadCast(true,packet); dominik@105: } dominik@105: dominik@105: void NetworkGameListFromInternet() { truelight@185: DEBUG(net, 2) ("[NET][G-List] searching servers over internet"); dominik@105: NetworkGameListClear(); dominik@105: dominik@105: // **TODO** masterserver communication [internet protocol list] dominik@105: dominik@105: } dominik@105: dominik@105: char * NetworkGameListItem(uint16 index) { dominik@105: NetworkGameList * item; truelight@193: NetworkGameList * next; dominik@105: uint16 cnt = 0; dominik@105: dominik@105: item = _network_game_list; dominik@105: dominik@105: while ((item != NULL) && (cnt != index)) { dominik@105: next = (NetworkGameList *) item -> _next; dominik@105: item = next; dominik@105: cnt++; dominik@105: } dominik@105: dominik@105: return (char *) item; dominik@105: } dominik@105: dominik@105: // *************************** // dominik@105: // * Network Game Extensions * // dominik@105: // *************************** // dominik@105: dominik@105: void NetworkGameFillDefaults() { dominik@105: NetworkGameInfo * game = &_network_game; dominik@105: #if defined(WITH_REV) dominik@105: extern char _openttd_revision[]; dominik@105: #endif truelight@193: truelight@185: DEBUG(net, 4) ("[NET][G-Info] setting defaults"); dominik@105: dominik@105: ttd_strlcpy(game->server_name,"OpenTTD Game",13); dominik@105: game->game_password[0]='\0'; dominik@105: game->map_name[0]='\0'; dominik@105: #if defined(WITH_REV) dominik@105: ttd_strlcpy(game->server_revision,_openttd_revision,strlen(_openttd_revision)); dominik@105: #else dominik@105: ttd_strlcpy(game->server_revision,"norev000",strlen("norev000")); dominik@105: #endif dominik@105: game->game_date=0; dominik@105: dominik@105: game->map_height=0; dominik@105: game->map_width=0; dominik@105: game->map_set=0; dominik@105: dominik@105: game->players_max=8; dominik@105: game->players_on=0; truelight@193: dominik@105: game->server_lang=_dynlang.curr; dominik@105: } dominik@105: dominik@105: void NetworkGameChangeDate(uint16 newdate) { dominik@105: if (_networking_server) { dominik@105: _network_game.game_date = newdate; dominik@105: } dominik@105: } dominik@105: dominik@8: #else // not ENABLE_NETWORK truelight@0: truelight@0: // stubs dominik@105: void NetworkInitialize() {} truelight@0: void NetworkShutdown() {} dominik@105: void NetworkListen() {} truelight@0: void NetworkConnect(const char *hostname, int port) {} truelight@0: void NetworkReceive() {} truelight@0: void NetworkSend() {} truelight@0: void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) {} truelight@0: void NetworkProcessCommands() {} dominik@106: void NetworkStartSync(bool fcreset) {} dominik@105: void NetworkCoreInit() { _network_available=false; }; dominik@105: void NetworkCoreShutdown() {}; dominik@105: void NetworkCoreDisconnect() {}; dominik@105: void NetworkCoreLoop(bool incomming) {}; darkvater@177: bool NetworkCoreConnectGame(const byte* b, unsigned short port) {return false;}; dominik@106: bool NetworkCoreStartGame() {return false;}; dominik@105: void NetworkLobbyShutdown() {}; dominik@105: void NetworkLobbyInit() {}; dominik@105: void NetworkGameListClear() {}; dominik@105: char * NetworkGameListAdd() {return NULL;}; dominik@105: void NetworkGameListFromLAN() {}; dominik@105: void NetworkGameListFromInternet() {}; dominik@105: void NetworkGameFillDefaults() {}; dominik@105: char * NetworkGameListItem(uint16 index) {return NULL;}; dominik@105: void NetworkGameChangeDate(uint16 newdate) {}; dominik@105: dominik@105: #endif