Permalink
Browse files

- 16 (bzw jetzt nur noch 15) Bit Werte falls moeglich in einem Byte …

…uebertragen

 - Handshake Paket mit Protokoll Versionsnummer & Check der Version im Client
 - konsistente Benennung der Funktionen in client.c und server.c


git-svn-id: http://infon.googlecode.com/svn/trunk@23 8171fb75-e542-0410-96e4-03d5dd800671
  • Loading branch information...
1 parent dbb4807 commit d7dfbd46f125329fddc458ec2bfe3d8b2d85e3b9 @dividuum committed Jul 11, 2006
Showing with 147 additions and 117 deletions.
  1. +4 −1 Makefile
  2. +48 −31 client.c
  3. +5 −1 client.rb
  4. +1 −1 creature.c
  5. +3 −0 global.h
  6. +3 −3 infon.c
  7. +1 −1 listener.c
  8. +2 −2 misc.c
  9. +16 −15 packet.c
  10. +1 −0 packet.h
  11. +3 −3 player.c
  12. +1 −1 scroller.c
  13. +52 −51 server.c
  14. +5 −5 server.h
  15. +1 −1 video.c
  16. +1 −1 world.c
View
@@ -42,19 +42,22 @@ dist:
$(MAKE) linux-client-dist linux-server-dist
win32-client-dist: $(GUI_EXECUTABLE)
+ /opt/xmingw/bin/i386-mingw32msvc-strip $(GUI_EXECUTABLE)
zip infon-win32-r$(REVISION).zip $(GUI_EXECUTABLE) gfx/*.fnt gfx/*.gif gfx/*.png gfx/*.bmp
linux-client-dist: $(GUI_EXECUTABLE)
+ strip $(GUI_EXECUTABLE)
tar cfvz infon-linux-r$(REVISION).tgz $(GUI_EXECUTABLE) gfx/*.fnt gfx/*.gif gfx/*.png gfx/*.bmp
linux-server-dist: infond
+ strip infond infond-static
tar cfvz infond-linux-r$(REVISION).tgz infond infond-static *.lua
infond: lua-5.0.2/lib/liblua.a infond.o server.o listener.o map.o path.o misc.o packet.o player.o world.o creature.o scroller.o
$(CC) $^ $(LDFLAGS) -o $@
$(CC) $^ $(LDFLAGS) -static -o $@-static
-$(GUI_EXECUTABLE): lua-5.0.2/lib/liblua.a infon.o client.o packet.o misc.o gui_player.o gui_world.o gui_creature.o gui_scroller.o video.o sprite.o $(RES)
+$(GUI_EXECUTABLE): infon.o client.o packet.o misc.o gui_player.o gui_world.o gui_creature.o gui_scroller.o video.o sprite.o $(RES)
$(CC) $^ $(GUI_LDFLAGS) -o $@
infon.res: infon.rc
View
@@ -42,24 +42,38 @@
#include "packet.h"
#include "global.h"
-#include "server.h"
+#include "client.h"
#include "misc.h"
#include "gui_player.h"
#include "gui_world.h"
#include "gui_scroller.h"
#include "gui_creature.h"
-static int serverfd;
+static int clientfd;
static struct event rd_event;
static struct event wr_event;
static struct evbuffer *in_buf;
static struct evbuffer *out_buf;
-void server_destroy(char *reason);
-void server_writeto(const void *data, size_t size);
+void client_destroy(char *reason);
+void client_writeto(const void *data, size_t size);
int client_is_connected();
-static void server_handle_packet(packet_t *packet) {
+static void client_read_handshake(packet_t *packet) {
+ uint8_t serverprotocol;
+ if (!packet_read08(packet, &serverprotocol)) goto failed;
+ if (serverprotocol != PROTOCOL_VERSION) {
+ die("server has %s protocol version %d. I have %d.\n"
+ "visit the infon homepage for more information.",
+ serverprotocol < PROTOCOL_VERSION ? "older" : "newer",
+ serverprotocol, PROTOCOL_VERSION);
+ }
+ return;
+failed:
+ printf("parsing player update packet failed\n");
+}
+
+static void client_handle_packet(packet_t *packet) {
//printf("ptype=%d\n", packet->type);
switch (packet->type) {
case PACKET_PLAYER_UPDATE:
@@ -75,39 +89,42 @@ static void server_handle_packet(packet_t *packet) {
gui_scroller_from_network(packet);
break;
case PACKET_WELCOME_MSG:
- server_writeto("guiclient\n", 10);
+ client_writeto("guiclient\n", 10);
break;
case PACKET_QUIT_MSG:
printf("server wants us to disconnect: %.*s\n",
packet->len, packet->data);
- server_destroy("done");
+ client_destroy("done");
break;
case PACKET_KOTH_UPDATE:
gui_player_king_from_network(packet);
break;
+ case PACKET_HANDSHAKE:
+ client_read_handshake(packet);
+ break;
default:
printf("packet->type %d unknown\n", packet->type);
break;
}
}
-static void server_readable(int fd, short event, void *arg) {
+static void client_readable(int fd, short event, void *arg) {
struct event *cb_event = arg;
int ret = evbuffer_read(in_buf, fd, 260);
if (ret < 0) {
- server_destroy(strerror(errno));
+ client_destroy(strerror(errno));
} else if (ret == 0) {
- server_destroy("eof reached");
+ client_destroy("eof reached");
} else if (EVBUFFER_LENGTH(in_buf) > 8192) {
- server_destroy("line too long. strange server.");
+ client_destroy("line too long. strange server.");
} else {
while (EVBUFFER_LENGTH(in_buf) >= (int)EVBUFFER_DATA(in_buf)[0] + 2) {
packet_t packet;
memcpy(&packet,EVBUFFER_DATA(in_buf), (int)EVBUFFER_DATA(in_buf)[0] + 2);
int len = packet.len + 2;
packet_rewind(&packet);
- server_handle_packet(&packet);
+ client_handle_packet(&packet);
if (!client_is_connected())
return;
evbuffer_drain(in_buf, len);
@@ -116,21 +133,21 @@ static void server_readable(int fd, short event, void *arg) {
}
}
-static void server_writable(int fd, short event, void *arg) {
+static void client_writable(int fd, short event, void *arg) {
struct event *cb_event = arg;
int ret = evbuffer_write(out_buf, fd);
if (ret < 0) {
- server_destroy(strerror(errno));
+ client_destroy(strerror(errno));
} else if (ret == 0) {
- server_destroy("null write?");
+ client_destroy("null write?");
} else {
if (EVBUFFER_LENGTH(out_buf) > 0)
event_add(cb_event, NULL);
}
}
-void server_writeto(const void *data, size_t size) {
+void client_writeto(const void *data, size_t size) {
if (size == 0)
return;
if (EVBUFFER_LENGTH(in_buf) > 1024*1024)
@@ -139,19 +156,19 @@ void server_writeto(const void *data, size_t size) {
event_add(&wr_event, NULL);
}
-void server_destroy(char *reason) {
- assert(serverfd != -1);
+void client_destroy(char *reason) {
+ assert(clientfd != -1);
printf("disconnected from server: %s\n", reason);
evbuffer_free(in_buf);
evbuffer_free(out_buf);
event_del(&rd_event);
event_del(&wr_event);
- close(serverfd);
- serverfd = -1;
+ close(clientfd);
+ clientfd = -1;
}
int client_is_connected() {
- return serverfd != -1;
+ return clientfd != -1;
}
void client_tick() {
@@ -206,22 +223,22 @@ void client_init(char *addr) {
fprintf(stderr, "connecting to %s:%d (%s:%d)\n", addr, port, inet_ntoa(serveraddr.sin_addr), port);
/* Socket erzeugen */
- serverfd = socket(AF_INET, SOCK_STREAM, 0);
+ clientfd = socket(AF_INET, SOCK_STREAM, 0);
/* Fehler beim Socket erzeugen? */
#ifdef WIN32
- if (serverfd == INVALID_SOCKET)
+ if (clientfd == INVALID_SOCKET)
die("cannot open socket: Error %d", WSAGetLastError());
#else
- if (serverfd == -1)
+ if (clientfd == -1)
die("cannot open socket: %s", strerror(errno));
#endif
#ifdef WIN32
- if (connect(serverfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)
+ if (connect(clientfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)
die("cannot connect socket: Error %d", WSAGetLastError());
#else
- if (connect(serverfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
+ if (connect(clientfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
die("cannot connect socket: %s", strerror(errno));
#endif
@@ -230,14 +247,14 @@ void client_init(char *addr) {
/* Non Blocking setzen */
#ifdef WIN32
DWORD notblock = 1;
- ioctlsocket(serverfd, FIONBIO, &notblock);
+ ioctlsocket(clientfd, FIONBIO, &notblock);
#else
- if (fcntl(serverfd, F_SETFL, O_NONBLOCK) < 0)
+ if (fcntl(clientfd, F_SETFL, O_NONBLOCK) < 0)
die("cannot set socket nonblocking: %s", strerror(errno));
#endif
- event_set(&rd_event, serverfd, EV_READ, server_readable, &rd_event);
- event_set(&wr_event, serverfd, EV_WRITE, server_writable, &wr_event);
+ event_set(&rd_event, clientfd, EV_READ, client_readable, &rd_event);
+ event_set(&wr_event, clientfd, EV_WRITE, client_writable, &wr_event);
in_buf = evbuffer_new();
out_buf = evbuffer_new();
@@ -246,7 +263,7 @@ void client_init(char *addr) {
void client_shutdown() {
if (client_is_connected())
- server_destroy("shutdown");
+ client_destroy("shutdown");
#ifdef WIN32
WSACleanup();
#endif
View
@@ -15,7 +15,9 @@ def read8
end
def read16
- read(2).unpack("n")[0]
+ ret = read8
+ ret |= read8 << 7 if ret & 0x80 != 0
+ ret
end
def read32
@@ -67,6 +69,8 @@ def read32
when 32:
socket.write("guiclient\n")
puts "welcome: %s" % socket.read(len).delete("\n").strip
+ when 255:
+ puts "protocol %d" % socket.read8
else
puts "???: #{type}"
end
View
@@ -856,7 +856,7 @@ void creature_to_network(creature_t *creature, int dirtymask, client_t *client)
if (dirtymask & CREATURE_DIRTY_SPEED)
packet_write08(&packet, creature->network_speed);
- client_send_packet(&packet, client);
+ server_send_packet(&packet, client);
}
void creature_init() {
View
@@ -40,4 +40,7 @@ int game_round;
// der Tiles komische Pfade.
#define PATHFIND_AREA_MERGE
+// Netzwerk Protokol
+#define PROTOCOL_VERSION 1
+
#endif
View
@@ -158,9 +158,9 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument, int nFunsterStil)
{
if (strlen(lpszArgument) == 0) {
- die("Programm muss als erster Parameter der\n"
- "Hostname des Servers uebergeben werden.\n\n"
- "Beispiel: 'infon bl0rg.net'");
+ die("you must supply the gameserver hostname\n"
+ "as command line parameter.\n\n"
+ "example: 'infon.exe bl0rg.net'");
}
char *argv[] = { "foobar", lpszArgument, NULL };
return main(2, argv);
View
@@ -60,7 +60,7 @@ static void listener_cb(int fd, short event, void *arg) {
goto error;
}
- if (!client_accept(clientfd, &peer))
+ if (!server_accept(clientfd, &peer))
goto error;
event_add(cb_event, NULL);
View
4 misc.c
@@ -37,9 +37,9 @@ void die(char *fmt, ...) {
#else
va_list ap;
va_start(ap, fmt);
- printf("FATAL: ");
+ printf("--[ FATAL ERROR ]-----------\n");
vprintf(fmt, ap);
- printf("\n");
+ printf("\n----------------------------\n");
va_end(ap);
#endif
exit(1);
View
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <string.h>
+#include <assert.h>
#include "packet.h"
@@ -48,17 +49,18 @@ int packet_read08(packet_t *packet, uint8_t *data) {
return 1;
}
-int packet_read16(packet_t *packet, uint16_t *data) {
- if (packet->len - packet->offset < 2) {
- printf("reading beyond end\n");
- return 0;
+int packet_read16(packet_t *packet, uint16_t *data) {
+ uint8_t byte;
+ if (!packet_read08(packet, &byte)) return 0;
+ *data = byte & 0x7F;
+ if (byte & 0x80) {
+ if (!packet_read08(packet, &byte)) return 0;
+ *data |= byte << 7;
}
- *data = ntohs(*(uint16_t*)&packet->data[packet->offset]);
- packet->offset += 2;
return 1;
}
-int packet_read32(packet_t *packet, uint32_t *data) {
+int packet_read32(packet_t *packet, uint32_t *data) {
if (packet->len - packet->offset < 4) {
printf("reading beyond end\n");
return 0;
@@ -68,7 +70,7 @@ int packet_read32(packet_t *packet, uint32_t *data) {
return 1;
}
-int packet_readXX(packet_t *packet, void *data, int len) {
+int packet_readXX(packet_t *packet, void *data, int len) {
if (packet->len - packet->offset < len) {
printf("reading beyond end\n");
return 0;
@@ -89,13 +91,12 @@ int packet_write08(packet_t *packet, uint8_t data) {
}
int packet_write16(packet_t *packet, uint16_t data) {
- if (sizeof(packet->data) - packet->offset <= 2) {
- printf("packet too full\n");
- return 0;
- }
- *((uint16_t*)&packet->data[packet->offset]) = htons(data);
- packet->offset += 2;
- return 1;
+ assert(data <= 0x7FFF);
+ if (data <= 0x7F)
+ return packet_write08(packet, data);
+ else
+ return packet_write08(packet, (data & 0x7F) | 0x80) &&
+ packet_write08(packet, data >> 7);
}
int packet_write32(packet_t *packet, uint32_t data) {
View
@@ -35,6 +35,7 @@ typedef struct {
#define PACKET_QUIT_MSG 4
#define PACKET_KOTH_UPDATE 5
#define PACKET_WELCOME_MSG 32
+#define PACKET_HANDSHAKE 255
uint8_t data[256];
// Mgmt Data
uint8_t offset;
View
@@ -508,7 +508,7 @@ void player_writeto(player_t *player, const void *data, size_t size) {
client_t *client = player->clients;
do {
- client_writeto(client, data, size);
+ server_writeto(client, data, size);
client = client->next;
} while (client != player->clients);
}
@@ -736,7 +736,7 @@ void player_send_king_update(client_t *client) {
packet_write08(&packet, player_num(king_player));
else
packet_write08(&packet, 0xFF);
- client_send_packet(&packet, client);
+ server_send_packet(&packet, client);
}
void player_is_king_of_the_hill(player_t *player, int delta) {
@@ -804,7 +804,7 @@ void player_to_network(player_t *player, int dirtymask, client_t *client) {
if (dirtymask & PLAYER_DIRTY_SCORE)
packet_write16(&packet, player->score - PLAYER_KICK_SCORE);
- client_send_packet(&packet, client);
+ server_send_packet(&packet, client);
}
Oops, something went wrong.

0 comments on commit d7dfbd4

Please sign in to comment.