Permalink
Browse files

work on msgpass

  • Loading branch information...
1 parent d52e317 commit 961c388f4eb25e56753bd585e81672e098989992 @fluffymormegil committed May 16, 2011
Showing with 60 additions and 47 deletions.
  1. +52 −37 src/msgpass.cc
  2. +8 −10 src/msgpass.hh
View
@@ -1,6 +1,5 @@
// msgpass.cc - message-passing functions for Martin's Dungeon Bash
-//
-// Copyright 2011 Martin Read
+// // Copyright 2011 Martin Read
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
@@ -31,9 +30,28 @@
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/uio.h>
+#include <signal.h>
+
+int client_socket;
+int engine_socket;
+int child_pid;
const uint32_t dunbash::Message::max_size = 16384;
+static void logger_kill();
+
+const char * const msg_tags[] = {
+ "no-op",
+ "continuation",
+ "shutdown",
+ "text message",
+ "command",
+ "dance",
+ "command request",
+ "dance request",
+ "tick report"
+};
+
ssize_t dunbash::Message::emit(int fd)
{
uint32_t tmptype = htonl(uint32_t(flavour));
@@ -46,46 +64,43 @@ ssize_t dunbash::Message::emit(int fd)
return writev(fd, outbound, 3);
}
-dunbash::Message *next_message(int fd)
+dunbash::Message kill_msg;
+
+static void logger_kill()
{
- uint32_t header_buf[2];
- ssize_t i;
- dunbash::Message *msg;
- i = read(fd, header_buf, 8);
- if (i != 8)
- {
- return 0;
- }
- msg = new dunbash::Message;
- uint32_t prospective_msg_type = htonl(header_buf[0]);
- uint32_t size = htonl(header_buf[1]);
- if (size > dunbash::Message::max_size)
- {
- // Either we've lost frame sync due to a bug, or we are being
- // attacked. Annoyingly, there is no handy "discard all data
- // available for reading from this socket" syscall.
- }
- msg->flavour = Msg_type(prospective_msg_type);
- msg->size = size;
- msg->payload = new unsigned char[size];
- i = read(fd, msg->payload, size);
- if (i == -1)
- {
- // Read error.
- delete msg;
- return 0;
- }
- else if (i == 0)
+ kill_msg.emit(engine_socket);
+}
+
+void msgpass_init()
+{
+ int sockets[2];
+ int i;
+ i = socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);
+ if (i < 0)
{
- // End of file. Shouldn't happen.
- delete msg;
- return 0;
+ perror("dungeonbash: msgpass_init: socketpair");
+ exit(1);
}
- else if (i < (ssize_t) size)
+ engine_socket = sockets[0];
+ client_socket = sockets[1];
+ kill_msg.flavour = Msg_type_shutdown;
+ kill_msg.size = 0;
+ i = fork();
+ switch (i)
{
- // hmmmm not sure what to do.
+ case 0:
+ close(engine_socket);
+ logger();
+ break;
+ case -1:
+ perror("dungeonbash: msgpass_init: fork");
+ exit(1);
+ default:
+ child_pid = i;
+ atexit(logger_kill);
+ close(client_socket);
+ break;
}
- return msg;
}
// msgpass.cc
View
@@ -31,6 +31,8 @@ enum Msg_type
{
Msg_type_no_op,
Msg_type_continuation,
+ Msg_type_shutdown,
+ Msg_type_text,
Msg_type_cmd,
Msg_type_dance,
Msg_type_cmdreq,
@@ -53,17 +55,13 @@ namespace dunbash
};
}
-// Client writes
-extern void write_cmd_message(int fd, Game_cmd cmd);
-extern void write_dance_message(int fd, Game_cmd cmd);
+extern int client_socket;
+extern int engine_socket;
+extern const char * const msg_tags[];
+extern void logger();
+extern void logger_process_msg(const dunbash::Message& msg);
-// Client reads
-
-// Engine writes
-
-// Engine reads
-extern void read_cmd_message(int fd, Game_cmd *pcmd);
-extern void read_dance_message(int fd, Game_cmd *pcmd);
+extern void msgpass_init();
#endif

0 comments on commit 961c388

Please sign in to comment.