2
Copyright (C) 2000-2005 SKYRIX Software AG
4
This file is part of SOPE.
6
SOPE 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
SOPE 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 SOPE; see the file COPYING. If not, write to the
18
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22
#ifndef __NGNet_NGSocket_H__
23
#define __NGNet_NGSocket_H__
25
#import <Foundation/NSObject.h>
26
#include <NGStreams/NGSocketProtocols.h>
32
@class NSFileHandle, NSException;
35
Represents the sockets accessible through the standard Unix sockets library.
36
The socket class itself is abstract and has two concrete subclasses,
37
NGActiveSocket and NGPassiveSocket. The terminology may be confusing
38
at first, but I choose to use these instead of Client/ServerSocket
39
because the NGPassiveSocket accept method returns a socket which isn't
40
one of these. It's an active socket which is already connected.
41
NGActiveSocket represents a connection while NGPassiveSocket only accepts
42
connections (it can't be read or written).
44
Each socket has a local address. The socket can be bound to an address
45
by using the bind() call or the address can be assigned by the operating
47
Passive sockets are normally bound to a well-known port (or service),
48
active sockets receive in most cases their address from the kernel.
50
fd is the file descriptor gained through the socket() call. closeOnFree
51
specifies whether the socket is closed when the memory for the
52
socket object is reclaimed.
54
Note that the creation of the actual socket has to wait until it is
55
bound or a connect or listen call has been initiated. This is because
56
the socket() call needs the socket-domain, which is encapsulated in the
57
NGSocketAddress objects.
58
Until the socket is created the fd variable contains the value
59
NGInvalidSocketDescriptor.
63
# define NGInvalidSocketDescriptor INVALID_SOCKET
65
# define NGInvalidSocketDescriptor ((int)-1)
68
@interface NGSocket : NSObject < NGSocket >
74
int fd; // socket descriptor
76
id<NGSocketDomain> domain;
77
id<NGSocketAddress> localAddress;
78
NSFileHandle *fileHandle; // not retained !
81
int closeOnFree:1; // close socket on collect/dealloc ?
82
int isBound:1; // was a bind issued (either by the kernel or explicitly)
85
NSException *lastException;
88
+ (id)socketInDomain:(id<NGSocketDomain>)_domain;
89
- (id)initWithDomain:(id<NGSocketDomain>)_domain; // designated initializer
91
// ************************* create a socket *****************
93
- (BOOL)primaryCreateSocket;
96
// ************************* bind a socket *******************
99
// NGSocketAlreadyBoundException if the socket is already bound
100
- (BOOL)bindToAddress:(id<NGSocketAddress>)_address;
103
// NGSocketAlreadyBoundException if the socket is already bound
104
- (BOOL)kernelBoundAddress;
106
// ************************* accessors ***********************
108
- (id<NGSocketAddress>)localAddress;
111
- (void)setLastException:(NSException *)_exception;
112
- (NSException *)lastException;
113
- (void)resetLastException;
115
- (int)socketType; // abstract
116
- (id<NGSocketDomain>)domain;
118
- (NSFileHandle *)fileHandle;
120
- (SOCKET)fileDescriptor;
122
- (int)fileDescriptor;
125
// ************************* options *************************
127
// set methods throw NGCouldNotSetSocketOptionException
128
// get methods throw NGCouldNotGetSocketOptionException
130
- (void)setDebug:(BOOL)_flag;
131
- (void)setReuseAddress:(BOOL)_flag;
132
- (void)setKeepAlive:(BOOL)_flag;
133
- (void)setDontRoute:(BOOL)_flag;
135
- (BOOL)doesReuseAddress;
136
- (BOOL)doesKeepAlive;
137
- (BOOL)doesNotRoute;
139
- (void)setSendBufferSize:(int)_size;
140
- (void)setReceiveBufferSize:(int)_size;
141
- (int)sendBufferSize;
142
- (int)receiveBufferSize;
148
// Windows Descriptor Functions
152
# define POLLRDNORM 1
153
# define POLLIN POLLRDNORM
154
# define POLLWRNORM 2
155
# define POLLOUT POLLWRNORM
161
Polls a descriptor. Returns 1 if events occurred, 0 if a timeout occured
162
and -1 if an error other than EINTR or EAGAIN occured.
164
int NGPollDescriptor(SOCKET _fd, short _events, int _timeout);
167
Reading and writing with non-blocking IO support.
169
-1 on error, with errno set to either recv's or poll's errno
170
0 on the end of file condition
171
-2 if the operation timed out
173
Enable login topic 'nonblock' to find out about timeouts.
175
int NGDescriptorRecv(SOCKET _fd, char *_buf, int _len, int _flags, int _timeout);
176
int NGDescriptorSend(SOCKET _fd, const char *_buf, int _len, int _flags, int _timeout);
180
#endif /* __NGNet_NGSocket_H__ */