Cube  Diff

Differences From Artifact [9e4179b5d8]:

To Artifact [0b1a4d40d0]:


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
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







-
+


















-
+




-
+







	[stream writeFormat:@"name \"%@\"\nteam \"%@\"\n", player1.name,
	        player1.team];
}

void
connects(OFString *servername)
{
	disconnect(1); // reset state
	disconnect(true, false); // reset state
	addserver(servername);

	conoutf(@"attempting to connect to %@", servername);
	ENetAddress address = { ENET_HOST_ANY, CUBE_SERVER_PORT };
	if (enet_address_set_host(&address, servername.UTF8String) < 0) {
		conoutf(@"could not resolve server %@", 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();
		disconnect(false, false);
	}
}

void
disconnect(int onlyclean, int async)
disconnect(bool onlyclean, bool async)
{
	if (clienthost) {
		if (!connecting && !disconnecting) {
			enet_peer_disconnect(clienthost->peers);
			enet_host_flush(clienthost);
			disconnecting = lastmillis;
		}
158
159
160
161
162
163
164
165

166
167
168
169
170
171
172
158
159
160
161
162
163
164

165
166
167
168
169
170
171
172







-
+







{
	if (!clienthost) {
		conoutf(@"not connected");
		return;
	}
	if (connecting) {
		conoutf(@"aborting connection attempt");
		disconnect();
		disconnect(false, false);
		return;
	}
	conoutf(@"attempting to disconnect...");
	disconnect(0, !disconnecting);
}

static OFString *ctext;
229
230
231
232
233
234
235
236

237
238
239
240
241
242
243
229
230
231
232
233
234
235

236
237
238
239
240
241
242
243







-
+







	[messages addObject:msg];
}

void
server_err()
{
	conoutf(@"server network error, disconnecting...");
	disconnect();
	disconnect(false, false);
}

int lastupdate = 0, lastping = 0;
OFString *toservermap;
bool senditemstoserver =
    false; // after a map change, since server doesn't have map data

378
379
380
381
382
383
384
385

386
387
388
389
390
391
392
378
379
380
381
382
383
384

385
386
387
388
389
390
391
392







-
+







		return;
	if (connecting && lastmillis / 3000 > connecting / 3000) {
		conoutf(@"attempting to connect...");
		connecting = lastmillis;
		++connattempts;
		if (connattempts > 3) {
			conoutf(@"could not connect to server");
			disconnect();
			disconnect(false, false);
			return;
		}
	}
	while (
	    clienthost != NULL && enet_host_service(clienthost, &event, 0) > 0)
		switch (event.type) {
		case ENET_EVENT_TYPE_CONNECT:
402
403
404
405
406
407
408
409

410
411
412
413
414
402
403
404
405
406
407
408

409
410
411
412
413
414







-
+





				localservertoclient(event.packet->data,
				    event.packet->dataLength);
			enet_packet_destroy(event.packet);
			break;

		case ENET_EVENT_TYPE_DISCONNECT:
			if (disconnecting)
				disconnect();
				disconnect(false, false);
			else
				server_err();
			return;
		}
}