File: OOConsoleConnection.c

package info (click to toggle)
oolite 1.77.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,264 kB
  • ctags: 5,362
  • sloc: objc: 132,090; ansic: 10,457; python: 2,225; sh: 1,325; makefile: 332; perl: 259; xml: 125; php: 5
file content (606 lines) | stat: -rw-r--r-- 19,205 bytes parent folder | download
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
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
180
181
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#include "OOConsoleConnection.h"
#include "OOTCPStreamDecoder.h"
#include "OODebugTCPConsoleProtocol.h"
#include "JAAutoreleasePool.h"

#include <stdlib.h>


#define LOG_PACKETS		0


#if LOG_PACKETS
static void LogPacket(CFStringRef label, CFDictionaryRef packet);
#else
#define LogPacket(label, packet) do {} while (0)
#endif


typedef enum
{
	kStateWaitingForConnection,
	kStateConnected,
	kStateClosing,
	kStateDisconnected
} OOConsoleConnectionState;


struct OOConsoleConnection
{
	OOTCPStreamDecoderRef			decoder;
	
	OOConsoleConnectionCallbacks	cb;
	void							*cbInfo;
	
	uint8_t							vFormat, vMajor, vMinor, noteUnknownPackets;
	
	CFStringRef						consoleIdentityString;
	CFMutableDictionaryRef			configuration;
	OOConsoleConnectionState		state;
};


static void Error(OOConsoleConnectionRef connection, OOALStringRef format, ...);

static void SendWithOneParameter(OOConsoleConnectionRef connection, CFStringRef packetType, CFStringRef paramKey, CFPropertyListRef paramValue);
// static void SendWithDictionary(OOConsoleConnectionRef connection, CFStringRef packetType, CFDictionaryRef parameters);
static void SendPacketDictionary(OOConsoleConnectionRef connection, CFDictionaryRef packet);

static OOConsoleRGBColor GetSpecificColor(OOConsoleConnectionRef connection, CFStringRef colorKey);

static void DecoderPacket(void *cbInfo, OOALStringRef packetType, OOALDictionaryRef packet);
static void DecoderError(void *cbInfo, OOALStringRef errorDesc);

static void HandleRequestConnectionPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandleCloseConnectionPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandleConsoleOutputPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandleClearConsolePacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandleShowConsolePacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandleNoteConfigurationPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandleNoteConfigurationChangePacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandlePingPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);
static void HandlePongPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet);

static void RejectConnection(OOConsoleConnectionRef connection, CFStringRef message);


OOConsoleConnectionRef OOConsoleConnectionCreate(CFStringRef consoleIdentityString, OOConsoleConnectionCallbacks *callbacks, void *cbInfo)
{
	OOConsoleConnectionRef		result = NULL;
	
	// Sanity check: we need at least SendData and ConsoleOutput callbacks to be meaningful.
	if (callbacks == NULL || callbacks->SendData == NULL || callbacks->ConsoleOutput == NULL)  return NULL;
	
	result = malloc(sizeof *result);
	if (result == NULL)  return result;
	
	result->decoder = OOTCPStreamDecoderCreate(DecoderPacket, DecoderError, NULL, result);
	if (result->decoder == NULL)
	{
		free(result);
		return NULL;
	}
	
	result->cb = *callbacks;
	result->cbInfo = cbInfo;
	result->consoleIdentityString = consoleIdentityString ? CFRetain(consoleIdentityString) : NULL;
	result->state = kStateWaitingForConnection;
	
	return result;
}


void OOConsoleConnectionDestroy(OOConsoleConnectionRef connection)
{
	if (connection == NULL)  return;
	
	if (connection->state == kStateConnected)  OOConsoleConnectionClose(connection, CFSTR("Server-side connection object destroyed while open."));
	
	if (connection->cb.Finalize != NULL)  connection->cb.Finalize(connection->cbInfo);
	if (connection->consoleIdentityString != NULL)  CFRelease(connection->consoleIdentityString);
	if (connection->configuration != NULL)  CFRelease(connection->configuration);
	
	free(connection);
}


void OOConsoleConnectionClose(OOConsoleConnectionRef connection, CFStringRef message)
{
	if (connection != NULL && connection->state == kStateConnected)
	{
		SendWithOneParameter(connection, kOOTCPPacket_CloseConnection, kOOTCPMessage, message);
		connection->state = kStateDisconnected;
	}
}


Boolean OOConsoleConnectionIsOpen(OOConsoleConnectionRef connection)
{
	return connection != NULL && connection->state == kStateConnected;
}


void OOConsoleConnectionReceiveData(OOConsoleConnectionRef connection, CFDataRef data)
{
	if (connection == NULL || data == NULL || connection->state == kStateDisconnected)  return;
	
	OOConsoleConnectionReceiveBytes(connection, OOALDataGetBytePtr(data), OOALDataGetLength(data));
}

void OOConsoleConnectionReceiveBytes(OOConsoleConnectionRef connection, const void *bytes, size_t length)
{
	if (connection == NULL)  return;
	
	OOTCPStreamDecoderReceiveBytes(connection->decoder, bytes, length);
}


void OOConsoleConnectionPerformCommand(OOConsoleConnectionRef connection, CFStringRef command)
{
	if (connection == NULL || command == NULL)  return;
	
	if (connection->state != kStateConnected)
	{
		Error(connection, CFSTR("Attempt to send command to console when not connected."));
		return;
	}
	
	SendWithOneParameter(connection, kOOTCPPacket_PerformCommand, kOOTCPMessage, command);
}


CFPropertyListRef OOConsoleConnectionGetConfiguration(OOConsoleConnectionRef connection, CFStringRef key)
{
	if (!OOConsoleConnectionIsOpen(connection) || connection->configuration == NULL || key == NULL)  return NULL;
	
	return CFDictionaryGetValue(connection->configuration, key);
}


void OOConsoleConnectionSetConfiguration(OOConsoleConnectionRef connection, CFStringRef key, CFPropertyListRef value)
{
	CFDictionaryRef					changed = NULL;
	CFArrayRef						removed = NULL;
	
	if (!OOConsoleConnectionIsOpen(connection) || key == NULL)  return;
	
	if (value != NULL)
	{
		changed = CFDictionaryCreate(kCFAllocatorDefault, (const void **)&key, (const void **)&value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
		SendWithOneParameter(connection, kOOTCPPacket_NoteConfigurationChange, kOOTCPConfiguration, changed);
		CFRelease(changed);
	}
	else
	{
		removed = CFArrayCreate(kCFAllocatorDefault, (const void **)&key, 1, &kCFTypeArrayCallBacks);
		SendWithOneParameter(connection, kOOTCPPacket_NoteConfigurationChange, kOOTCPRemovedConfigurationKeys, removed);
		CFRelease(changed);
	}
}


OOConsoleRGBColor OOConsoleConnectionGetForegroundColor(OOConsoleConnectionRef connection, CFStringRef colorKey)
{
	OOConsoleRGBColor				color;
	
	color = GetSpecificColor(connection, CFFMTSTR("-foreground-color", colorKey));
	if (color.r < -1)  color = GetSpecificColor(connection, CFFMTSTR("-foreground-colour", colorKey));
	if (color.r < -1)  color = GetSpecificColor(connection, CFSTR("general-foreground-color"));
	if (color.r < -1)  color = GetSpecificColor(connection, CFSTR("general-foreground-colour"));
	if (color.r < -1)
	{
		color.r = color.g = color.b = 0.0f;
	}
	
	return color;
}


OOConsoleRGBColor OOConsoleConnectionGetBackgroundColor(OOConsoleConnectionRef connection, CFStringRef colorKey)
{
	OOConsoleRGBColor				color;
	
	color = GetSpecificColor(connection, CFFMTSTR("-background-color", colorKey));
	if (color.r < -1)  color = GetSpecificColor(connection, CFFMTSTR("-background-colour", colorKey));
	if (color.r < -1)  color = GetSpecificColor(connection, CFSTR("general-background-color"));
	if (color.r < -1)  color = GetSpecificColor(connection, CFSTR("general-background-colour"));
	if (color.r < -1)
	{
		color.r = color.g = color.b = 1.0f;
	}
	
	return color;
}


static void Error(OOConsoleConnectionRef connection, OOALStringRef format, ...)
{
	va_list							args;
	CFStringRef						string = NULL;
	
	if (connection == NULL || connection->cb.Error == NULL || format == NULL)  return;
	
	va_start(args, format);
	string = CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, NULL, format, args);
	va_end(args);
	
	if (string != NULL)
	{
		connection->cb.Error(connection->cbInfo, string);
		CFRelease(string);
	}
}


static void SendWithOneParameter(OOConsoleConnectionRef connection, CFStringRef packetType, CFStringRef paramKey, CFPropertyListRef paramValue)
{
	CFDictionaryRef					packet = NULL;
	const void						*keys[2] = { kOOTCPPacketType, paramKey };
	const void						*values[2] = { packetType, paramValue };
	
	if (connection == NULL || packetType == NULL)  return;
	
	packet = CFDictionaryCreate(kCFAllocatorDefault, keys, values, (paramKey != NULL && paramValue != NULL) ? 2 : 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
	if (packet != NULL)
	{
		SendPacketDictionary(connection, packet);
		CFRelease(packet);
	}
}

/*
static void SendWithDictionary(OOConsoleConnectionRef connection, CFStringRef packetType, CFDictionaryRef parameters)
{
	CFMutableDictionaryRef			packet = NULL;
	
	if (connection == NULL || packetType == NULL)  return;
	if (parameters != NULL)
	{
		packet = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, CFDictionaryGetCount(parameters) + 1, parameters);
		if (packet != NULL)
		{
			SendPacketDictionary(connection, packet);
			CFRelease(packet);
		}
	}
	else
	{
		SendWithOneParameter(connection, packetType, NULL, NULL);
	}
}*/


static void SendPacketDictionary(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	CFDataRef						packetData = NULL;
	uint32_t						length;
	uint8_t							header[4];
	
	if (connection == NULL || packet == NULL)  return;
	
	packetData = CFPropertyListCreateXMLData(kCFAllocatorDefault, packet);
	if (packetData != NULL)
	{
		LogPacket(CFSTR("OUT"), packet);
		
		length = CFDataGetLength(packetData);
		if (length != 0)
		{
			header[0] = (length & 0xFF000000) >> 24;
			header[1] = (length & 0x00FF0000) >> 16;
			header[2] = (length & 0x0000FF00) >> 8;
			header[3] = length & 0x000000FF;
			
			connection->cb.SendData(connection->cbInfo, header, 4);
			connection->cb.SendData(connection->cbInfo, CFDataGetBytePtr(packetData), length);
		}
		CFRelease(packetData);
	}
}


static OOConsoleRGBColor GetSpecificColor(OOConsoleConnectionRef connection, CFStringRef colorKey)
{
	OOConsoleRGBColor				color = { -2.0f, 0.0f, 0.0f };
	CFArrayRef						components = NULL;
	CFNumberRef						r = NULL;
	CFNumberRef						g = NULL;
	CFNumberRef						b = NULL;
	
	components = OOConsoleConnectionGetConfiguration(connection, colorKey);
	if (components != NULL && CFGetTypeID(components) == CFArrayGetTypeID() && CFArrayGetCount(components) >= 3)
	{
		r = CFArrayGetValueAtIndex(components, 0);
		g = CFArrayGetValueAtIndex(components, 1);
		b = CFArrayGetValueAtIndex(components, 2);
		if (CFGetTypeID(r) == CFNumberGetTypeID() &&
			CFGetTypeID(g) == CFNumberGetTypeID() &&
			CFGetTypeID(b) == CFNumberGetTypeID())
		{
			CFNumberGetValue(r, kCFNumberFloatType, &color.r);
			CFNumberGetValue(g, kCFNumberFloatType, &color.g);
			CFNumberGetValue(b, kCFNumberFloatType, &color.b);
			
			color.r = fminf(fmaxf(color.r, 0.0f), 1.0f);
			color.g = fminf(fmaxf(color.g, 0.0f), 1.0f);
			color.b = fminf(fmaxf(color.b, 0.0f), 1.0f);
		}
	}
	
	return color;
}


static void DecoderPacket(void *cbInfo, OOALStringRef packetType, OOALDictionaryRef packet)
{
	OOConsoleConnectionRef			connection = cbInfo;
	
	if (connection == NULL)  return;
	
	LogPacket(CFSTR("IN"), packet);
	
#define PACKET_CASE(x) else if (CFEqual(packetType, kOOTCPPacket_##x))  { Handle##x##Packet(connection, packet); }
	if (0)  {}
	PACKET_CASE(RequestConnection)
	PACKET_CASE(CloseConnection)
	PACKET_CASE(ConsoleOutput)
	PACKET_CASE(ClearConsole)
	PACKET_CASE(ShowConsole)
	PACKET_CASE(NoteConfiguration)
	PACKET_CASE(NoteConfigurationChange)
	PACKET_CASE(Ping)
	PACKET_CASE(Pong)
	else
	{
		if (connection->noteUnknownPackets)  Error(connection, CFSTR("Unhandled packet type %@."), packetType);
	}
}


static void DecoderError(void *cbInfo, OOALStringRef errorDesc)
{
	OOConsoleConnectionRef			connection = cbInfo;
	
	if (connection != NULL && connection->cb.Error != NULL)
	{
		connection->cb.Error(connection->cbInfo, errorDesc);
	}
}


static void HandleRequestConnectionPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	CFNumberRef						protocolVersionCF = NULL;
	uint32_t						protocolVersion;
	
	if (connection->state == kStateWaitingForConnection)
	{
		// Get protocol verison.
		protocolVersionCF = CFDictionaryGetValue(packet, kOOTCPProtocolVersion);
		if (protocolVersionCF == NULL || CFGetTypeID(protocolVersionCF) != CFNumberGetTypeID())
		{
			RejectConnection(connection, CFSTR("Protocol error: invalid Request Connection packet (no protocol version)."));
		}
		else
		{
			CFNumberGetValue(protocolVersionCF, kCFNumberSInt32Type, &protocolVersion);
			connection->vFormat = OOTCP_VERSION_FORMAT(protocolVersion);
			connection->vMajor = OOTCP_VERSION_MAJOR(protocolVersion);
			connection->vMinor = OOTCP_VERSION_MINOR(protocolVersion);
			
			// We currently support protocol version 1:1.x
			if (connection->vFormat != kOOTCPProtocolVersionPListFormat || connection->vMajor != 1)
			{
				RejectConnection(connection, CFFMTSTR("Unsupported protocol version: %u:%u.%u.", connection->vFormat, connection->vMajor, connection->vMinor));
			}
			else
			{
				// Approve connection.
				connection->noteUnknownPackets = connection->vMinor <= 0;
				connection->state = kStateConnected;
				
				SendWithOneParameter(connection, kOOTCPPacket_ApproveConnection, kOOTCPConsoleIdentity, connection->consoleIdentityString);
				if (connection->consoleIdentityString != NULL)
				{
					CFRelease(connection->consoleIdentityString);
					connection->consoleIdentityString = NULL;
				}
				
				if (connection->cb.ConnectionEstablished != NULL)  connection->cb.ConnectionEstablished(connection->cbInfo, CFDictionaryGetValue(packet, kOOTCPOoliteVersion));
			}
		}
	}
	else
	{
		Error(connection, CFSTR("Protocol error: got Request Connection packet while not waiting for connection, ignoring."));
	}
}


static void HandleCloseConnectionPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	CFStringRef						message = NULL;
	
	if (!OOConsoleConnectionIsOpen(connection))  return;
	
	connection->state = kStateClosing;
	message = CFDictionaryGetValue(packet, kOOTCPMessage);
	if (connection->cb.Close != NULL)  connection->cb.Close(connection->cbInfo, TRUE, message);
	connection->state = kStateDisconnected;
	
	if (connection->configuration != NULL)
	{
		CFRelease(connection->configuration);
		connection->configuration = NULL;
	}
}


static void HandleConsoleOutputPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	CFStringRef						message = NULL;
	CFStringRef						colorKey = NULL;
	
	if (!OOConsoleConnectionIsOpen(connection))  return;
	
	message = CFDictionaryGetValue(packet, kOOTCPMessage);
	if (message == NULL)
	{
		Error(connection, CFSTR("Protocol error: Console Output packet with no message."));
		return;
	}
	
	colorKey = CFDictionaryGetValue(packet, kOOTCPColorKey);
	if (colorKey == NULL)  colorKey = CFSTR("general");
	
	// TODO: handle emphasis ranges.
	
	connection->cb.ConsoleOutput(connection->cbInfo, message, colorKey, NULL, 0);
}


static void HandleClearConsolePacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	if (!OOConsoleConnectionIsOpen(connection))  return;
	
	if (connection->cb.ClearConsole)  connection->cb.ClearConsole(connection->cbInfo);
}


static void HandleShowConsolePacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	if (!OOConsoleConnectionIsOpen(connection))  return;
	
	if (connection->cb.ShowConsole)  connection->cb.ShowConsole(connection->cbInfo);
}


static void HandleNoteConfigurationPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	if (connection->configuration != NULL)  CFDictionaryRemoveAllValues(connection->configuration);
	HandleNoteConfigurationChangePacket(connection, packet);
}


static void HandleNoteConfigurationChangePacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	CFDictionaryRef					set = NULL;
	CFArrayRef						remove = NULL;
	CFIndex							i, count;
	const void						**keys = NULL;
	const void						**values = NULL;
	CFTypeRef						key = NULL;
	CFMutableArrayRef				changed = NULL;
	
	if (connection->configuration == NULL)  connection->configuration = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
	if (connection->configuration == NULL)  return;
	
	changed = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
	if (changed == NULL)  return;
	
	// Set the keys in kOOTCPConfiguration, assuming it's a dictionary.
	set = CFDictionaryGetValue(packet, kOOTCPConfiguration);
	if (set != NULL && CFGetTypeID(set) == CFDictionaryGetTypeID())
	{
		count = CFDictionaryGetCount(set);
		if (count > 0)
		{
			// Get all the keys...
			keys = malloc(sizeof *keys * count);
			values = malloc(sizeof *values * count);
			if (keys != NULL && values != NULL)
			{
				CFDictionaryGetKeysAndValues(set, keys, values);
				
				// ..and set them in configuration.
				for (i = 0; i != count; ++i)
				{
					key = keys[i];
					if (CFGetTypeID(key) == CFArrayGetTypeID())
					{
						CFDictionarySetValue(connection->configuration, key, values[i]);
						CFArrayAppendValue(changed, key);
					}
				}
			}
			free(keys);
			free(values);
		}
	}
	
	remove = CFDictionaryGetValue(packet, kOOTCPRemovedConfigurationKeys);
	if (remove != NULL && CFGetTypeID(set) == CFArrayGetTypeID())
	{
		count = CFArrayGetCount(remove);
		for (i = 0; i != count; ++i)
		{
			key = CFArrayGetValueAtIndex(remove, i);
			if (CFGetTypeID(key) == CFArrayGetTypeID())
			{
				CFDictionaryRemoveValue(connection->configuration, key);
				CFArrayAppendValue(changed, key);
			}
		}
	}
	
	if (connection->cb.ConfigurationChanged != NULL && CFArrayGetCount(changed) != 0)
	{
		connection->cb.ConfigurationChanged(connection->cbInfo, changed);
	}
	CFRelease(changed);
}


static void HandlePingPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	CFStringRef						message = NULL;
	
	message = CFDictionaryGetValue(packet, kOOTCPMessage);
	SendWithOneParameter(connection, kOOTCPPacket_Pong, kOOTCPMessage, message);
}


static void HandlePongPacket(OOConsoleConnectionRef connection, CFDictionaryRef packet)
{
	// Ignore; we don't send pings so shouldn't get any pongs.
}


static void RejectConnection(OOConsoleConnectionRef connection, CFStringRef message)
{
	connection->state = kStateClosing;
	SendWithOneParameter(connection, kOOTCPPacket_RejectConnection, kOOTCPMessage, message);
	if (connection->cb.Close != NULL)  connection->cb.Close(connection->cbInfo, FALSE, message);
	connection->state = kStateDisconnected;
	
	if (connection->configuration != NULL)
	{
		CFRelease(connection->configuration);
		connection->configuration = NULL;
	}
}


#if LOG_PACKETS
#include "JAPrint.h"

static void LogPacket(CFStringRef label, CFDictionaryRef packet)
{
	static Boolean			triedToOpen = FALSE;
	static FILE				*file = NULL;
	CFDataRef				data = NULL;
	
	if (!triedToOpen)
	{
		triedToOpen = TRUE;
		file = fopen("traffic log.txt", "w");
	}
	if (file == NULL)  return;
	
	data = CFPropertyListCreateXMLData(kCFAllocatorDefault, packet);
	
	JAFPrint(file, CFSTR("* %@:\n"), label);
	fwrite(CFDataGetBytePtr(data), CFDataGetLength(data), 1, file);
	JAFPrint(file, CFSTR("\n\n"));
}
#endif