Sockets don't quite work, but they will almost work.
[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 /*!
21  * @enum ITInetSocketState
22  * @abstract Possible states of a socket
23  * @constant ITInetSocketConnecting The socket is negotiating a connection.
24  * @constant ITInetSocketListening The socket is a server socket.
25  * @constant ITInetSocketReading The socket is reading data from the other side.
26  * @constant ITInetSocketWriting The socket is sending data to the other side.
27  * @constant ITInetSocketDisconnected The socket does not have a connection.
28  */
29 typedef enum {
30     ITInetSocketConnecting,
31     ITInetSocketListening,
32     ITInetSocketReading,
33     ITInetSocketWriting,
34     ITInetSocketDisconnected
35 } ITInetSocketState;
36
37 /*!
38  * @enum ITInetSocketError
39  * @abstract Possible error conditions of a socket
40  * @constant ITInetHostNotFound The host specified does not actually exist.
41  * @constant ITInetConnectionDropped The remote side dropped the connection.
42  * @constant ITInetCouldNotConnect The socket was unable to connect for some reason
43  */
44 typedef enum {
45     ITInetHostNotFound,
46     ITInetConnectionDropped,
47     ITInetCouldNotConnect
48 } ITInetSocketError;
49
50 @class ITInetSocket;
51
52 /*!
53  * @protocol ITInetSocketDelegate
54  * @abstract Delegate methods for ITInetSocket
55  * @discussion ITInetSockets use these methods to communicate with their delegates
56  */
57 @protocol ITInetSocketDelegate <ITByteStreamDelegate>
58 /*!
59  * @method dataReceived:
60  * @abstract Alerts the delegate of data.
61  * @discussion The delegate should check [sender readPipe] to get the data.
62  * @param sender The socket that the messages came from.
63  */
64 - (oneway void) dataReceived:(ITInetSocket *)sender;
65 /*!
66  * @method errorOccured:during:onSocket:
67  * @abstract Alerts the delegate of an error condition.
68  * @discussion The delegate can try retryCondition.
69  * @param err The error class.
70  * @param state What the socket was doing when the error occured.
71  * @param sender The socket the error occured on.
72  */
73 - (oneway void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(ITInetSocket*)sender;
74 /*!
75  * @method finishedConnecting:
76  * @abstract Alerts the delegate of a successful connection attempt.
77  * @discussion The delegate should send whatever initial data is required for the protocol (nickname for IRC, etc.)
78  * @param sender The socket that established the connection.
79  */
80 - (oneway void) finishedConnecting:(ITInetSocket *)sender;
81 @end
82
83 /*!
84  * @class ITInetSocket
85  * @abstract An Internet socket class.
86  * @discussion ITInetSocket is an Internet socket class supporting IPv6 and Rendezvous.
87  */
88 @interface ITInetSocket : NSObject <ITByteStreamDelegate> {
89
90     int sockfd;
91     int port;
92     int nc;
93     unsigned short bufs;
94     volatile int dieflag;
95     volatile int actionflag;
96     id <ITInetSocketDelegate,NSObject> delegate;
97     struct addrinfo *ai, *ai_cur;
98     ITByteStream *readPipe, *writePipe;
99     ITInetSocketState state;
100     NSArray *sarr;
101 }
102 /*!
103  * @method startAutoconnectingToService:delegate:
104  * @abstract Automatically creates sockets whenever a certain type of Rendezvous service appears.
105  * @discussion The auto-created sockets will send finishedConnecting: to the delegate.
106  * @param type The type of Rendezvous service to listen on.
107  * @param d The delegate that the sockets will belong to.
108  */
109 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d;
110 /*!
111  * @method initWithFD:delegate:
112  * @abstract Wraps a socket around an existing socket descriptor.
113  * @discussion The socket will start listening on the descriptor as normal.
114  * @param fd The descriptor.
115  * @param d The delegate for the socket.
116  */
117 -(id) initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d;
118 /*!
119  * @method initWithDelegate:
120  * @abstract Creates a new socket.
121  * @discussion The socket will not be connected to anything.
122  * @param d The delegate of the socket.
123  */
124 -(id) initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d;
125
126 -(id <ITInetSocketDelegate>)delegate;
127 -(unsigned short)bufferSize;
128 -(void)setBufferSize:(unsigned short)bufs;
129 -(void) connectToHost:(NSString*)host onPort:(short)port;
130 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)port;
131 -(void) connectWithSockaddrArray:(NSArray*)arr;
132 -(ITInetSocketState) state;
133 -(void) retryConnection;
134 -(void) disconnect;
135 @end