LOTS more socket work. It can connect. It looks like it can read/write, but I'm not...
[ITFoundation.git] / ITInetSocket.h
1 //
2 //  ITInetSocket.h
3 //  ITFoundation
4 //
5 //  Created by Alexander Strange on Tue Feb 11 2003.
6 //  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10 #import <netinet/in.h>
11 #import <netdb.h>
12 #import "ITByteStream.h"
13
14 /*!
15  * @header ITInetSocket
16  * @abstract Definitions for the ITInetSocket class
17  */
18
19 /*!
20  * @constant ITInetMaxConnections
21  * @abstract The maximum number of running ITInetSockets you can have.
22  * @discussion The socket will error during a connection request if you are over the maximum.
23  */
24
25 enum {
26     ITInetMaxConnections = 128
27 };
28
29 /*!
30  * @enum ITInetSocketState
31  * @abstract Possible states of a socket
32  * @constant ITInetSocketConnecting The socket is negotiating a connection.
33  * @constant ITInetSocketListening The socket is a server socket.
34  * @constant ITInetSocketReading The socket is reading data from the other side.
35  * @constant ITInetSocketWriting The socket is sending data to the other side.
36  * @constant ITInetSocketDisconnected The socket does not have a connection.
37  */
38 typedef enum {
39     ITInetSocketConnecting,
40     ITInetSocketListening,
41     ITInetSocketReading,
42     ITInetSocketWriting,
43     ITInetSocketDisconnected
44 } ITInetSocketState;
45
46 /*!
47  * @enum ITInetSocketError
48  * @abstract Possible error conditions of a socket
49  * @constant ITInetHostNotFound The host specified does not actually exist.
50  * @constant ITInetConnectionDropped The remote side dropped the connection.
51  * @constant ITInetCouldNotConnect The socket was unable to connect for some reason
52  */
53 typedef enum {
54     ITInetHostNotFound,
55     ITInetConnectionDropped,
56     ITInetCouldNotConnect
57 } ITInetSocketError;
58
59 @class ITInetSocket;
60
61 /*!
62  * @protocol ITInetSocketDelegate
63  * @abstract Delegate methods for ITInetSocket
64  * @discussion ITInetSockets use these methods to communicate with their delegates
65  */
66 @protocol ITInetSocketDelegate
67 /*!
68  * @method dataReceived:
69  * @abstract Alerts the delegate of data.
70  * @discussion The delegate should check [sender readPipe] to get the data.
71  * @param sender The socket that the messages came from.
72  */
73 - (void) dataReceived:(in ITInetSocket *)sender;
74 /*!
75  * @method errorOccured:during:onSocket:
76  * @abstract Alerts the delegate of an error condition.
77  * @discussion The delegate can try retryCondition.
78  * @param err The error class.
79  * @param state What the socket was doing when the error occured.
80  * @param sender The socket the error occured on.
81  */
82 - (void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(in ITInetSocket*)sender;
83 /*!
84  * @method finishedConnecting:
85  * @abstract Alerts the delegate of a successful connection attempt.
86  * @discussion The delegate should send whatever initial data is required for the protocol (nickname for IRC, etc.)
87  * @param sender The socket that established the connection.
88  */
89 - (void) finishedConnecting:(in ITInetSocket *)sender;
90 @end
91
92 /*!
93  * @class ITInetSocket
94  * @abstract An Internet socket class.
95  * @discussion ITInetSocket is an Internet socket class supporting IPv6 and Rendezvous.
96  */
97 @interface ITInetSocket : NSObject {
98     @public
99     /*!
100      * @var sockfd
101         * @abstract KLWONZ
102      */
103     int sockfd;
104     int port;
105     unsigned short bufs;
106     volatile int dieflag;
107     volatile int actionflag;
108     id <ITInetSocketDelegate,NSObject> delegate;
109     struct addrinfo *ai, *ai_cur;
110     ITByteStream *readPipe, *writePipe;
111     ITInetSocketState state;
112     NSArray *sarr;
113 }
114 /*!
115  * @method startAutoconnectingToService:delegate:
116  * @abstract Automatically creates sockets whenever a certain type of Rendezvous service appears.
117  * @discussion The auto-created sockets will send finishedConnecting: to the delegate.
118  * @param type The type of Rendezvous service to listen on.
119  * @param d The delegate that the sockets will belong to.
120  */
121 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d;
122 /*!
123  * @method initWithFD:delegate:
124  * @abstract Wraps a socket around an existing socket descriptor.
125  * @discussion The socket will start listening on the descriptor as normal.
126  * @param fd The descriptor.
127  * @param d The delegate for the socket.
128  */
129 -(id) initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d;
130 /*!
131  * @method initWithDelegate:
132  * @abstract Creates a new socket.
133  * @discussion The socket will not be connected to anything.
134  * @param d The delegate of the socket.
135  */
136 -(id) initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d;
137
138 -(id <ITInetSocketDelegate>)delegate;
139 -(unsigned short)bufferSize;
140 -(void)setBufferSize:(unsigned short)bufs;
141 -(void) connectToHost:(NSString*)host onPort:(short)port;
142 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)port;
143 -(void) connectWithSockaddrArray:(NSArray*)arr;
144 -(ITInetSocketState) state;
145 -(void) retryConnection;
146 -(void) disconnect;
147 @end