Add weapon cycling bindings for mouse and joystick buttons. Add weapon cycling bindi...
[chocolate-doom.git] / src / net_io.c
blob1d57661861380af46e444de99635d942718b6ca4
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2005 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
21 // DESCRIPTION:
22 // Network packet I/O. Base layer for sending/receiving packets,
23 // through the network module system
25 //-----------------------------------------------------------------------------
27 #include "i_system.h"
28 #include "net_defs.h"
29 #include "net_io.h"
30 #include "z_zone.h"
32 #define MAX_MODULES 16
34 struct _net_context_s
36 net_module_t *modules[MAX_MODULES];
37 int num_modules;
40 net_addr_t net_broadcast_addr;
42 net_context_t *NET_NewContext(void)
44 net_context_t *context;
46 context = Z_Malloc(sizeof(net_context_t), PU_STATIC, 0);
47 context->num_modules = 0;
49 return context;
52 void NET_AddModule(net_context_t *context, net_module_t *module)
54 if (context->num_modules >= MAX_MODULES)
56 I_Error("NET_AddModule: No more modules for context");
59 context->modules[context->num_modules] = module;
60 ++context->num_modules;
63 net_addr_t *NET_ResolveAddress(net_context_t *context, char *addr)
65 int i;
66 net_addr_t *result;
68 result = NULL;
70 for (i=0; i<context->num_modules; ++i)
72 result = context->modules[i]->ResolveAddress(addr);
74 if (result != NULL)
76 break;
80 return result;
83 void NET_SendPacket(net_addr_t *addr, net_packet_t *packet)
85 addr->module->SendPacket(addr, packet);
88 void NET_SendBroadcast(net_context_t *context, net_packet_t *packet)
90 int i;
92 for (i=0; i<context->num_modules; ++i)
94 context->modules[i]->SendPacket(&net_broadcast_addr, packet);
98 boolean NET_RecvPacket(net_context_t *context,
99 net_addr_t **addr,
100 net_packet_t **packet)
102 int i;
104 // check all modules for new packets
106 for (i=0; i<context->num_modules; ++i)
108 if (context->modules[i]->RecvPacket(addr, packet))
110 return true;
114 return false;
117 // Note: this prints into a static buffer, calling again overwrites
118 // the first result
120 char *NET_AddrToString(net_addr_t *addr)
122 static char buf[128];
124 addr->module->AddrToString(addr, buf, sizeof(buf) - 1);
126 return buf;
129 void NET_FreeAddress(net_addr_t *addr)
131 addr->module->FreeAddress(addr);