Skip to content
This repository
Browse code

server ported to windows

git-svn-id: http://infon.googlecode.com/svn/trunk@211 8171fb75-e542-0410-96e4-03d5dd800671
  • Loading branch information...
commit 0bffba2988e437da1ecf0feda56350e967b45d8a 1 parent 52f6efb
Florian Wesch authored July 11, 2007
20  Makefile
@@ -23,7 +23,7 @@ ifdef WINDOWS
23 23
 endif
24 24
 
25 25
 ifdef OPTIMIZE
26  
-	CFLAGS += -O3 -fexpensive-optimizations -finline-functions -fomit-frame-pointer -DNDEBUG
  26
+	CFLAGS += -O3 -DNDEBUG
27 27
 else
28 28
 	CFLAGS += -ggdb
29 29
 endif
@@ -36,8 +36,9 @@ ifdef WINDOWS
36 36
 	CFLAGS           += -I$(MINGW)/include
37 37
 	WINDRES           = /opt/xmingw/bin/i386-mingw32msvc-windres
38 38
 	STRIP             = /opt/xmingw/bin/i386-mingw32msvc-strip
39  
-	LUAPLAT            = mingw
  39
+	LUAPLAT           = mingw
40 40
 	INFON_EXECUTABLE  = infon.exe
  41
+	INFOND_EXECUTABLE = infond.exe
41 42
 
42 43
 	SDL_RENDERER      = sdl_gui.dll
43 44
 	GL_RENDERER       = gl_gui.dll
@@ -76,6 +77,11 @@ $(INFON_EXECUTABLE) : LDFLAGS  += $(MINGW)/lib/libevent.a $(MINGW)/lib/libz.a \
76 77
                                   -lmingw32 $(MINGW)/lib/libSDLmain.a -lwsock32 -mwindows -Wl,-s
77 78
 $(INFON_EXECUTABLE) : infon.res
78 79
 
  80
+$(INFOND_EXECUTABLE): CFLAGS   += -I$(LUA)/src/ -DNO_CONSOLE_CLIENT
  81
+$(INFOND_EXECUTABLE): LDFLAGS  += $(MINGW)/lib/libevent.a $(MINGW)/lib/libz.a \
  82
+                                  -lmingw32 -lwsock32 -mconsole -Wl,-s
  83
+$(INFOND_EXECUTABLE): infon.res luacore.o
  84
+
79 85
 $(SDL_RENDERER)     : CFLAGS   += -I$(SDLDIR)/include/SDL 
80 86
 $(SDL_RENDERER)     : LDFLAGS  += $(MINGW)/lib/libSGE.a $(MINGW)/lib/libevent.a $(MINGW)/lib/libSDL_image.a \
81 87
                                   $(MINGW)/lib/libpng.a $(MINGW)/lib/libz.a     $(MINGW)/lib/libSDL_gfx.a $(MINGW)/lib/libSDL.a \
@@ -149,6 +155,8 @@ dist:
149 155
 	$(MAKE) clean
150 156
 	WINDOWS=1  $(MAKE) win32-client-dist
151 157
 	$(MAKE) clean
  158
+	WINDOWS=1  $(MAKE) win32-server-dist
  159
+	$(MAKE) clean
152 160
 	OPTIMIZE=1 $(MAKE) linux-client-dist
153 161
 	$(MAKE) clean
154 162
 	$(MAKE) linux-server-dist
@@ -165,6 +173,14 @@ win32-client-dist: $(INFON_EXECUTABLE) $(SDL_RENDERER) $(GL_RENDERER)
165 173
 		README.txt $^ gfx/*.fnt gfx/*.png gfx/*.bmp gfx/*.mdl \
166 174
 		contrib/bots/*.lua contrib/bots/*.txt
167 175
 
  176
+win32-server-dist: $(INFOND_EXECUTABLE)
  177
+	$(STRIP) $^
  178
+	upx -9 --all-methods $(INFOND_EXECUTABLE)
  179
+	zip infond-win32-r$(REVISION).zip \
  180
+		README.txt $(INFOND_EXECUTABLE) \
  181
+		*.lua level/*.lua rules/*.lua api/*.lua libs/*.lua \
  182
+		contrib/bots/*.lua contrib/bots/*.txt
  183
+
168 184
 linux-client-dist: $(INFON_EXECUTABLE) $(SDL_RENDERER) $(NULL_RENDERER) $(GL_RENDERER)
169 185
 	strip $^
170 186
 	tar cfvz infon-linux-i386-r$(REVISION).tgz \
29  client.c
@@ -30,15 +30,15 @@
30 30
 #include <sys/ioctl.h>
31 31
 #include <netdb.h>
32 32
 #endif
33  
-#include <sys/stat.h>
  33
+#include <assert.h>
  34
+#include <errno.h>
34 35
 #include <fcntl.h>
35  
-#include <stdlib.h>
36 36
 #include <stdarg.h>
37  
-#include <unistd.h>
38 37
 #include <stdio.h>
  38
+#include <stdlib.h>
39 39
 #include <string.h>
40  
-#include <errno.h>
41  
-#include <assert.h>
  40
+#include <sys/stat.h>
  41
+#include <unistd.h>
42 42
 
43 43
 #include <event.h>
44 44
 #include <zlib.h>
@@ -53,7 +53,7 @@
53 53
 #include "client_creature.h"
54 54
 #include "client_game.h"
55 55
 
56  
-static int              clientfd;
  56
+static int              clientfd = INVALID_SOCKET;
57 57
 static struct event     rd_event;
58 58
 static struct event     wr_event;
59 59
 static struct evbuffer *in_buf;
@@ -286,7 +286,7 @@ void client_printf(const char *fmt, ...) {
286 286
 }
287 287
 
288 288
 int  client_is_connected() {
289  
-    return clientfd != -1;
  289
+    return clientfd != INVALID_SOCKET;
290 290
 }
291 291
 
292 292
 void file_loop(int delta) {
@@ -363,11 +363,10 @@ int client_open_socket(char *addr) {
363 363
     int fd = socket(AF_INET, SOCK_STREAM, 0);
364 364
 
365 365
     /* Fehler beim Socket erzeugen? */
366  
-#ifdef WIN32
367 366
     if (fd == INVALID_SOCKET)
  367
+#ifdef WIN32
368 368
         die("cannot open socket: %s", ErrorString(WSAGetLastError()));
369 369
 #else
370  
-    if (fd == -1) 
371 370
         die("cannot open socket: %s", strerror(errno));
372 371
 #endif
373 372
 
@@ -436,7 +435,7 @@ void client_init(char *source) {
436 435
 }
437 436
 
438 437
 void client_destroy(const char *reason) {
439  
-    assert(clientfd != -1);
  438
+    assert(clientfd != INVALID_SOCKET);
440 439
     fprintf(stderr, "datasource destroyed: %s\n", reason);
441 440
 
442 441
     evbuffer_free(in_buf);
@@ -446,8 +445,16 @@ void client_destroy(const char *reason) {
446 445
     if (compression) 
447 446
         inflateEnd(&strm);
448 447
 
  448
+#ifdef WIN32
  449
+    if (is_file_source) {
  450
+        close(clientfd);
  451
+    } else {
  452
+        closesocket(clientfd);
  453
+    }
  454
+#else
449 455
     close(clientfd);
450  
-    clientfd = -1;
  456
+#endif
  457
+    clientfd = INVALID_SOCKET;
451 458
     
452 459
     if (!is_file_source) {
453 460
         event_del(&rd_event);
4  game.c
@@ -234,7 +234,11 @@ void game_one_game() {
234 234
                 lasttick = tick;
235 235
                 continue;
236 236
             } else if (delta < 100) {
  237
+#ifdef WIN32
  238
+                Sleep(max(95 - delta, 10));
  239
+#else
237 240
                 usleep(max(95000 - delta * 1000, 1000));
  241
+#endif
238 242
                 continue;
239 243
             }
240 244
         } else {
10  global.h
@@ -53,4 +53,14 @@
53 53
 #define DEFAULT_RENDERER sdl_gui
54 54
 #endif
55 55
 
  56
+#ifdef WIN32
  57
+#define PLATFORM "win32"
  58
+#else
  59
+#define PLATFORM "posix"
  60
+#endif
  61
+
  62
+#ifndef INVALID_SOCKET
  63
+#define INVALID_SOCKET -1
  64
+#endif
  65
+
56 66
 #endif
3  infond.c
@@ -65,7 +65,9 @@ int main(int argc, char *argv[]) {
65 65
 
66 66
     signal(SIGTERM, sighandler);
67 67
     signal(SIGINT,  sighandler);
  68
+#ifndef WIN32
68 69
     signal(SIGPIPE, SIG_IGN);
  70
+#endif
69 71
 
70 72
     srand(time(NULL));
71 73
 
@@ -73,6 +75,7 @@ int main(int argc, char *argv[]) {
73 75
     luaL_openlibs(L);
74 76
     
75 77
     lua_register_string_constant(L, PREFIX);
  78
+    lua_register_string_constant(L, PLATFORM);
76 79
 
77 80
     game_init();
78 81
     server_init();
12  infond.lua
@@ -438,6 +438,7 @@ function world_load(map)
438 438
         world_fill_all    = world_fill_all;
439 439
         world_tile_center = world_tile_center;
440 440
         game_time         = game_time;
  441
+        server_info       = server_info;
441 442
 
442 443
         level_spawn_point = world_find_digged_worldcoord;
443 444
 
@@ -778,3 +779,14 @@ dofile(PREFIX .. "server.lua")
778 779
 
779 780
 -- setup listen socket
780 781
 if config.listenaddr and config.listenport then start_listener() end 
  782
+
  783
+if PLATFORM == "win32" then
  784
+    print("\n")
  785
+    print(config.banner:match("\r\n") and config.banner or config.banner:gsub("\n", "\r\n"))
  786
+    print [[
  787
+
  788
+          The Infon Server is now running. Have fun!
  789
+
  790
+           To terminate it, just close this window.
  791
+]]
  792
+end
73  listener.c
@@ -18,11 +18,17 @@
18 18
 
19 19
 */
20 20
 
  21
+#ifdef WIN32
  22
+#include <winsock2.h>
  23
+#include <ws2tcpip.h>
  24
+#include <windows.h>
  25
+#else
21 26
 #include <netinet/tcp.h>
22 27
 #include <arpa/inet.h>
23 28
 #include <netinet/in.h>
24 29
 #include <sys/types.h>
25 30
 #include <sys/socket.h>
  31
+#endif
26 32
 #include <fcntl.h>
27 33
 #include <stdlib.h>
28 34
 #include <unistd.h>
@@ -32,13 +38,14 @@
32 38
 
33 39
 #include <event.h>
34 40
 
  41
+#include "misc.h"
35 42
 #include "infond.h"
36 43
 #include "global.h"
37 44
 #include "server.h"
38 45
 #include "listener.h"
39 46
 #include "server.h"
40 47
 
41  
-static int listenfd = -1;
  48
+static int listenfd = INVALID_SOCKET;
42 49
 static struct event listener_event;
43 50
 
44 51
 static void listener_cb(int fd, short event, void *arg) {
@@ -50,7 +57,7 @@ static void listener_cb(int fd, short event, void *arg) {
50 57
     /* Neue Verbindung accept()ieren */
51 58
     clientfd = accept(listenfd, (struct sockaddr*)&peer, &addrlen);
52 59
 
53  
-    if (clientfd == -1) {
  60
+    if (clientfd == INVALID_SOCKET) {
54 61
         /* Warning nur anzeigen, falls accept() fehlgeschlagen hat allerdings
55 62
            ohne dabei EAGAIN (was bei non-blocking sockets auftreten kann)
56 63
            zu melden. */
@@ -60,22 +67,31 @@ static void listener_cb(int fd, short event, void *arg) {
60 67
         goto error;
61 68
     }
62 69
 
  70
+#ifndef WIN32
63 71
     /* TCP_NODELAY setzen. Dadurch werden Daten fruehestmoeglich versendet */
64  
-    static const int one = 1;
  72
+    const int one = 1;
65 73
     if (setsockopt(clientfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0) {
66 74
         fprintf(stderr, "cannot enable TCP_NODELAY: %s\n", strerror(errno));
67 75
         goto error;
68 76
     }
  77
+#endif
69 78
 
70 79
     /* SO_LINGER setzen. Falls sich noch Daten in der Sendqueue der Verbindung
71 80
        befinden, werden diese verworfen. */
72  
-    static const struct linger l = { 1, 0 };
  81
+    const struct linger l = { 1, 0 };
  82
+#ifdef WIN32
  83
+    if (setsockopt(clientfd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == SOCKET_ERROR) {
  84
+        fprintf(stderr, "cannot set SO_LINGER: %s\n", ErrorString(WSAGetLastError()));
  85
+        goto error;
  86
+    }
  87
+#else
73 88
     if (setsockopt(clientfd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1) {
74 89
         fprintf(stderr, "cannot set SO_LINGER: %s\n", strerror(errno));
75 90
         goto error;
76 91
     }
  92
+#endif
77 93
 
78  
-    static char address[128];
  94
+    char address[128];
79 95
     sprintf(address, "ip4:%s:%d", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
80 96
 
81 97
     if (!server_accept(clientfd, address))
@@ -83,22 +99,30 @@ static void listener_cb(int fd, short event, void *arg) {
83 99
 
84 100
     return;
85 101
 error:
86  
-    if (clientfd != -1)
  102
+    if (clientfd != INVALID_SOCKET) 
  103
+#ifdef WIN32
  104
+        closesocket(clientfd);
  105
+#else
87 106
         close(clientfd);
  107
+#endif
88 108
 }
89 109
 
90 110
 void listener_shutdown() {
91  
-    if (listenfd == -1) 
  111
+    if (listenfd == INVALID_SOCKET) 
92 112
         return;
93 113
 
94 114
     event_del(&listener_event);
  115
+#ifdef WIN32
  116
+    closesocket(listenfd);
  117
+#else
95 118
     close(listenfd);
96  
-    listenfd = -1;
  119
+#endif
  120
+    listenfd = INVALID_SOCKET;
97 121
 }
98 122
 
99 123
 int listener_init(const char *listenaddr, int port) {
100 124
     struct sockaddr_in addr;
101  
-    static const int one = 1;
  125
+    const int one = 1;
102 126
 
103 127
     /* Alten Listener, falls vorhanden, schliessen */
104 128
     listener_shutdown();
@@ -117,17 +141,23 @@ int listener_init(const char *listenaddr, int port) {
117 141
     listenfd = socket(AF_INET, SOCK_STREAM, 0);
118 142
 
119 143
     /* Fehler beim Socket erzeugen? */
120  
-    if (listenfd == -1) {
  144
+    if (listenfd == INVALID_SOCKET) {
121 145
         fprintf(stderr, "cannot open socket: %s\n", strerror(errno));
122 146
         goto error;
123 147
     }
124 148
 
125 149
     /* Auf nonblocking setzen */
  150
+#ifdef WIN32
  151
+    DWORD notblock = 1;
  152
+    ioctlsocket(listenfd, FIONBIO, &notblock);
  153
+#else
126 154
     if (fcntl(listenfd, F_SETFL, O_NONBLOCK) == -1) {
127 155
         fprintf(stderr, "cannot set socket nonblocking: %s\n", strerror(errno));
128 156
         goto error;
129 157
     }
  158
+#endif
130 159
 
  160
+#ifndef WIN32
131 161
     /* SO_REUSEADDR verwenden. Falls sich zuvor ein Programm unschön
132 162
        beendet hat, so ist der port normalerweise für einen bestimmten
133 163
        Zeitrahmen weiterhin belegt. SO_REUSEADDR verwendet dann den
@@ -136,28 +166,47 @@ int listener_init(const char *listenaddr, int port) {
136 166
         fprintf(stderr, "cannot enable SO_REUSEADDR: %s\n", strerror(errno));
137 167
         goto error;
138 168
     }
  169
+#endif
139 170
 
140 171
     /* Socket bind()en */
  172
+#ifdef WIN32
  173
+    if (bind(listenfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  174
+        fprintf(stderr, "cannot bind socket: %s\n", ErrorString(WSAGetLastError()));
  175
+        goto error;
  176
+    }
  177
+#else
141 178
     if (bind(listenfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1) {
142 179
         fprintf(stderr, "cannot bind socket: %s\n", strerror(errno));
143 180
         goto error;
144 181
     }
  182
+#endif
145 183
 
146 184
     /* Und listen() mit einem backlog von 128 Verbindungen. Wobei
147 185
        dies eventuell ignoriert wird, falls SYN cookies aktiviert sind */
  186
+#ifdef WIN32
  187
+    if (listen(listenfd, 128) == SOCKET_ERROR) {
  188
+        fprintf(stderr, "cannot listen() on socket: %s\n", ErrorString(WSAGetLastError()));
  189
+        goto error;
  190
+    }
  191
+#else
148 192
     if (listen(listenfd, 128) == -1) {
149 193
         fprintf(stderr, "cannot listen() on socket: %s\n", strerror(errno));
150 194
         goto error;
151 195
     }
  196
+#endif
152 197
 
153 198
     event_set(&listener_event, listenfd, EV_READ | EV_PERSIST, listener_cb, &listener_event);
154 199
     event_add(&listener_event, NULL);
155 200
 
156 201
     return 1;
157 202
 error:
158  
-    if (listenfd != -1) {
  203
+    if (listenfd != INVALID_SOCKET) {
  204
+#ifdef WIN32
  205
+        closesocket(listenfd);
  206
+#else
159 207
         close(listenfd);
160  
-        listenfd = -1;
  208
+#endif
  209
+        listenfd = INVALID_SOCKET;
161 210
     }
162 211
     return 0;
163 212
 }
6  pinger.c
@@ -18,10 +18,16 @@
18 18
 
19 19
 */
20 20
 
  21
+#ifdef WIN32
  22
+#include <winsock2.h>
  23
+#include <ws2tcpip.h>
  24
+#include <windows.h>
  25
+#else
21 26
 #include <arpa/inet.h>
22 27
 #include <netinet/in.h>
23 28
 #include <sys/types.h>
24 29
 #include <sys/socket.h>
  30
+#endif
25 31
 #include <string.h>
26 32
 #include <unistd.h>
27 33
 
6  player.c
@@ -200,7 +200,9 @@ static int player_at_cpu_exceeded(lua_State *L) {
200 200
 }
201 201
 
202 202
 static player_t *alarm_player = NULL;
  203
+#ifndef WIN32
203 204
 static struct itimerval timer;
  205
+#endif
204 206
 
205 207
 static void alarm_signal(int sig) {
206 208
     assert(sig == SIGVTALRM);
@@ -227,6 +229,7 @@ static int player_call_user_lua(const char *where, player_t *player, int params)
227 229
 
228 230
     // Notfallzeitbeschraenkung einbauen. Kann auftreten, falls sehr viele
229 231
     // teure Lua Funktionen aufgerufen werden.
  232
+#ifndef WIN32
230 233
     alarm_player              = player;
231 234
     timer.it_interval.tv_sec  = 0;
232 235
     timer.it_interval.tv_usec = 0;
@@ -234,6 +237,7 @@ static int player_call_user_lua(const char *where, player_t *player, int params)
234 237
     timer.it_value.tv_usec    = 0;
235 238
     signal(SIGVTALRM, alarm_signal);
236 239
     setitimer(ITIMER_VIRTUAL, &timer, NULL);
  240
+#endif
237 241
 
238 242
     // Normalerweise liegt ein "cpu exceeded" Fehler an Ueberschreitung der lua VM Cycles.
239 243
     // Nur im CPU Rechenzeit-Fall wird diese Meldung entsprechend angepasst.
@@ -242,11 +246,13 @@ static int player_call_user_lua(const char *where, player_t *player, int params)
242 246
     // Usercode aufrufen
243 247
     int ret = lua_pcall(player->L, params, 0, -2 - params);
244 248
 
  249
+#ifndef WIN32
245 250
     // Alarm aufheben
246 251
     timer.it_value.tv_sec     = 0;
247 252
     timer.it_value.tv_usec    = 0;
248 253
     setitimer(ITIMER_VIRTUAL, &timer, NULL);
249 254
     alarm_player              = NULL;
  255
+#endif
250 256
 
251 257
     // Keine Speicherbeschraenkung mehr (ab hier wird die VM wieder von infon gesteuert)
252 258
     player->mem_enforce = 0;
79  server.c
@@ -18,19 +18,24 @@
18 18
 
19 19
 */
20 20
 
  21
+#ifdef WIN32
  22
+#include <winsock2.h>
  23
+#include <ws2tcpip.h>
  24
+#include <windows.h>
  25
+#else
21 26
 #include <netinet/tcp.h>
22 27
 #include <sys/socket.h>
23  
-#include <sys/ioctl.h>
24 28
 #include <sys/types.h>
25  
-#include <sys/stat.h>
  29
+#include <sys/ioctl.h>
  30
+#endif
  31
+#include <assert.h>
  32
+#include <errno.h>
26 33
 #include <fcntl.h>
27  
-#include <stdlib.h>
28  
-#include <unistd.h>
29 34
 #include <stdio.h>
  35
+#include <stdlib.h>
30 36
 #include <string.h>
31  
-#include <errno.h>
32  
-#include <assert.h>
33  
-#include <fcntl.h>
  37
+#include <sys/stat.h>
  38
+#include <unistd.h>
34 39
 
35 40
 #include <lauxlib.h>
36 41
 #include <event.h>
@@ -67,22 +72,34 @@ int client_num(client_t *client) {
67 72
 }
68 73
 
69 74
 client_t *server_accept(int fd, const char *address) {
70  
-    if (fd >= MAXCLIENTS) {
71  
-        fprintf(stderr, "cannot accept() new incoming connection: file descriptor too large\n");
72  
-        return NULL;
  75
+    client_t *client = clients;
  76
+
  77
+    for (int i = 0; i < MAXCLIENTS; i++, client++) {
  78
+        if (!CLIENT_USED(client))
  79
+            goto found;
73 80
     }
74  
-    
75  
-    memset(&clients[fd], 0, sizeof(client_t));
76  
-    client_t *client = &clients[fd];
  81
+
  82
+    // write(fd, "no free slot\r\n", 14);
  83
+    fprintf(stderr, "cannot accept() new incoming connection: no free slot\n");
  84
+    return NULL;
  85
+
  86
+found:
  87
+    memset(client, 0, sizeof(client_t));
  88
+    client->fd = fd;
77 89
 
78 90
     // File Writer wird leicht unterschiedlich behandelt
79 91
     client->is_file_writer = strstr(address, "special:file") == address;
80 92
 
81 93
     // Non Blocking setzen 
82  
-    if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
  94
+#ifdef WIN32
  95
+    DWORD notblock = 1;
  96
+    ioctlsocket(client->fd, FIONBIO, &notblock);
  97
+#else
  98
+    if (fcntl(client->fd, F_SETFL, O_NONBLOCK) < 0) {
83 99
         fprintf(stderr, "cannot set accept()ed socket nonblocking: %s\n", strerror(errno));
84 100
         return NULL;
85 101
     }
  102
+#endif
86 103
 
87 104
     // Soll Verbindung angenommen werden?
88 105
     lua_pushliteral(L, "on_new_client");
@@ -97,7 +114,7 @@ client_t *server_accept(int fd, const char *address) {
97 114
 
98 115
     if (!lua_toboolean(L, -2)) {
99 116
         size_t len; const char *msg = lua_tolstring(L, -1, &len);
100  
-        write(fd, msg, len);
  117
+        write(client->fd, msg, len);
101 118
         lua_pop(L, 2);
102 119
         return NULL;
103 120
     }
@@ -105,8 +122,8 @@ client_t *server_accept(int fd, const char *address) {
105 122
     lua_pop(L, 2);
106 123
 
107 124
     // Libevent aktivieren
108  
-    event_set(&client->rd_event, fd, EV_READ | EV_PERSIST, server_readable, &client->rd_event);
109  
-    event_set(&client->wr_event, fd, EV_WRITE            , server_writable, &client->wr_event);
  125
+    event_set(&client->rd_event, client->fd, EV_READ | EV_PERSIST, server_readable, client);
  126
+    event_set(&client->wr_event, client->fd, EV_WRITE            , server_writable, client);
110 127
     
111 128
     client->in_buf  = evbuffer_new();
112 129
     client->out_buf = evbuffer_new();
@@ -128,7 +145,7 @@ client_t *server_accept(int fd, const char *address) {
128 145
     // Annehmen
129 146
     lua_pushliteral(L, "on_client_accepted");
130 147
     lua_rawget(L, LUA_GLOBALSINDEX);
131  
-    lua_pushnumber(L, fd);
  148
+    lua_pushnumber(L, client_num(client));
132 149
     lua_pushstring(L, address);
133 150
 
134 151
     if (lua_pcall(L, 2, 0, 0) != 0) {
@@ -143,7 +160,7 @@ client_t *server_accept(int fd, const char *address) {
143 160
 }
144 161
 
145 162
 static void server_readable(int fd, short event, void *arg) {
146  
-    client_t *client = &clients[fd];
  163
+    client_t *client = (client_t*)arg;
147 164
 
148 165
     // Der Client wurde 'extern' gekickt, allerdings noch
149 166
     // nicht entfernt. Dann wird dieser Readcallback aufgerufen,
@@ -163,7 +180,7 @@ static void server_readable(int fd, short event, void *arg) {
163 180
         while ((line = evbuffer_readline(client->in_buf))) {
164 181
             lua_pushliteral(L, "on_client_input");   
165 182
             lua_rawget(L, LUA_GLOBALSINDEX);      
166  
-            lua_pushnumber(L, fd);               
  183
+            lua_pushnumber(L, client_num(client));
167 184
             lua_pushstring(L, line);
168 185
             free(line);
169 186
 
@@ -208,7 +225,7 @@ static void server_flush_compression(client_t *client) {
208 225
 }
209 226
 
210 227
 static void server_writable(int fd, short event, void *arg) {
211  
-    client_t *client = &clients[fd];
  228
+    client_t *client = (client_t*)arg;
212 229
 
213 230
 #ifndef NO_CONSOLE_CLIENT    
214 231
     // HACK um die Ausgabe des Consolenclients an
@@ -307,11 +324,9 @@ void server_send_packet(packet_t *packet, client_t *client) {
307 324
 }
308 325
 
309 326
 void server_destroy(client_t *client, const char *reason) {
310  
-    int fd = client_num(client);
311  
-
312 327
     lua_pushliteral(L, "on_client_close");
313 328
     lua_rawget(L, LUA_GLOBALSINDEX);
314  
-    lua_pushnumber(L, fd);
  329
+    lua_pushnumber(L, client_num(client));
315 330
     lua_pushstring(L, reason);
316 331
     if (lua_pcall(L, 2, 0, 0) != 0) {
317 332
         fprintf(stderr, "error calling on_client_close: %s\n", lua_tostring(L, -1));
@@ -337,7 +352,7 @@ void server_destroy(client_t *client, const char *reason) {
337 352
     // Bei Filewritern muss nichts geschrieben werden, da deren Daten
338 353
     // immer direkt rausgeschrieben werden.
339 354
     if (!client->is_file_writer) 
340  
-        evbuffer_write(client->out_buf, fd);
  355
+        evbuffer_write(client->out_buf, client->fd);
341 356
 
342 357
     evbuffer_free(client->in_buf);
343 358
     evbuffer_free(client->out_buf);
@@ -372,9 +387,17 @@ void server_destroy(client_t *client, const char *reason) {
372 387
     num_clients--;
373 388
     
374 389
 #ifndef NO_CONSOLE_CLIENT    
375  
-    if (fd != STDIN_FILENO)
  390
+    if (client->fd != STDIN_FILENO)
  391
+#endif
  392
+#ifdef WIN32
  393
+        if (client->is_file_writer) {
  394
+            close(client->fd);
  395
+        } else {
  396
+            closesocket(client->fd);
  397
+        }
  398
+#else
  399
+        close(client->fd);
376 400
 #endif
377  
-        close(fd);
378 401
 }
379 402
 
380 403
 static void initial_update(client_t *client) {
@@ -410,7 +433,7 @@ client_t *server_start_file_writer(const char *filename) {
410 433
     int fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC|O_EXCL, 0644);
411 434
     if (fd < 0)
412 435
         return NULL;
413  
-    static char address[512];
  436
+    char address[512];
414 437
     snprintf(address, sizeof(address), "special:file:%s", filename);
415 438
     return server_accept(fd, address);
416 439
 }
2  server.h
@@ -42,6 +42,8 @@
42 42
 #define SEND_BROADCAST NULL
43 43
 
44 44
 typedef struct client_s {
  45
+    int              fd;
  46
+
45 47
     struct event     rd_event;
46 48
     struct event     wr_event;
47 49
     struct evbuffer *in_buf;

0 notes on commit 0bffba2

Please sign in to comment.
Something went wrong with that request. Please try again.