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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
int clientnum = -1; // our client id in the game
bool c2sinit = false; // whether we need to tell the other clients our stats
int
getclientnum()
{
return clientnum;
};
bool
multiplayer()
{
// check not correct on listen server?
if (clienthost)
conoutf("operation not available in multiplayer");
return clienthost != NULL;
};
bool
allowedittoggle()
{
bool allow = !clienthost || gamemode == 1;
if (!allow)
conoutf("editing in multiplayer requires coopedit mode (1)");
return allow;
};
VARF(rate, 0, 0, 25000,
if (clienthost && (!rate || rate > 1000))
enet_host_bandwidth_limit(clienthost, rate, rate));
void throttle();
VARF(throttle_interval, 0, 5, 30, throttle());
VARF(throttle_accel, 0, 2, 32, throttle());
VARF(throttle_decel, 0, 2, 32, throttle());
void
throttle()
{
if (!clienthost || connecting)
return;
assert(ENET_PEER_PACKET_THROTTLE_SCALE == 32);
enet_peer_throttle_configure(clienthost->peers,
throttle_interval * 1000, throttle_accel, throttle_decel);
};
void
newname(char *name)
{
c2sinit = false;
strn0cpy(player1->name, name, 16);
};
void
newteam(char *name)
{
c2sinit = false;
strn0cpy(player1->team, name, 5);
};
COMMANDN(team, newteam, ARG_1STR);
COMMANDN(name, newname, ARG_1STR);
void
writeclientinfo(FILE *f)
{
fprintf(f, "name \"%s\"\nteam \"%s\"\n", player1->name, player1->team);
};
void
connects(char *servername)
{
disconnect(1); // reset state
addserver(servername);
conoutf("attempting to connect to %s", servername);
ENetAddress address = {ENET_HOST_ANY, CUBE_SERVER_PORT};
if (enet_address_set_host(&address, servername) < 0) {
conoutf("could not resolve server %s", servername);
return;
};
clienthost = enet_host_create(NULL, 1, rate, rate);
if (clienthost) {
enet_host_connect(clienthost, &address, 1);
enet_host_flush(clienthost);
connecting = lastmillis;
connattempts = 0;
} else {
conoutf("could not connect to server");
disconnect();
};
};
void
disconnect(int onlyclean, int async)
{
if (clienthost) {
if (!connecting && !disconnecting) {
enet_peer_disconnect(clienthost->peers);
enet_host_flush(clienthost);
disconnecting = lastmillis;
};
if (clienthost->peers->state != ENET_PEER_STATE_DISCONNECTED) {
if (async)
return;
enet_peer_reset(clienthost->peers);
};
enet_host_destroy(clienthost);
};
if (clienthost && !connecting)
conoutf("disconnected");
clienthost = NULL;
connecting = 0;
connattempts = 0;
disconnecting = 0;
clientnum = -1;
c2sinit = false;
player1->lifesequence = 0;
loopv(players) zapdynent(players[i]);
localdisconnect();
if (!onlyclean) {
stop();
localconnect();
};
};
void
trydisconnect()
{
if (!clienthost) {
conoutf("not connected");
return;
};
if (connecting) {
conoutf("aborting connection attempt");
disconnect();
return;
};
conoutf("attempting to disconnect...");
disconnect(0, !disconnecting);
};
string ctext;
void
toserver(char *text)
{
conoutf("%s:\f %s", player1->name, text);
strn0cpy(ctext, text, 80);
};
void
echo(char *text)
{
conoutf("%s", text);
};
COMMAND(echo, ARG_VARI);
COMMANDN(say, toserver, ARG_VARI);
COMMANDN(connect, connects, ARG_1STR);
COMMANDN(disconnect, trydisconnect, ARG_NONE);
// collect c2s messages conveniently
|
<
>
|
>
<
>
>
|
>
<
>
<
>
>
|
<
>
<
>
|
|
|
<
<
>
>
|
<
<
>
>
|
<
>
|
<
>
|
<
>
|
>
|
|
<
>
|
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
int clientnum = -1; // our client id in the game
bool c2sinit = false; // whether we need to tell the other clients our stats
int
getclientnum()
{
return clientnum;
}
bool
multiplayer()
{
// check not correct on listen server?
if (clienthost)
conoutf(@"operation not available in multiplayer");
return clienthost != NULL;
}
bool
allowedittoggle()
{
bool allow = !clienthost || gamemode == 1;
if (!allow)
conoutf(@"editing in multiplayer requires coopedit mode (1)");
return allow;
}
VARF(rate, 0, 0, 25000,
if (clienthost && (!rate || rate > 1000))
enet_host_bandwidth_limit(clienthost, rate, rate));
void throttle();
VARF(throttle_interval, 0, 5, 30, throttle());
VARF(throttle_accel, 0, 2, 32, throttle());
VARF(throttle_decel, 0, 2, 32, throttle());
void
throttle()
{
if (!clienthost || connecting)
return;
assert(ENET_PEER_PACKET_THROTTLE_SCALE == 32);
enet_peer_throttle_configure(clienthost->peers,
throttle_interval * 1000, throttle_accel, throttle_decel);
}
void
newname(char *name)
{
c2sinit = false;
strn0cpy(player1->name, name, 16);
}
void
newteam(char *name)
{
c2sinit = false;
strn0cpy(player1->team, name, 5);
}
COMMANDN(team, newteam, ARG_1STR);
COMMANDN(name, newname, ARG_1STR);
void
writeclientinfo(FILE *f)
{
fprintf(f, "name \"%s\"\nteam \"%s\"\n", player1->name, player1->team);
}
void
connects(char *servername)
{
disconnect(1); // reset state
addserver(servername);
conoutf(@"attempting to connect to %s", servername);
ENetAddress address = {ENET_HOST_ANY, CUBE_SERVER_PORT};
if (enet_address_set_host(&address, servername) < 0) {
conoutf(@"could not resolve server %s", servername);
return;
};
clienthost = enet_host_create(NULL, 1, rate, rate);
if (clienthost) {
enet_host_connect(clienthost, &address, 1);
enet_host_flush(clienthost);
connecting = lastmillis;
connattempts = 0;
} else {
conoutf(@"could not connect to server");
disconnect();
}
}
void
disconnect(int onlyclean, int async)
{
if (clienthost) {
if (!connecting && !disconnecting) {
enet_peer_disconnect(clienthost->peers);
enet_host_flush(clienthost);
disconnecting = lastmillis;
};
if (clienthost->peers->state != ENET_PEER_STATE_DISCONNECTED) {
if (async)
return;
enet_peer_reset(clienthost->peers);
};
enet_host_destroy(clienthost);
};
if (clienthost && !connecting)
conoutf(@"disconnected");
clienthost = NULL;
connecting = 0;
connattempts = 0;
disconnecting = 0;
clientnum = -1;
c2sinit = false;
player1->lifesequence = 0;
loopv(players) zapdynent(players[i]);
localdisconnect();
if (!onlyclean) {
stop();
localconnect();
}
}
void
trydisconnect()
{
if (!clienthost) {
conoutf(@"not connected");
return;
}
if (connecting) {
conoutf(@"aborting connection attempt");
disconnect();
return;
}
conoutf(@"attempting to disconnect...");
disconnect(0, !disconnecting);
}
string ctext;
void
toserver(char *text)
{
conoutf(@"%s:\f %s", player1->name, text);
strn0cpy(ctext, text, 80);
}
void
echo(char *text)
{
conoutf(@"%s", text);
}
COMMAND(echo, ARG_VARI);
COMMANDN(say, toserver, ARG_VARI);
COMMANDN(connect, connects, ARG_1STR);
COMMANDN(disconnect, trydisconnect, ARG_NONE);
// collect c2s messages conveniently
|
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
return;
if (num != msgsizelookup(type)) {
sprintf_sd(s)("inconsistant msg size for %d (%d != %d)", type,
num, msgsizelookup(type));
fatal(s);
};
if (messages.length() == 100) {
conoutf("command flood protection (type %d)", type);
return;
};
ivector &msg = messages.add();
msg.add(num);
msg.add(rel);
msg.add(type);
va_list marker;
va_start(marker, type);
loopi(num - 1) msg.add(va_arg(marker, int));
va_end(marker);
};
void
server_err()
{
conoutf("server network error, disconnecting...");
disconnect();
};
int lastupdate = 0, lastping = 0;
string toservermap;
bool senditemstoserver =
false; // after a map change, since server doesn't have map data
string clientpassword;
|
|
<
>
|
<
>
|
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
return;
if (num != msgsizelookup(type)) {
sprintf_sd(s)("inconsistant msg size for %d (%d != %d)", type,
num, msgsizelookup(type));
fatal(s);
};
if (messages.length() == 100) {
conoutf(@"command flood protection (type %d)", type);
return;
};
ivector &msg = messages.add();
msg.add(num);
msg.add(rel);
msg.add(type);
va_list marker;
va_start(marker, type);
loopi(num - 1) msg.add(va_arg(marker, int));
va_end(marker);
}
void
server_err()
{
conoutf(@"server network error, disconnecting...");
disconnect();
}
int lastupdate = 0, lastping = 0;
string toservermap;
bool senditemstoserver =
false; // after a map change, since server doesn't have map data
string clientpassword;
|