forked from cloudflare/doom-wasm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
net_io.c
119 lines (94 loc) · 2.85 KB
/
net_io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Copyright(C) 2005-2014 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.
//
// DESCRIPTION:
// Network packet I/O. Base layer for sending/receiving packets,
// through the network module system
//
#include <stdio.h>
#include "debug.h"
#include "i_system.h"
#include "m_argv.h"
#include "net_defs.h"
#include "net_io.h"
#include "z_zone.h"
#define MAX_MODULES 16
struct _net_context_s {
net_module_t *modules[MAX_MODULES];
int num_modules;
};
net_addr_t net_broadcast_addr;
net_context_t *NET_NewContext(void)
{
net_context_t *context;
context = Z_Malloc(sizeof(net_context_t), PU_STATIC, 0);
context->num_modules = 0;
return context;
}
void NET_AddModule(net_context_t *context, net_module_t *module)
{
if (context->num_modules >= MAX_MODULES) {
I_Error("NET_AddModule: No more modules for context");
}
context->modules[context->num_modules] = module;
++context->num_modules;
}
void NET_SendPacket(net_addr_t *addr, net_packet_t *packet) { addr->module->SendPacket(addr, packet); }
void NET_SendBroadcast(net_context_t *context, net_packet_t *packet)
{
int i;
for (i = 0; i < context->num_modules; ++i) {
context->modules[i]->SendPacket(&net_broadcast_addr, packet);
}
}
boolean NET_RecvPacket(net_context_t *context, net_addr_t **addr, net_packet_t **packet)
{
int i;
// check all modules for new packets
for (i = 0; i < context->num_modules; ++i) {
// D(printf("net_io.c :: RecvPacket()\n"));
if (context->modules[i]->RecvPacket(addr, packet)) {
// D(printf("net_io.c :: NET_ReferenceAddress()\n"));
NET_ReferenceAddress(*addr);
return true;
}
}
return false;
}
// Note: this prints into a static buffer, calling again overwrites
// the first result
char *NET_AddrToString(net_addr_t *addr)
{
static char buf[128];
addr->module->AddrToString(addr, buf, sizeof(buf) - 1);
return buf;
}
void NET_ReferenceAddress(net_addr_t *addr)
{
if (addr == NULL) {
return;
}
++addr->refcount;
// printf("%s: +refcount=%d\n", NET_AddrToString(addr), addr->refcount);
}
void NET_ReleaseAddress(net_addr_t *addr)
{
if (addr == NULL) {
return;
}
--addr->refcount;
// printf("%s: -refcount=%d\n", NET_AddrToString(addr), addr->refcount);
if (addr->refcount <= 0) {
addr->module->FreeAddress(addr);
}
}