File: EDTCPSocket.m

package info (click to toggle)
gridlock.app 1.10-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,556 kB
  • sloc: objc: 10,334; ansic: 669; makefile: 12
file content (130 lines) | stat: -rw-r--r-- 5,593 bytes parent folder | download | duplicates (7)
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
//---------------------------------------------------------------------------------------
//  EDTCPSocket.m created by erik
//  @(#)$Id: EDTCPSocket.m,v 2.0 2002/08/16 18:12:48 erik Exp $
//
//  Copyright (c) 1997-2001 by Erik Doernenburg. All rights reserved.
//
//  Permission to use, copy, modify and distribute this software and its documentation
//  is hereby granted, provided that both the copyright notice and this permission
//  notice appear in all copies of the software, derivative works or modified versions,
//  and any portions thereof, and that both notices appear in supporting documentation,
//  and that credit is given to Erik Doernenburg in all documents and publicity
//  pertaining to direct or indirect use of this code or its derivatives.
//
//  THIS IS EXPERIMENTAL SOFTWARE AND IT IS KNOWN TO HAVE BUGS, SOME OF WHICH MAY HAVE
//  SERIOUS CONSEQUENCES. THE COPYRIGHT HOLDER ALLOWS FREE USE OF THIS SOFTWARE IN ITS
//  "AS IS" CONDITION. THE COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY
//  DAMAGES WHATSOEVER RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE
//  OR OF ANY DERIVATIVE WORK.
//---------------------------------------------------------------------------------------

#import <Foundation/Foundation.h>
#import "osdep.h"
#import "EDTCPSocket.h"


#ifdef WIN32
#define EDSOCKETHANDLE ((int)[self nativeHandle])
#else
#define EDSOCKETHANDLE [self fileDescriptor]
#endif


//---------------------------------------------------------------------------------------
    @implementation EDTCPSocket
//---------------------------------------------------------------------------------------

/*" This class implementes TCP sockets and some methods specific to such sockets. Most of the commonly used functionality is in its superclasses #EDIPSocket and #NSFileHandle as well as in the NSFileHandle category defined in this framework.

The following code example shows how to set up a client socket, and connect it to Apple's web server: !{

    EDTCPSocket *socket;

    socket = [EDTCPSocket socket];
    [socket connectToHost:[NSHost hostWithName:hostname] port:80]
}

The following code example shows how to set up a server socket, listening on the standard web port on all interfaces of this host. If we are debugging it sets the "re-use address" option so that we don't have to wait to set up the socket after restarting our application: !{

    EDTCPSocket *socket;

    socket = [EDTCPSocket socket];
\#if DEBUG
    [socket setAllowsAddressReuse:YES];
\#endif
    [socket startListeningOnLocalPort:80];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(newClientNotification:)
                                                 name:NSFileHandleConnectionAcceptedNotification
                                               object:socket];
    [socket acceptConnectionInBackgroundAndNotify];

}

Note that, generally, working with #EDStreams is a better choice than working directly with sockets.

"*/

//---------------------------------------------------------------------------------------
//	CLASS ATTRIBUTES
//---------------------------------------------------------------------------------------

+ (int)socketProtocol
{
    return IPPROTO_TCP;
}


+ (int)socketType
{
    return SOCK_STREAM;
}


//---------------------------------------------------------------------------------------
//	OPTIONS & PARAMETERS
//---------------------------------------------------------------------------------------

/*" Controls wether data written to the socket is sent immediatly or whether the operating system should try to accumulate some data before sending a packet to reduce overhead. This is commonly known as Nagle's algorithm (RFC 896) and corresponds to TCP_NODELAY. "*/

- (void)setSendsDataImmediately:(BOOL)flag
{
    [self setSocketOption:TCP_NODELAY level:IPPROTO_TCP value:flag];
}


//---------------------------------------------------------------------------------------
//	LISTENING
//---------------------------------------------------------------------------------------

/*" Starts listening for incoming connections. This requires that a local port has been set before. Note that #acceptConnectionInBackgroundAndNotify must be called afterwards so that incoming connections are handled. "*/

- (void)startListening
{
    if(listen(EDSOCKETHANDLE, 5) == -1)   // backlog length of 5 is maximum
        [NSException raise:NSFileHandleOperationException format:@"Unable start listening on local port: %s", strerror(ED_ERRNO)];
    flags.listening = YES;
}


/*" Starts listening for incoming connections on aPort on all local IP addresses (interfaces). Note that #acceptConnectionInBackgroundAndNotify must be called afterwards so that incoming connections are handled. "*/

- (void)startListeningOnLocalPort:(unsigned short)aPort
{	
    [self setLocalPort:aPort];
    [self startListening];
}


/*" Starts listening for incoming connections on the aPort on the specified local address; the latter being in the "typical" dotted numerical notation. Note that #acceptConnectionInBackgroundAndNotify must be called afterwards so that incoming connections are handled. "*/

- (void)startListeningOnLocalPort:(unsigned short)aPort andAddress:(NSString *)addressString
{
    [self setLocalPort:aPort andAddress:addressString];
    [self startListening];
}


//---------------------------------------------------------------------------------------
    @end
//---------------------------------------------------------------------------------------