network_data.c
changeset 716 8af847728d5b
parent 543 946badd71033
child 826 fff56bbc3606
equal deleted inserted replaced
715:28dd55cc55ae 716:8af847728d5b
    84 
    84 
    85 // This function puts the packet in the send-queue and it is send
    85 // This function puts the packet in the send-queue and it is send
    86 //  as soon as possible
    86 //  as soon as possible
    87 // (that is: the next tick, or maybe one tick later if the
    87 // (that is: the next tick, or maybe one tick later if the
    88 //   OS-network-buffer is full)
    88 //   OS-network-buffer is full)
    89 void NetworkSend_Packet(Packet *packet, ClientState *cs)
    89 void NetworkSend_Packet(Packet *packet, NetworkClientState *cs)
    90 {
    90 {
    91 	Packet *p;
    91 	Packet *p;
    92 	assert(packet != NULL);
    92 	assert(packet != NULL);
    93 
    93 
    94 	packet->pos = 0;
    94 	packet->pos = 0;
   112 // Functions to help NetworkRecv_Packet/NetworkSend_Packet a bit
   112 // Functions to help NetworkRecv_Packet/NetworkSend_Packet a bit
   113 //  A socket can make errors. When that happens
   113 //  A socket can make errors. When that happens
   114 //  this handles what to do.
   114 //  this handles what to do.
   115 // For clients: close connection and drop back to main-menu
   115 // For clients: close connection and drop back to main-menu
   116 // For servers: close connection and that is it
   116 // For servers: close connection and that is it
   117 NetworkRecvStatus CloseConnection(ClientState *cs)
   117 NetworkRecvStatus CloseConnection(NetworkClientState *cs)
   118 {
   118 {
   119 	CloseClient(cs);
   119 	NetworkCloseClient(cs);
   120 
   120 
   121 	// Clients drop back to the main menu
   121 	// Clients drop back to the main menu
   122 	if (!_network_server) {
   122 	if (!_network_server) {
   123 		_switch_mode = SM_MENU;
   123 		_switch_mode = SM_MENU;
   124 		_networking = false;
   124 		_networking = false;
   134 //  it stops when:
   134 //  it stops when:
   135 //   1) all packets are send (queue is empty)
   135 //   1) all packets are send (queue is empty)
   136 //   2) the OS reports back that it can not send any more
   136 //   2) the OS reports back that it can not send any more
   137 //        data right now (full network-buffer, it happens ;))
   137 //        data right now (full network-buffer, it happens ;))
   138 //   3) sending took too long
   138 //   3) sending took too long
   139 bool NetworkSend_Packets(ClientState *cs)
   139 bool NetworkSend_Packets(NetworkClientState *cs)
   140 {
   140 {
   141 	ssize_t res;
   141 	ssize_t res;
   142 	Packet *p;
   142 	Packet *p;
   143 
   143 
   144 	// We can not write to this socket!!
   144 	// We can not write to this socket!!
   240 // If PacketSize changes of size, you have to change the 2 packet->size
   240 // If PacketSize changes of size, you have to change the 2 packet->size
   241 //   lines below matching the size of packet->size/PacketSize!
   241 //   lines below matching the size of packet->size/PacketSize!
   242 // (the line: 'p->size = (uint16)p->buffer[0];' and below)
   242 // (the line: 'p->size = (uint16)p->buffer[0];' and below)
   243 assert_compile(sizeof(PacketSize) == 2);
   243 assert_compile(sizeof(PacketSize) == 2);
   244 
   244 
   245 Packet *NetworkRecv_Packet(ClientState *cs, NetworkRecvStatus *status)
   245 Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status)
   246 {
   246 {
   247 	ssize_t res;
   247 	ssize_t res;
   248 	Packet *p;
   248 	Packet *p;
   249 
   249 
   250 	*status = NETWORK_RECV_STATUS_OKAY;
   250 	*status = NETWORK_RECV_STATUS_OKAY;
   328 
   328 
   329 	return p;
   329 	return p;
   330 }
   330 }
   331 
   331 
   332 // Add a command to the local command queue
   332 // Add a command to the local command queue
   333 void NetworkAddCommandQueue(ClientState *cs, CommandPacket *cp)
   333 void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp)
   334 {
   334 {
   335 	CommandPacket *new_cp = malloc(sizeof(CommandPacket));
   335 	CommandPacket *new_cp = malloc(sizeof(CommandPacket));
   336 
   336 
   337 	*new_cp = *cp;
   337 	*new_cp = *cp;
   338 
   338 
   388 		// If we are the server, we queue the command in our 'special' queue.
   388 		// If we are the server, we queue the command in our 'special' queue.
   389 		//   In theory, we could execute the command right away, but then the
   389 		//   In theory, we could execute the command right away, but then the
   390 		//   client on the server can do everything 1 tick faster then others.
   390 		//   client on the server can do everything 1 tick faster then others.
   391 		//   So to keep the game fair, we delay the command with 1 tick
   391 		//   So to keep the game fair, we delay the command with 1 tick
   392 		//   which gives about the same speed as most clients.
   392 		//   which gives about the same speed as most clients.
   393 		ClientState *cs;
   393 		NetworkClientState *cs;
   394 
   394 
   395 		// And we queue it for delivery to the clients
   395 		// And we queue it for delivery to the clients
   396 		FOR_ALL_CLIENTS(cs) {
   396 		FOR_ALL_CLIENTS(cs) {
   397 			if (cs->status > STATUS_AUTH) {
   397 			if (cs->status > STATUS_AUTH) {
   398 				NetworkAddCommandQueue(cs, c);
   398 				NetworkAddCommandQueue(cs, c);