summaryrefslogtreecommitdiff
path: root/src/net_loop.c
diff options
context:
space:
mode:
authorSimon Howard2005-10-30 19:56:15 +0000
committerSimon Howard2005-10-30 19:56:15 +0000
commit59c7b8446575847ddf9b22b9b93a4eb58b7fe042 (patch)
tree6adc4b3b47de4f090b2330f0e7b6dce660d0fae6 /src/net_loop.c
parent1e9ce0901c8439ebb3299b77690cd7cd4ccf25b0 (diff)
downloadchocolate-doom-59c7b8446575847ddf9b22b9b93a4eb58b7fe042.tar.gz
chocolate-doom-59c7b8446575847ddf9b22b9b93a4eb58b7fe042.tar.bz2
chocolate-doom-59c7b8446575847ddf9b22b9b93a4eb58b7fe042.zip
Add foundation code for the new networking system
Subversion-branch: /trunk/chocolate-doom Subversion-revision: 229
Diffstat (limited to 'src/net_loop.c')
-rw-r--r--src/net_loop.c227
1 files changed, 227 insertions, 0 deletions
diff --git a/src/net_loop.c b/src/net_loop.c
new file mode 100644
index 00000000..5dc3bbf8
--- /dev/null
+++ b/src/net_loop.c
@@ -0,0 +1,227 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// $Id: net_loop.c 229 2005-10-30 19:56:15Z fraggle $
+//
+// Copyright(C) 2005 Simon Howard
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+// $Log$
+// Revision 1.1 2005/10/30 19:56:15 fraggle
+// Add foundation code for the new networking system
+//
+//
+// DESCRIPTION:
+// Loopback network module for server compiled into the client
+//
+//-----------------------------------------------------------------------------
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "i_system.h"
+#include "net_defs.h"
+#include "net_loop.h"
+#include "net_packet.h"
+
+#define MAX_QUEUE_SIZE 16
+
+typedef struct
+{
+ net_packet_t *packets[MAX_QUEUE_SIZE];
+ int head, tail;
+} packet_queue_t;
+
+static packet_queue_t client_queue;
+static packet_queue_t server_queue;
+static net_addr_t client_addr;
+static net_addr_t server_addr;
+
+static void QueueInit(packet_queue_t *queue)
+{
+ queue->head = queue->tail = 0;
+}
+
+static void QueuePush(packet_queue_t *queue, net_packet_t *packet)
+{
+ int new_tail;
+
+ new_tail = (queue->tail + 1) % MAX_QUEUE_SIZE;
+
+ if (new_tail == queue->head)
+ {
+ // queue is full
+
+ return;
+ }
+
+ queue->packets[queue->tail] = packet;
+ queue->tail = new_tail;
+}
+
+static net_packet_t *QueuePop(packet_queue_t *queue)
+{
+ net_packet_t *packet;
+
+ if (queue->tail == queue->head)
+ {
+ // queue empty
+
+ return NULL;
+ }
+
+ packet = queue->packets[queue->head];
+ queue->head = (queue->head + 1) % MAX_QUEUE_SIZE;
+
+ return packet;
+}
+
+//-----------------------------------------------------------------------------
+//
+// Client end code
+//
+//-----------------------------------------------------------------------------
+
+static boolean NET_CL_InitClient(void)
+{
+ QueueInit(&client_queue);
+ client_addr.module = &net_loop_client_module;
+
+ return true;
+}
+
+static boolean NET_CL_InitServer(void)
+{
+ I_Error("NET_CL_InitServer: attempted to initialise client pipe end as a server!");
+ return false;
+}
+
+static void NET_CL_SendPacket(net_addr_t *addr, net_packet_t *packet)
+{
+ QueuePush(&server_queue, NET_PacketDup(packet));
+}
+
+static boolean NET_CL_RecvPacket(net_addr_t **addr, net_packet_t **packet)
+{
+ net_packet_t *popped;
+
+ popped = QueuePop(&client_queue);
+
+ if (popped != NULL)
+ {
+ *packet = popped;
+ *addr = &client_addr;
+
+ return true;
+ }
+
+ return false;
+}
+
+static void NET_CL_AddrToString(net_addr_t *addr, char *buffer, int buffer_len)
+{
+ snprintf(buffer, buffer_len, "local server");
+}
+
+static void NET_CL_FreeAddress(net_addr_t *addr)
+{
+}
+
+static net_addr_t *NET_CL_ResolveAddress(char *address)
+{
+ return &client_addr;
+}
+
+net_module_t net_loop_client_module =
+{
+ NET_CL_InitClient,
+ NET_CL_InitServer,
+ NET_CL_SendPacket,
+ NET_CL_RecvPacket,
+ NET_CL_AddrToString,
+ NET_CL_FreeAddress,
+ NET_CL_ResolveAddress,
+};
+
+//-----------------------------------------------------------------------------
+//
+// Server end code
+//
+//-----------------------------------------------------------------------------
+
+static boolean NET_SV_InitClient(void)
+{
+ I_Error("NET_SV_InitClient: attempted to initialise server pipe end as a client!");
+ return false;
+}
+
+static boolean NET_SV_InitServer(void)
+{
+ QueueInit(&server_queue);
+ server_addr.module = &net_loop_server_module;
+
+ return true;
+}
+
+static void NET_SV_SendPacket(net_addr_t *addr, net_packet_t *packet)
+{
+ QueuePush(&client_queue, NET_PacketDup(packet));
+}
+
+static boolean NET_SV_RecvPacket(net_addr_t **addr, net_packet_t **packet)
+{
+ net_packet_t *popped;
+
+ popped = QueuePop(&server_queue);
+
+ if (popped != NULL)
+ {
+ *packet = popped;
+ *addr = &server_addr;
+
+ return true;
+ }
+
+ return false;
+}
+
+static void NET_SV_AddrToString(net_addr_t *addr, char *buffer, int buffer_len)
+{
+ snprintf(buffer, buffer_len, "local client");
+}
+
+static void NET_SV_FreeAddress(net_addr_t *addr)
+{
+}
+
+static net_addr_t *NET_SV_ResolveAddress(char *address)
+{
+ return &server_addr;
+}
+
+net_module_t net_loop_server_module =
+{
+ NET_SV_InitClient,
+ NET_SV_InitServer,
+ NET_SV_SendPacket,
+ NET_SV_RecvPacket,
+ NET_SV_AddrToString,
+ NET_SV_FreeAddress,
+ NET_SV_ResolveAddress,
+};
+
+