~ubuntu-branches/ubuntu/edgy/sope/edgy

« back to all changes in this revision

Viewing changes to sope-core/NGStreams/NGStreams/NGSocket.h

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Ley
  • Date: 2005-08-19 16:53:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050819165331-hs683wz1osm708pw
Tags: upstream-4.4rc.2
ImportĀ upstreamĀ versionĀ 4.4rc.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2000-2005 SKYRIX Software AG
 
3
 
 
4
  This file is part of SOPE.
 
5
 
 
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
 
9
  later version.
 
10
 
 
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.
 
15
 
 
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
 
19
  02111-1307, USA.
 
20
*/
 
21
 
 
22
#ifndef __NGNet_NGSocket_H__
 
23
#define __NGNet_NGSocket_H__
 
24
 
 
25
#import <Foundation/NSObject.h>
 
26
#include <NGStreams/NGSocketProtocols.h>
 
27
 
 
28
#if defined(WIN32)
 
29
#  include <winsock.h>
 
30
#endif
 
31
 
 
32
@class NSFileHandle, NSException;
 
33
 
 
34
/*
 
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).
 
43
 
 
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
 
46
  system kernel.
 
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.
 
49
 
 
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.
 
53
 
 
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.
 
60
*/
 
61
 
 
62
#if defined(WIN32)
 
63
#  define NGInvalidSocketDescriptor INVALID_SOCKET
 
64
#else
 
65
#  define NGInvalidSocketDescriptor ((int)-1)
 
66
#endif
 
67
 
 
68
@interface NGSocket : NSObject < NGSocket >
 
69
{
 
70
@protected
 
71
#if defined(WIN32)
 
72
  SOCKET              fd;
 
73
#else
 
74
  int                 fd;           // socket descriptor
 
75
#endif
 
76
  id<NGSocketDomain>  domain;
 
77
  id<NGSocketAddress> localAddress;
 
78
  NSFileHandle        *fileHandle;  // not retained !
 
79
 
 
80
  struct {
 
81
    int closeOnFree:1; // close socket on collect/dealloc ?
 
82
    int isBound:1;     // was a bind issued (either by the kernel or explicitly)
 
83
  } flags;
 
84
  
 
85
  NSException *lastException;
 
86
}
 
87
 
 
88
+ (id)socketInDomain:(id<NGSocketDomain>)_domain;
 
89
- (id)initWithDomain:(id<NGSocketDomain>)_domain; // designated initializer
 
90
 
 
91
// ************************* create a socket *****************
 
92
 
 
93
- (BOOL)primaryCreateSocket;
 
94
- (BOOL)close;
 
95
 
 
96
// ************************* bind a socket *******************
 
97
 
 
98
// throws
 
99
//   NGSocketAlreadyBoundException    if the socket is already bound
 
100
- (BOOL)bindToAddress:(id<NGSocketAddress>)_address;
 
101
 
 
102
// throws
 
103
//   NGSocketAlreadyBoundException    if the socket is already bound
 
104
- (BOOL)kernelBoundAddress;
 
105
 
 
106
// ************************* accessors ***********************
 
107
 
 
108
- (id<NGSocketAddress>)localAddress;
 
109
- (BOOL)isBound;
 
110
 
 
111
- (void)setLastException:(NSException *)_exception;
 
112
- (NSException *)lastException;
 
113
- (void)resetLastException;
 
114
 
 
115
- (int)socketType;       // abstract
 
116
- (id<NGSocketDomain>)domain;
 
117
 
 
118
- (NSFileHandle *)fileHandle;
 
119
#if defined(WIN32)
 
120
- (SOCKET)fileDescriptor;
 
121
#else
 
122
- (int)fileDescriptor;
 
123
#endif
 
124
 
 
125
// ************************* options *************************
 
126
//
 
127
//   set methods throw NGCouldNotSetSocketOptionException
 
128
//   get methods throw NGCouldNotGetSocketOptionException
 
129
 
 
130
- (void)setDebug:(BOOL)_flag;
 
131
- (void)setReuseAddress:(BOOL)_flag;
 
132
- (void)setKeepAlive:(BOOL)_flag;
 
133
- (void)setDontRoute:(BOOL)_flag;
 
134
- (BOOL)doesDebug;
 
135
- (BOOL)doesReuseAddress;
 
136
- (BOOL)doesKeepAlive;
 
137
- (BOOL)doesNotRoute;
 
138
 
 
139
- (void)setSendBufferSize:(int)_size;
 
140
- (void)setReceiveBufferSize:(int)_size;
 
141
- (int)sendBufferSize;
 
142
- (int)receiveBufferSize;
 
143
 
 
144
@end
 
145
 
 
146
#if defined(WIN32)
 
147
 
 
148
// Windows Descriptor Functions
 
149
 
 
150
// events
 
151
#  ifndef POLLIN
 
152
#    define POLLRDNORM 1
 
153
#    define POLLIN     POLLRDNORM
 
154
#    define POLLWRNORM 2
 
155
#    define POLLOUT    POLLWRNORM
 
156
#    define POLLERR    4
 
157
#    define POLLHUP    4
 
158
#  endif
 
159
 
 
160
/*
 
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.
 
163
*/
 
164
int NGPollDescriptor(SOCKET _fd, short _events, int _timeout);
 
165
 
 
166
/*
 
167
  Reading and writing with non-blocking IO support.
 
168
  The functions return
 
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
 
172
 
 
173
  Enable login topic 'nonblock' to find out about timeouts.
 
174
*/
 
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);
 
177
 
 
178
#endif /* WIN32 */
 
179
 
 
180
#endif /* __NGNet_NGSocket_H__ */