BY THE POWER OF HEADERDOC!
[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 dataRecieved:
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) dataRecieved:(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:(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:(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     int sockfd;
100     int port;
101     id delegate;
102     struct addrinfo *ai, *ai_cur;
103     ITByteStream *readPipe, *writePipe;
104     ITInetSocketState state;
105     NSArray *sarr;
106 }
107 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate>)d;
108 -(id) initWithFD:(int)fd delegate:(id <ITInetSocketDelegate>)d;
109 -(id) initWithDelegate:(id <ITInetSocketDelegate>)d;
110
111 -(id <ITInetSocketDelegate>)delegate;
112 -(void) connectToHost:(NSString*)host onPort:(short)port;
113 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)port;
114 -(void) connectWithSockaddrArray:(NSArray*)arr;
115 -(ITInetSocketState) state;
116 -(void) retryConnection;
117 -(void) disconnect;
118 @end