-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing1.html
executable file
·1109 lines (906 loc) · 39.7 KB
/
listing1.html
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>CFLocalServer - /Client.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/CoreFoundation/index.html">Core Foundation</a> > <a href="../../samplecode/CoreFoundation/idxNetworking-date.html">Networking</a> > <A HREF="javascript:location.replace('index.html');">CFLocalServer</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">CFLocalServer</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/Client.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/Client.c</option>
<option value="listing2.html">/Common.c</option>
<option value="listing3.html">/Common.h</option>
<option value="listing4.html">/Protocol.h</option>
<option value="listing5.html">/Read Me About CFLocalServer.txt</option>
<option value="listing6.html">/Server.c</option></select>
</p>
</form>
<p><strong><a href="CFLocalServer.zip">Download Sample</a></strong> (“CFLocalServer.zip”, 122.5K)<BR>
<strong><a href="CFLocalServer.dmg">Download Sample</a></strong> (“CFLocalServer.dmg”, 190.2K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: Client.c
Contains: Client showing integration of CFSockets and UNIX domain sockets.
Written by: DTS
Copyright: Copyright (c) 2005 by Apple Computer, Inc., All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Change History (most recent first):
$Log: Client.c,v $
Revision 1.2 2005/05/18 13:36:28
Fixed various documentation/comment changes.
Revision 1.1 2005/05/17 12:19:13
First checked in.
*/
/////////////////////////////////////////////////////////////////
// System interfaces
#include <CoreServices/CoreServices.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
// Project interfaces
#include "Protocol.h"
#include "Common.h"
/////////////////////////////////////////////////////////////////
#pragma mark ***** Connection Abstraction
// A ConnectionRef represents a connection from the client to the server.
// The internals of this are opaque to external callers. All operations on
// a connection are done via the routines in this section.
enum {
kConnectionStateMagic = 'LCCM' // Local Client Connection Magic
};
typedef struct ConnectionState * ConnectionRef;
// Pseudo-opaque reference to the connection.
typedef Boolean (*ConnectionCallbackProcPtr)(
ConnectionRef conn,
const PacketHeader * packet,
void * refCon
);
// When the client enables listening on a connection, it supplies a
// function of this type as a callback. We call this function in
// the context of the runloop specified by the client when they enable
// listening.
//
// conn is a reference to the connection. It will not be NULL.
//
// packet is a pointer to the packet that arrived, or NULL if we've
// detected that the connection to the server is broken.
//
// refCon is a value that the client specified when it registered this
// callback.
//
// If the server sends you a bad packet, you can return false to
// tell the connection management system to shut down the connection.
// ConnectionState is the structure used to track a single connection to
// the server. All fields after fSockFD are only relevant if the client
// has enabled listening.
struct ConnectionState {
OSType fMagic; // kConnectionStateMagic
int fSockFD; // UNIX domain socket to server
CFSocketRef fSockCF; // CFSocket wrapper for the above
CFRunLoopSourceRef fRunLoopSource; // runloop source for the above
CFMutableDataRef fBufferedPackets; // buffer for incomplete packet data
ConnectionCallbackProcPtr fCallback; // client's packet callback
void * fCallbackRefCon; // refCon for the above.
};
// Forward declarations. See the comments associated with the function definition.
static void ConnectionShutdown(ConnectionRef conn);
static void ConnectionCloseInternal(ConnectionRef conn, Boolean sayGoodbye);
static int ConnectionOpen(ConnectionRef *connPtr)
// Opens a connection to the server.
//
// On entry, connPtr must not be NULL
// On entry, *connPtr must be NULL
// Returns an errno-style error code
// On success, *connPtr will not be NULL
// On error, *connPtr will be NULL
{
int err;
ConnectionRef conn;
Boolean sayGoodbye;
assert( connPtr != NULL);
assert(*connPtr == NULL);
sayGoodbye = false;
// Allocate a ConnectionState structure and fill out some basic fields.
err = 0;
conn = (ConnectionRef) calloc(1, sizeof(*conn));
if (conn == NULL) {
err = ENOMEM;
}
if (err == 0) {
conn->fMagic = kConnectionStateMagic;
// For clean up to work properly, we must make sure that, if
// the connection record is allocated successfully, we always
// set fSockFD to -1. So, while the following line is redundant
// in the current code, it's present to press home this point.
conn->fSockFD = -1;
}
// Create a UNIX domain socket and connect to the server.
if (err == 0) {
conn->fSockFD = socket(AF_UNIX, SOCK_STREAM, 0);
err = MoreUNIXErrno(conn->fSockFD);
}
if (err == 0) {
struct sockaddr_un connReq;
connReq.sun_len = sizeof(connReq);
connReq.sun_family = AF_UNIX;
strcpy(connReq.sun_path, kServerSocketPath);
err = connect(conn->fSockFD, (struct sockaddr *) &connReq, SUN_LEN(&connReq));
err = MoreUNIXErrno(err);
sayGoodbye = (err == 0);
}
// Clean up.
if (err != 0) {
ConnectionCloseInternal(conn, sayGoodbye);
conn = NULL;
}
*connPtr = conn;
assert( (err == 0) == (*connPtr != NULL) );
return err;
}
static int ConnectionSend(ConnectionRef conn, const PacketHeader *packet)
// Send a packet to the server. Use this when you're not expecting a
// reply.
//
// conn must be a valid connection
// packet must be a valid, ready-to-send, packet
// Returns an errno-style error code
{
int err;
assert(conn != NULL);
assert(conn->fSockFD != -1); // connection must not be shut down
// conn->fSockCF may or may not be NULL; it's OK to send a packet when listening
// because there's no reply; OTOH, you can't do an RPC while listening because
// an unsolicited packet might get mixed up with the RPC reply.
assert(packet != NULL);
assert(packet->fMagic == kPacketMagic);
assert(packet->fSize >= sizeof(PacketHeader));
// Simply send the packet down the socket.
err = MoreUNIXWrite(conn->fSockFD, packet, packet->fSize, NULL);
return err;
}
static int ConnectionRPC(
ConnectionRef conn,
const PacketHeader * request,
PacketHeader * reply,
size_t replySize
)
// Perform an RPC (Remote Procedure Call) with the server. That is, send
// the server a packet and wait for a reply. You can only use this on
// connections that are not in listening mode.
//
// conn must be a valid connection
//
// packet must be a valid, ready-to-send, packet
//
// reply and replySize specify a buffer where the reply packet is placed;
// reply size must not be NULL; replySize must not be less that the
// packet header size (sizeof(PacketHeader)); if the reply packet is bigger
// than replySize, the data that won't fit is discarded; you can detect this
// by looking at reply->fSize
//
// Returns an errno-style error code
// On success, the buffer specified by reply and replySize will contain the
// reply packet; on error, the contents of that buffer is invalid; also,
// if this routine errors the connection is no longer useful (conn is still
// valid, but you can't use it to transmit any more data)
{
int err;
assert(conn != NULL);
assert(conn->fSockFD != -1); // connection must not be shut down
assert(conn->fSockCF == NULL); // RPC and listening are mutually exclusive
// because unsolicited packet might get mixed up
// with the reply
assert(request != NULL);
assert(request->fMagic == kPacketMagic);
assert(request->fSize >= sizeof(PacketHeader));
assert(reply != NULL);
assert(replySize >= sizeof(PacketHeader));
// Send the request.
err = ConnectionSend(conn, request);
// Read and validate the reply header.
if (err == 0) {
err = MoreUNIXRead(conn->fSockFD, reply, sizeof(PacketHeader), NULL);
}
if ( (err == 0) && (reply->fMagic != kPacketMagic) ) {
fprintf(stderr, "ConnectionRPC: Bad magic (%.4s).\n", (char *) &reply->fMagic);
err = EINVAL;
}
if ( (err == 0) && (reply->fType != kPacketTypeReply) ) {
fprintf(stderr, "ConnectionRPC: Type wrong (%.4s).\n", (char *) &reply->fType);
err = EINVAL;
}
if ( (err == 0) && (reply->fID != request->fID) ) {
fprintf(stderr, "ConnectionRPC: ID mismatch (%" PRId32 ").\n", reply->fID);
err = EINVAL;
}
if ( (err == 0) && ( (reply->fSize < sizeof(PacketHeader)) || (reply->fSize > kPacketMaximumSize) ) ) {
fprintf(stderr, "ConnectionRPC: Bogus packet size (%" PRIu32 ").\n", reply->fSize);
err = EINVAL;
}
// Read the packet payload that will fit in the reply buffer.
if ( (err == 0) && (reply->fSize > sizeof(PacketHeader)) ) {
uint32_t payloadToRead;
if (reply->fSize > replySize) {
payloadToRead = replySize;
} else {
payloadToRead = reply->fSize;
}
payloadToRead -= sizeof(PacketHeader);
err = MoreUNIXRead(conn->fSockFD, ((char *) reply) + sizeof(PacketHeader), payloadToRead, NULL);
}
// Discard any remaining packet payload that will fit in the reply buffer.
// The addition check in the next line is necessary to avoid the undefined behaviour
// of malloc(0) in the dependent block.
if ( (err == 0) && (reply->fSize > replySize) ) {
uint32_t payloadToJunk;
void * junkBuf;
payloadToJunk = reply->fSize - replySize;
junkBuf = malloc(payloadToJunk);
if (junkBuf == NULL) {
err = ENOMEM;
}
if (err == 0) {
err = MoreUNIXRead(conn->fSockFD, junkBuf, payloadToJunk, NULL);
}
free(junkBuf);
}
// Any errors cause us to immediately shut down our connection because we
// we're no longer sure of the state of the channel (that is, did we leave
// half a packet stuck in the pipe).
if (err != 0) {
ConnectionShutdown(conn);
}
return err;
}
static void ConnectionGotData(
CFSocketRef s,
CFSocketCallBackType type,
CFDataRef address,
const void * data,
void * info
)
// This is a a CFSocket callback indicating that data has arrived on the
// socket. It's only called if the user has registered the associated
// connection for listening. The parameter are as per the CFSocket
// documentation. As this is a callback of type kCFSocketDataCallBack,
// data contains newly arrived data that CFSocket has already read for us.
{
#pragma unused(address)
CFDataRef newData;
ConnectionRef conn;
assert(s != NULL);
assert(type == kCFSocketDataCallBack);
// Cast data to a CFDataRef, newData.
newData = (CFDataRef) data;
assert(newData != NULL);
assert( CFGetTypeID(newData) == CFDataGetTypeID() );
// Cast info to a ConnectionRef.
conn = (ConnectionRef) info;
assert(conn->fMagic == kConnectionStateMagic);
if ( CFDataGetLength(newData) == 0 ) {
// End of data stream; the server is dead.
fprintf(stderr, "ConnectionGotData: Server died unexpectedly.\n");
// Tell the client.
(void) conn->fCallback(conn, NULL, conn->fCallbackRefCon);
// Shut 'er down Clancy, she's pumping mud!
ConnectionShutdown(conn);
} else {
// We have new data from the server. Appending to our buffer.
CFDataAppendBytes(conn->fBufferedPackets, CFDataGetBytePtr(newData), CFDataGetLength(newData));
// Now see if there are any complete packets in the buffer; and,
// if so, deliver them to the client.
do {
PacketHeader * thisPacket;
Boolean success;
if ( CFDataGetLength(conn->fBufferedPackets) < sizeof(PacketHeader) ) {
// Not enough data for the packet header; we're done.
break;
}
thisPacket = (PacketHeader *) CFDataGetBytePtr(conn->fBufferedPackets);
if ( thisPacket->fMagic != kPacketMagic ) {
fprintf(stderr, "ConnectionGotData: Server sent us a packet with bad magic (%.4s).\n", (char *) &thisPacket->fMagic);
ConnectionShutdown(conn);
break;
}
if (thisPacket->fSize > kPacketMaximumSize) {
fprintf(stderr, "ConnectionGotData: Server sent us a packet that's just too big (%" PRIu32 ").\n", thisPacket->fSize);
ConnectionShutdown(conn);
break;
}
if ( CFDataGetLength(conn->fBufferedPackets) < thisPacket->fSize ) {
// Not enough data for the packet body; we're done.
break;
}
// Tell the client about the packet.
success = conn->fCallback(conn, thisPacket, conn->fCallbackRefCon);
if ( ! success ) {
ConnectionShutdown(conn);
break;
}
// Delete this packet from the front of our packet buffer. I horror at
// the inefficiency of this, but it is sample code after all.
CFDataDeleteBytes(conn->fBufferedPackets, CFRangeMake(0, thisPacket->fSize));
} while (true);
}
}
static int ConnectionRegisterListener(
ConnectionRef conn,
CFRunLoopRef runLoop,
CFStringRef runLoopMode,
ConnectionCallbackProcPtr callback,
void * refCon
)
// Register a listener to be called when packets arrive. Once you've done
// this, you can no longer use conn for RPCs.
//
// conn must be a valid connection
//
// runLoop and runLoopMode specify the context in which the callback will
// be called; in most cases you specify CFRunLoopGetCurrent() and
// kCFRunLoopDefaultMode
//
// callback is the function you want to be called when packets arrive; it
// must not be NULL
//
// refCon is passed to callback
//
// Returns an errno-style error code
// On success, the connection has been converted to a listener and your
// callback will be called from the context of the specific runloop when
// a packet arrives; on error, the connection is no longer useful (conn is
// still valid, but you can't use it to transmit any more data)
{
int err;
assert(conn != NULL);
assert(runLoop != NULL);
assert(runLoopMode != NULL);
assert(callback != NULL);
assert(conn->fSockFD != -1); // connection must not be shut down
assert(conn->fSockCF == NULL); // can't register twice
// Create the packet buffer.
err = 0;
conn->fBufferedPackets = CFDataCreateMutable(NULL, 0);
if (conn->fBufferedPackets == NULL) {
err = ENOMEM;
}
// Add the source to the runloop.
if (err == 0) {
CFSocketContext context;
memset(&context, 0, sizeof(context));
context.info = conn;
conn->fSockCF = CFSocketCreateWithNative(
NULL,
(CFSocketNativeHandle) conn->fSockFD,
kCFSocketDataCallBack,
ConnectionGotData,
&context
);
if (conn->fSockCF == NULL) {
err = EINVAL;
}
}
if (err == 0) {
conn->fRunLoopSource = CFSocketCreateRunLoopSource(NULL, conn->fSockCF, 0);
if (conn->fRunLoopSource == NULL) {
err = EINVAL;
}
}
if (err == 0) {
conn->fCallback = callback;
conn->fCallbackRefCon = refCon;
CFRunLoopAddSource( runLoop, conn->fRunLoopSource, runLoopMode);
}
// Any failure means the entire connection is dead; again, this is the
// draconian approach to error handling. But hey, connections are
// (relatively) cheap.
if (err != 0) {
ConnectionShutdown(conn);
}
return err;
}
static void ConnectionShutdown(ConnectionRef conn)
// This routine shuts down down the connection to the server
// without saying goodbye; it leaves conn valid. This routine
// is primarily used internally to the connection abstraction
// where we notice that the connection has failed for some reason.
// It's also called by the client after a successful quit RPC
// because we know that the server has closed its end of the
// connection.
//
// It's important to nil out the fields as we close them because
// this routine is called if any messaging routine fails. If it
// doesn't nil out the fields, two bad things might happen:
//
// o When the connection is eventually closed, ConnectionCloseInternal
// will try to send a Goodbye, which fails triggering an assert.
//
// o If ConnectionShutdown is called twice on a particular connection
// (which happens a lot; this is a belts and braces implementation
// [that's "belts and suspenders" for the Americans reading this;
// ever wonder why Monty Python's lumberjacks sing about "suspenders
// and a bra"?; well look up "suspenders" in a non-American dictionary
// for a quiet chuckle :-] )
{
int junk;
Boolean hadSockCF;
assert(conn != NULL);
conn->fCallback = NULL;
conn->fCallbackRefCon = NULL;
if (conn->fRunLoopSource != NULL) {
CFRunLoopSourceInvalidate(conn->fRunLoopSource);
CFRelease(conn->fRunLoopSource);
conn->fRunLoopSource = NULL;
}
// CFSocket will close conn->fSockFD when we invalidate conn->fSockCF,
// so we remember whether we did this so that, later on, we know
// whether to close the file descriptor ourselves. We need an extra
// variable because we NULL out fSockCF as we release it, for the reason
// described above.
hadSockCF = (conn->fSockCF != NULL);
if (conn->fSockCF != NULL) {
CFSocketInvalidate(conn->fSockCF);
CFRelease(conn->fSockCF);
conn->fSockCF = NULL;
}
if (conn->fBufferedPackets != NULL) {
CFRelease(conn->fBufferedPackets);
conn->fBufferedPackets = NULL;
}
if ( (conn->fSockFD != -1) && ! hadSockCF ) {
junk = close(conn->fSockFD);
assert(junk == 0);
}
// We always set fSockFD to -1 because either we've closed it or
// CFSocket has.
conn->fSockFD = -1;
}
static void ConnectionCloseInternal(ConnectionRef conn, Boolean sayGoodbye)
// The core of ConnectionClose. It's called by ConnectionClose
// and by ConnectionOpen, if it fails for some reason. This exists
// as a separate routine so that we can add the sayGoodbye parameter,
// which controls whether we send a goodbye packet to the server. We
// need this because we should always try to say goodbye if we're called
// from ConnectionClose, but if we're called from ConnectionOpen we
// should only try to say goodbye if we successfully connected the
// socket.
//
// Regardless, the bulk of the work of this routine is done by
// ConnectionShutdown. This routine exists to a) say goodbye, if
// necessary, and b) free the memory associated with the connection.
{
int junk;
if (conn != NULL) {
assert(conn->fMagic == kConnectionStateMagic);
if ( (conn->fSockFD != -1) && sayGoodbye ) {
PacketGoodbye goodbye;
InitPacketHeader(&goodbye.fHeader, kPacketTypeGoodbye, sizeof(goodbye), false);
snprintf(goodbye.fMessage, sizeof(goodbye.fMessage), "Process %ld signing off", (long) getpid());
junk = ConnectionSend(conn, &goodbye.fHeader);
assert(junk == 0);
}
ConnectionShutdown(conn);
free(conn);
}
}
static void ConnectionClose(ConnectionRef conn)
// Closes the connection. It's legal to pass conn as NULL, in which
// case this does nothing (kinda like free'ing NULL).
{
ConnectionCloseInternal(conn, true);
}
/////////////////////////////////////////////////////////////////
#pragma mark ***** Command Line Tool
// The following routines use the connection abstraction defined above
// to implement a simple command line tool that exercises the server.
enum {
kResultColumnWidth = 10
};
static void PrintResult(const char *command, int errNum, const char *arg)
// Prints the result of a command. command is the name of the
// command, errNum is the errno-style error number, and arg
// (if not NULL) is the command argument.
{
if (errNum == 0) {
if (arg == NULL) {
fprintf(stderr, "%*s\n", kResultColumnWidth, command);
} else {
fprintf(stderr, "%*s \"%s\"\n", kResultColumnWidth, command, arg);
}
} else {
fprintf(stderr, "%*s failed with error %d\n", kResultColumnWidth, command, errNum);
}
}
static void DoNOP(ConnectionRef conn)
// Implements the "nop" command by doing a NOP RPC with the server.
{
int err;
PacketNOP request;
PacketReply reply;
InitPacketHeader(&request.fHeader, kPacketTypeNOP, sizeof(request), true);
err = ConnectionRPC(conn, &request.fHeader, &reply.fHeader, sizeof(reply));
if (err == 0) {
err = reply.fErr;
}
PrintResult("nop", err, NULL);
}
static void DoWhisper(ConnectionRef conn, const char *message)
// Implements the "whisper" command by doing a whisper RPC with the server.
//
// The server responds to this RPC by printing the message.
{
int err;
PacketWhisper request;
PacketReply reply;
InitPacketHeader(&request.fHeader, kPacketTypeWhisper, sizeof(request), true);
snprintf(request.fMessage, sizeof(request.fMessage), "%s", message);
err = ConnectionRPC(conn, &request.fHeader, &reply.fHeader, sizeof(reply));
if (err == 0) {
err = reply.fErr;
}
PrintResult("whisper", err, message);
}
static void DoShout(ConnectionRef conn, const char *message)
// Implements the "shout" command by sending a shout packet to the server.
// Note that this is /not/ an RPC.
//
// The server responds to this packet by echoing it to each registered
// listener.
{
int err;
PacketShout request;
InitPacketHeader(&request.fHeader, kPacketTypeShout, sizeof(request), false);
snprintf(request.fMessage, sizeof(request.fMessage), "%s", message);
err = ConnectionSend(conn, &request.fHeader);
PrintResult("shout", err, message);
}
static Boolean GotPacket(ConnectionRef conn, const PacketHeader *packet, void *refCon)
// DoListen registers this routine with the connection abstraction layer
// so that it is called when a packet arrives. For a description of the
// parameters, see the comments next to ConnectionCallbackProcPtr.
{
#pragma unused(conn)
Boolean result;
CFRunLoopRef runLoop;
// When we register this callback, we pass a reference to the runloop
// as the refCon. Extract that reference here.
runLoop = (CFRunLoopRef) refCon;
assert(runLoop != NULL);
assert( CFGetTypeID(runLoop) == CFRunLoopGetTypeID() );
result = true;
if (packet == NULL) {
// Server connection has gone away. No need to return false because
// the connection is torn anyway.
CFRunLoopStop(runLoop);
} else {
// We got a packet from the server. Tell the user about it.
switch (packet->fType) {
case kPacketTypeShout:
if (packet->fSize != sizeof(PacketShout)) {
fprintf(stderr, "GotPacket: Server sent us a Shout with the wrong size (%" PRIu32 ").\n", packet->fSize);
result = false;
}
if (result && (packet->fID != kPacketIDNone) ) {
fprintf(stderr, "GotPacket: Server sent us a Shout with the wrong size (%" PRId32 ").\n", packet->fID);
result = false;
}
if (result) {
PacketShout * shoutPacket;
shoutPacket = (PacketShout *) packet;
fprintf(stderr, "%*s heard \"%.*s\"\n", kResultColumnWidth, "", (int) sizeof(shoutPacket->fMessage), shoutPacket->fMessage);
}
break;
default:
fprintf(stderr, "GotPacket: Server sent us a packet with an unexpected type (%.4s).\n", (char *) &packet->fType);
result = false;
break;
}
}
return result;
}
static void DoListen(ConnectionRef conn)
// Implements the "listen" command. First this does a listen RPC
// to tell the server that this connection is now a listener. Next it
// calls ConnectionRegisterListener to register a packet listener callback
// (GotPacket, above) on the runloop. It then runs the runloop until its
// stopped (by a SIGINT, via SIGINTRunLoopCallback, below).
{
int err;
PacketListen request;
PacketReply reply;
InitPacketHeader(&request.fHeader, kPacketTypeListen, sizeof(request), true);
err = ConnectionRPC(conn, &request.fHeader, &reply.fHeader, sizeof(reply));
if (err == 0) {
err = reply.fErr;
}
if (err == 0) {
err = ConnectionRegisterListener(
conn,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode,
GotPacket,
CFRunLoopGetCurrent()
);
}
if (err != 0) {
PrintResult("listen", err, NULL);
} else {
fprintf(stderr, "%*s Press ^C to quit.\n", kResultColumnWidth, "listen");
CFRunLoopRun();
}
}
static void DoQuit(ConnectionRef conn)
// Implements the "quit" command by doing a quit RPC with the server.
// The server responds to this RPC by quitting. Cleverly, it sends us
// the RPC reply right before quitting.
{
int err;
PacketQuit request;
PacketReply reply;
InitPacketHeader(&request.fHeader, kPacketTypeQuit, sizeof(request), true);
err = ConnectionRPC(conn, &request.fHeader, &reply.fHeader, sizeof(reply));
if (err == 0) {
err = reply.fErr;
}
if (err == 0) {
// If the quit is successful, we shut down our end of the connection
// because we know that the server has shut down its end.
ConnectionShutdown(conn);
}
PrintResult("quit", err, NULL);
}
static void SIGINTRunLoopCallback(const siginfo_t *sigInfo, void *refCon)
// This routine is called in response to a SIGINT signal.
// It is not, however, a signal handler. Rather, we
// orchestrate to have it called from the runloop (via
// the magic of InstallSignalToSocket). It's purpose
// is to stop the runloop when the user types ^C.
{
#pragma unused(sigInfo)
#pragma unused(refCon)
// Stop the runloop. Note that we can get a reference to the runloop by
// calling CFRunLoopGetCurrent because this is called from the runloop.
CFRunLoopStop( CFRunLoopGetCurrent() );
// Print a bonus newline to ensure that the next command prompt isn't
// printed on the same line as the echoed ^C.
fprintf(stderr, "\n");
}
static void PrintUsage(const char *argv0)
// Print the program's usage.
{
const char *command;
command = strrchr(argv0, '/');
if (command == NULL) {
command = argv0;
} else {
command += 1;
}
fprintf(stderr, "usage: %s command...\n", command);
fprintf(stderr, " commands: nop\n");
fprintf(stderr, " whisper <message>\n");
fprintf(stderr, " shout <message>\n");
fprintf(stderr, " listen (must be last)\n");
fprintf(stderr, " quit\n");
}
int main (int argc, const char * argv[])
// The primary entry point.
{
int err;
ConnectionRef conn;
conn = NULL;
// If we get no arguments, just print the usage and fail.
err = 0;
if (argc == 1) {
PrintUsage(argv[0]);
err = ECANCELED;
}
// SIGPIPE is evil, so tell the system not to send it to us.
if (err == 0) {
err = MoreUNIXIgnoreSIGPIPE();
}
// Organise to have SIGINT delivered to a runloop callback.
if (err == 0) {
sigset_t justSIGINT;
(void) sigemptyset(&justSIGINT);
(void) sigaddset(&justSIGINT, SIGINT);
err = InstallSignalToSocket(
&justSIGINT,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode,
SIGINTRunLoopCallback,
NULL
);
}