Adding ITDebug.h to the framework header.
[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 <DataReciever>
58 /*!
59  * @method errorOccured:during:onSocket:
60  * @abstract Alerts the delegate of an error condition.
61  * @discussion The delegate can try retryCondition.
62  * @param err The error class.
63  * @param state What the socket was doing when the error occured.
64  * @param sender The socket the error occured on.
65  */
66 - (oneway void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(ITInetSocket*)sender;
67 /*!
68  * @method finishedConnecting:
69  * @abstract Alerts the delegate of a successful connection attempt.
70  * @discussion The delegate should send whatever initial data is required for the protocol (nickname for IRC, etc.)
71  * @param sender The socket that established the connection.
72  */
73 - (oneway void) finishedConnecting:(ITInetSocket *)sender;
74 @end
75
76 /*!
77  * @class ITInetSocket
78  * @abstract An Internet socket class.
79  * @discussion ITInetSocket is an Internet socket class supporting IPv6 and Rendezvous.
80  */
81 @interface ITInetSocket : NSObject <DataReciever> {
82     int sockfd;
83     int port;
84     int nc;
85     unsigned short bufs;
86     volatile int dieflag;
87     volatile int actionflag;
88     id <ITInetSocketDelegate> delegate;
89     struct addrinfo *ai, *ai_cur;
90     ITByteStream *readPipe, *writePipe;
91     ITInetSocketState state;
92     NSArray *sarr;
93 }
94 /*!
95  * @method startAutoconnectingToService:delegate:
96  * @abstract Automatically creates sockets whenever a certain type of Rendezvous service appears.
97  * @discussion The auto-created sockets will send finishedConnecting: to the delegate.
98  * @param type The type of Rendezvous service to listen on.
99  * @param d The delegate that the sockets will belong to.
100  */
101 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate>)d;
102 /*!
103  * @method initWithFD:delegate:
104  * @abstract Wraps a socket around an existing socket descriptor.
105  * @discussion The socket will start listening on the descriptor as normal.
106  * @param fd The descriptor.
107  * @param d The delegate for the socket.
108  */
109 -(id) initWithFD:(int)fd delegate:(id <ITInetSocketDelegate>)d;
110 /*!
111  * @method initWithDelegate:
112  * @abstract Creates a new socket.
113  * @discussion The socket will not be connected to anything.
114  * @param d The delegate of the socket.
115  */
116 -(id) initWithDelegate:(id <ITInetSocketDelegate>)d;
117
118 -(id <ITInetSocketDelegate>)delegate;
119 -(unsigned short)bufferSize;
120 -(void)setBufferSize:(unsigned short)bufs;
121 -(void) connectToHost:(NSString*)host onPort:(short)port;
122 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)port;
123 -(void) connectWithSockaddrArray:(NSArray*)arr;
124 -(ITInetSocketState) state;
125 -(void) retryConnection;
126 -(void) disconnect;
127 -(ITByteStream *)readPipe;
128 -(ITByteStream *)writePipe;
129 @end