]> err.no Git - sope/blob - sope-core/NGStreams/NGStreams/NGActiveSocket.h
Id fixes
[sope] / sope-core / NGStreams / NGStreams / NGActiveSocket.h
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #ifndef __NGNet_NGActiveSocket_H__
24 #define __NGNet_NGActiveSocket_H__
25
26 #import <Foundation/NSDate.h>
27 #include <NGStreams/NGSocket.h>
28 #include <NGStreams/NGSocketProtocols.h>
29 #include <NGStreams/NGStreamProtocols.h>
30
31 @class NSData, NSFileHandle;
32
33 /*
34   Represents an active STREAM socket based on the standard Unix sockets library.
35
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.
39
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
45   closed.
46 */
47
48 @interface NGActiveSocket : NGSocket < NGActiveSocket >
49 {
50 @private
51   id<NGSocketAddress> remoteAddress;
52   NGStreamMode        mode;
53   
54   NSTimeInterval receiveTimeout;
55   NSTimeInterval sendTimeout;
56 }
57
58 + (id)socketConnectedToAddress:(id<NGSocketAddress>)_address;
59 - (id)initWithDomain:(id<NGSocketDomain>)_domain; // designated initializer
60
61 #if !defined(WIN32)
62 + (BOOL)socketPair:(id<NGSocket>[2])_pair inDomain:(id<NGSocketDomain>)_domain;
63 #endif
64
65 // ******************** operations ********************
66
67 // throws
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;
72
73 - (BOOL)shutdown; // do a complete shutdown
74 - (BOOL)shutdownSendChannel;
75 - (BOOL)shutdownReceiveChannel;
76
77 // ******************** accessors *********************
78
79 - (id<NGSocketAddress>)remoteAddress;
80 - (BOOL)isConnected;
81 - (BOOL)isOpen;
82
83 - (void)setSendTimeout:(NSTimeInterval)_timeout;
84 - (NSTimeInterval)sendTimeout;
85 - (void)setReceiveTimeout:(NSTimeInterval)_timeout;
86 - (NSTimeInterval)receiveTimeout;
87
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;
92 - (BOOL)isAlive;
93
94 // ******************** NGStream **********************
95
96 // throws
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;
101
102 // throws
103 //   NGStreamWriteErrorException   when the write call failed
104 //   NGSocketNotConnectedException when the socket is not connected
105 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len;
106
107 - (BOOL)flush;                  // does nothing, sockets are unbuffered
108 - (NGStreamMode)mode;           // returns read/write
109
110 - (BOOL)safeReadBytes:(void *)_buf count:(unsigned)_len;
111 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len;
112
113 @end
114
115 @interface NGActiveSocket(DataMethods)
116
117 - (NSData *)readDataOfLength:(unsigned int)_length;
118 - (NSData *)safeReadDataOfLength:(unsigned int)_length;
119 - (unsigned int)writeData:(NSData *)_data;
120 - (BOOL)safeWriteData:(NSData *)_data;
121
122 @end
123
124 #endif /* __NGNet_NGActiveSocket_H__ */