DEADSOFTWARE

Изменён способ передачи does команд и исправлены адские тормоза
[netwar.git] / server.c
1 #include <assert.h>
2 #include <SDL2/SDL_net.h>
4 #include "game.h"
5 #include "server.h"
6 #include "network.h"
7 #include "protocol.h"
9 #define MAX_NAMESIZE 32
11 typedef struct Client {
12 bool connected;
13 IPaddress address;
14 char name[MAX_NAMESIZE];
15 } Client;
17 static Uint32 lastTime;
18 static Uint32 currTime;
20 static UDPsocket sock;
21 static Client client[MAX_PLAYERS];
22 static char name[MAX_NAMESIZE] = "netwar! server";
23 static int clientlimit = 0;
25 static inline float randf() {
26 return (float) rand() / (float) RAND_MAX;
27 }
29 static inline float randfm() {
30 return (rand() % 2) ? (-randf()) : (+randf());
31 }
33 static inline bool cmpaddr(IPaddress a, IPaddress b) {
34 return (a.host == b.host) && (a.port == b.port);
35 }
37 static int sv_find_client(IPaddress address) {
38 for(int i = 0; i < clientlimit; i++)
39 if(client[i].connected && cmpaddr(client[i].address, address))
40 return i;
41 return -1;
42 }
44 static void sv_sync_client(int id, bool full) {
45 assert(id >= 0);
46 assert(client[id].connected);
48 for(int i = 0; i < clientlimit; i++)
49 if(full || g_player[i].updated)
50 SendMessage(sock, client[id].address, sv_splr(i, g_player[i].live, g_player[i].x, g_player[i].y, g_player[i].r, g_player[i].vx, g_player[i].vy, g_player[i].vr));
52 for(int i = 0; i < MAX_BULLETS; i++)
53 if(full || g_bullet[i].updated)
54 SendMessage(sock, client[id].address, sv_sbul(i, g_bullet[i].owner, g_bullet[i].live, g_bullet[i].x, g_bullet[i].y, g_bullet[i].vx, g_bullet[i].vy, g_bullet[i].tick));
55 }
57 static void sv_spawn_player(int id) {
58 assert(id >= 0);
59 g_player_set(id, true, randfm(), randfm(), randfm(), 0, 0, 0);
60 }
62 static void sv_kill_player(int id, bool send, const char * msg) {
63 assert(id >= 0);
64 assert(client[id].connected);
66 if(send)
67 SendMessage(sock, client[id].address, sv_kill(msg));
69 client[id].connected = false;
70 g_player_set(id, false, 0, 0, 0, 0, 0, 0);
71 SDL_Log("Player %s (%i) disconnected: %s", client[id].name, id, msg);
72 }
74 static void sv_move_player(int id, ProtocolMessage m) {
75 assert(id >= 0);
76 assert(client[id].connected);
78 if(!g_player[id].live)
79 sv_spawn_player(id);
81 g_player_does(id, m.cl.does.code);
82 }
84 static void sv_register_player(IPaddress address, ProtocolMessage m) {
85 if(m.cl.info.version != PROTOCOL_VERSION)
86 SendMessage(sock, address, sv_kill("Uncompatible version"));
88 int i = 0;
89 while((i < clientlimit) && (client[i].connected))
90 ++i;
92 int namesize = sizeof(m.cl.info.name);
93 if(i >= clientlimit) {
94 SDL_Log("Too may clints, player %.*s disconnected", namesize, m.cl.info.name);
95 SendMessage(sock, address, sv_kill("Too many clients"));
96 return;
97 }
99 client[i].connected = true;
100 client[i].address = address;
101 strncpy(client[i].name, (char*) m.cl.info.name, MAX_NAMESIZE);
102 client[i].name[MAX_NAMESIZE - 1] = 0;
104 SDL_Log("Player %s (%i) connected", client[i].name, i);
105 SendMessage(sock, address, sv_info(name, i, clientlimit));
107 sv_spawn_player(i);
108 sv_sync_client(i, true);
111 static void sv_recv() {
112 IPaddress address;
113 ProtocolMessage m;
114 while(RecvMessage(sock, &address, &m)) {
115 switch(m.type) {
116 case CL_INFO: sv_register_player(address, m); break;
117 case CL_KILL: sv_kill_player(sv_find_client(address), false, ""); break;
118 case CL_DOES: sv_move_player(sv_find_client(address), m); break;
119 default: SDL_Log("invalid message %i", m.type);
124 static void sv_send() {
125 for(int i = 0; i < clientlimit; i++)
126 if(client[i].connected)
127 sv_sync_client(i, false);
129 for(int i = 0; i < clientlimit; i++)
130 g_player[i].updated = false;
132 for(int i = 0; i < MAX_BULLETS; i++)
133 g_bullet[i].updated = false;
136 void sv_start(uint16_t port) {
137 sock = OpenPort(port);
138 lastTime = SDL_GetTicks();
139 currTime = SDL_GetTicks();
142 void sv_stop() {
143 for(int i = 0; i < clientlimit; i++)
144 if(client[i].connected)
145 sv_kill_player(i, true, "Server shutdown");
146 ClosePort(sock);
149 void sv_setclientlimit(unsigned int maxclients) {
150 // TODO disconnect clients
151 clientlimit = (maxclients > MAX_PLAYERS) ? (MAX_PLAYERS) : (maxclients);
154 void sv_handle() {
155 if(currTime - lastTime >= TICK_DELAY) {
156 sv_recv();
157 g_update();
158 sv_send();
159 lastTime = SDL_GetTicks();
161 currTime = SDL_GetTicks();