2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #ifndef __NGNet_NGActiveSocket_H__
24 #define __NGNet_NGActiveSocket_H__
26 #import <Foundation/NSDate.h>
27 #include <NGStreams/NGSocket.h>
28 #include <NGStreams/NGSocketProtocols.h>
29 #include <NGStreams/NGStreamProtocols.h>
31 @class NSData, NSFileHandle;
34 Represents an active STREAM socket based on the standard Unix sockets library.
36 An active socket can be either a socket gained by calling accept with an
37 passive socket or by explicitly connecting one to an address (a client socket).
38 Therefore an active socket has two addresses, the local and the remote one.
40 There are three methods to perform a close, this is rooted in the fact that
41 a socket actually is full-duplex, it provides a send and a receive channel.
42 The stream-mode is updated according to what channels are open/closed.
43 Initially the socket is full-duplex and you cannot reopen a channel that was
44 shutdown. If you have shutdown both channels the socket can be considered
48 @interface NGActiveSocket : NGSocket < NGActiveSocket >
51 id<NGSocketAddress> remoteAddress;
54 NSTimeInterval receiveTimeout;
55 NSTimeInterval sendTimeout;
58 + (id)socketConnectedToAddress:(id<NGSocketAddress>)_address;
59 - (id)initWithDomain:(id<NGSocketDomain>)_domain; // designated initializer
62 + (BOOL)socketPair:(id<NGSocket>[2])_pair inDomain:(id<NGSocketDomain>)_domain;
65 // ******************** operations ********************
68 // NGSocketAlreadyConnectedException when the socket is already connected
69 // NGInvalidSocketDomainException when the remote domain != local domain
70 // NGCouldNotCreateSocketException if the socket creation failed
71 - (BOOL)connectToAddress:(id<NGSocketAddress>)_address;
73 - (BOOL)shutdown; // do a complete shutdown
74 - (BOOL)shutdownSendChannel;
75 - (BOOL)shutdownReceiveChannel;
77 // ******************** accessors *********************
79 - (id<NGSocketAddress>)remoteAddress;
83 - (void)setSendTimeout:(NSTimeInterval)_timeout;
84 - (NSTimeInterval)sendTimeout;
85 - (void)setReceiveTimeout:(NSTimeInterval)_timeout;
86 - (NSTimeInterval)receiveTimeout;
88 // test whether a read, a write or both would block the thread (using select)
89 - (BOOL)wouldBlockInMode:(NGStreamMode)_mode;
90 - (int)waitForMode:(NGStreamMode)_mode timeout:(NSTimeInterval)_timeout;
91 - (unsigned)numberOfAvailableBytesForReading;
94 // ******************** NGStream **********************
97 // NGStreamReadErrorException when the read call failed
98 // NGSocketNotConnectedException when the socket is not connected
99 // NGEndOfStreamException when the end of the stream is reached
100 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len;
103 // NGStreamWriteErrorException when the write call failed
104 // NGSocketNotConnectedException when the socket is not connected
105 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len;
107 - (BOOL)flush; // does nothing, sockets are unbuffered
108 - (NGStreamMode)mode; // returns read/write
110 - (BOOL)safeReadBytes:(void *)_buf count:(unsigned)_len;
111 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len;
115 @interface NGActiveSocket(DataMethods)
117 - (NSData *)readDataOfLength:(unsigned int)_length;
118 - (NSData *)safeReadDataOfLength:(unsigned int)_length;
119 - (unsigned int)writeData:(NSData *)_data;
120 - (BOOL)safeWriteData:(NSData *)_data;
124 #endif /* __NGNet_NGActiveSocket_H__ */