author | Merge Script |
Fri, 16 Dec 2016 06:46:40 +0100 | |
branch | jv |
changeset 4235 | 6648f07e2ddd |
parent 4215 | 195c5c496e71 |
parent 4234 | eb1b470a27ee |
child 4252 | be9c3684d375 |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
42 | 3 |
All Rights Reserved |
0 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
888 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3462 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
63 | 16 |
NonPositionableExternalStream subclass:#Socket |
4234 | 17 |
instanceVariableNames:'domain socketType protocol port peer peerName listening' |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
18 |
classVariableNames:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
19 |
poolDictionaries:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
20 |
category:'Streams-External' |
0 | 21 |
! |
22 |
||
126 | 23 |
!Socket primitiveDefinitions! |
24 |
%{ |
|
1009 | 25 |
/* #define DGRAM_DEBUG /* */ |
26 |
||
3590 | 27 |
#ifdef __MINGW__ |
28 |
# include <stdio.h> |
|
29 |
#endif |
|
1343 | 30 |
#include "stxOSDefs.h" |
126 | 31 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
32 |
#ifdef __win32__ |
3123 | 33 |
//#define DEBUG 1 |
2818 | 34 |
/* this is to catch uses of those - there should be none */ |
1503 | 35 |
# undef __BEGIN_INTERRUPTABLE__ |
36 |
# undef __END_INTERRUPTABLE__ |
|
2818 | 37 |
# ifdef __TCC__ |
38 |
# define __BEGIN_INTERRUPTABLE__ xxxx |
|
39 |
# define __END_INTERRUPTABLE__ yyyy |
|
40 |
# else |
|
41 |
# define __BEGIN_INTERRUPTABLE__ .. |
|
42 |
# define __END_INTERRUPTABLE__ .. |
|
43 |
# endif |
|
1503 | 44 |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
45 |
# define WRAP_STDIO |
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
46 |
// The default is linger until all data is sent in background |
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
47 |
// so the close on the socket is non-blocking |
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
48 |
// linger.onoff=off linger.time= *irrelevant* |
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
49 |
//# define SET_LINGER_WHEN_CREATING_SOCKET |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
50 |
|
3742 | 51 |
# ifdef __xxMINGW__ |
2909 | 52 |
extern HANDLE _get_osfhandle(); |
1845 | 53 |
# endif |
54 |
||
3620 | 55 |
# if defined(__BORLANDC__) |
56 |
// not defined in borland headers |
|
57 |
typedef unsigned int socklen_t; |
|
58 |
# endif |
|
59 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
60 |
# if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ )) |
2915 | 61 |
# define SOCKET_FROM_FD(fd) (SOCKET)(_get_osfhandle(fd)) |
2909 | 62 |
# else |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
63 |
# define SOCKET_FROM_FD(fd) (SOCKET)(fd) |
2909 | 64 |
# endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
65 |
#else /* not __win32__ */ |
3215 | 66 |
# define SOCKET_FROM_FD(fd) (fd) |
67 |
# define closesocket(sock) close(sock) |
|
68 |
# define SOCKET int |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
69 |
#endif /* ! __win32__ */ |
1503 | 70 |
|
1843
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
71 |
#define SOCKET_FROM_FILE(f) (SOCKET_FROM_FD(fileno(f))) |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
72 |
#define SOCKET_FROM_FILE_OBJECT(f) (__INST(handleType) == @symbol(socketHandle) ? SOCKET_FROM_FD(__FILEVal(f)) : SOCKET_FROM_FILE(__FILEVal(f))) |
1539
c0e89dcb58b3
non blocking stuff done right here;
Claus Gittinger <cg@exept.de>
parents:
1538
diff
changeset
|
73 |
|
3590 | 74 |
#ifndef __MINGW__ |
75 |
# include <stdio.h> |
|
76 |
#endif |
|
126 | 77 |
#include <errno.h> |
78 |
||
1343 | 79 |
#ifdef WANT__TCP_DOT_H |
80 |
# include <netinet/tcp.h> |
|
81 |
#endif |
|
82 |
||
1344 | 83 |
#ifdef WANT__NETDB_DOT_H |
84 |
# include <netdb.h> |
|
1343 | 85 |
#endif |
86 |
||
739
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
87 |
#if defined(TRY_AGAIN) || defined(HOST_NOT_FOUND) |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
88 |
# define USE_H_ERRNO |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
89 |
#endif |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
90 |
|
126 | 91 |
/* |
92 |
* on some systems errno is a macro ... check for it here |
|
93 |
*/ |
|
94 |
#ifndef errno |
|
95 |
extern errno; |
|
96 |
#endif |
|
97 |
||
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
98 |
#ifdef USE_H_ERRNO |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
99 |
# ifndef h_errno |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
100 |
extern h_errno; |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
101 |
# endif |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
102 |
#endif |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
103 |
|
126 | 104 |
#ifdef DEBUG |
1839 | 105 |
# define DBGPRINTF(x) { if (__debugging__) console_printf x; } |
106 |
# define DBGFPRINTF(x) { if (__debugging__) console_fprintf x; } |
|
126 | 107 |
#else |
108 |
# define DBGPRINTF(x) /* as nothing */ |
|
821
40b0b69bf4b5
more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
109 |
# define DBGFPRINTF(x) /* as nothing */ |
126 | 110 |
#endif |
111 |
||
1531 | 112 |
#ifndef TRUE |
1535 | 113 |
# define TRUE 1 |
1531 | 114 |
#endif |
115 |
#ifndef FALSE |
|
1535 | 116 |
# define FALSE 0 |
1531 | 117 |
#endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
118 |
#ifndef __win32__ |
1531 | 119 |
typedef int BOOL; |
120 |
#endif |
|
121 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
122 |
#ifdef __win32__ |
1843
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
123 |
# undef stdout |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
124 |
# undef stderr |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
125 |
# define stdout __win32_stdout() |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
126 |
# define stderr __win32_stderr() |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
127 |
#endif |
126 | 128 |
%} |
129 |
! ! |
|
130 |
||
511 | 131 |
!Socket primitiveVariables! |
132 |
%{ |
|
1844 | 133 |
static int __debugging__ = 0; |
511 | 134 |
%} |
135 |
! ! |
|
136 |
||
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
137 |
!Socket primitiveFunctions! |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
138 |
%{ |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
139 |
|
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
140 |
static int |
3616 | 141 |
setupBufferParameters(OBJ aDataBuffer, OBJ startIndex, char **p_extPtr, INT *p_offs, size_t *p_objSize) |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
142 |
{ |
3657 | 143 |
char *extPtr = 0; |
144 |
int sIdx = 0, objSize = 0, offs = 0; |
|
145 |
||
146 |
sIdx = 0; |
|
147 |
if (__isSmallInteger(startIndex)) { |
|
148 |
sIdx = __intVal(startIndex) - 1; |
|
149 |
} |
|
150 |
||
151 |
if (__isExternalBytesLike(aDataBuffer)) { |
|
152 |
OBJ sz; |
|
153 |
||
154 |
extPtr = (char *)(__externalBytesAddress(aDataBuffer)); |
|
155 |
sz = __externalBytesSize(aDataBuffer); |
|
156 |
if (__isSmallInteger(sz)) { |
|
157 |
objSize = __intVal(sz); |
|
158 |
} else { |
|
159 |
objSize = 0; /* unknown */ |
|
160 |
} |
|
161 |
offs = sIdx; |
|
162 |
} else { |
|
163 |
OBJ oClass; |
|
164 |
int nInstVars, nInstBytes; |
|
165 |
||
166 |
extPtr = (char *)0; |
|
167 |
oClass = __Class(aDataBuffer); |
|
168 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
169 |
case BYTEARRAY: |
|
170 |
offs = sIdx; |
|
171 |
break; |
|
172 |
case WORDARRAY: |
|
173 |
case SWORDARRAY: |
|
174 |
offs = sIdx * 2; |
|
175 |
break; |
|
176 |
case LONGARRAY: |
|
177 |
case SLONGARRAY: |
|
178 |
offs = sIdx * 4; |
|
179 |
break; |
|
180 |
case LONGLONGARRAY: |
|
181 |
case SLONGLONGARRAY: |
|
182 |
offs = sIdx * 8; |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
183 |
# ifdef __NEED_LONGLONG_ALIGN |
3657 | 184 |
offs += 4; |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
185 |
# endif |
3657 | 186 |
break; |
187 |
case FLOATARRAY: |
|
188 |
offs = sIdx * sizeof(float); |
|
189 |
break; |
|
190 |
case DOUBLEARRAY: |
|
191 |
offs = sIdx * sizeof(double); |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
192 |
# ifdef __NEED_DOUBLE_ALIGN |
3657 | 193 |
offs += 4; |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
194 |
# endif |
3657 | 195 |
break; |
196 |
default: |
|
197 |
*p_objSize = -1; |
|
198 |
return 0; |
|
199 |
} |
|
200 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
201 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
|
202 |
offs = offs + nInstBytes; |
|
203 |
objSize = __Size(aDataBuffer) - offs; |
|
204 |
} |
|
205 |
*p_extPtr = extPtr; |
|
206 |
*p_objSize = objSize; |
|
207 |
*p_offs = offs; |
|
208 |
return 1; |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
209 |
} |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
210 |
%} |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
211 |
! ! |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
212 |
|
464 | 213 |
!Socket class methodsFor:'documentation'! |
25 | 214 |
|
31 | 215 |
copyright |
216 |
" |
|
217 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
42 | 218 |
All Rights Reserved |
31 | 219 |
|
220 |
This software is furnished under a license and may be used |
|
221 |
only in accordance with the terms of that license and with the |
|
222 |
inclusion of the above copyright notice. This software may not |
|
223 |
be provided or otherwise made available to, or used by, any |
|
224 |
other person. No title to or ownership of the software is |
|
225 |
hereby transferred. |
|
226 |
" |
|
227 |
! |
|
228 |
||
25 | 229 |
documentation |
230 |
" |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
231 |
This class provides access to sockets for interprocess communication. |
25 | 232 |
|
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
233 |
Currently, only IP and UNIX domain sockets are really fully |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
234 |
tested and supported. |
525 | 235 |
Code for appletalk is included, but was never tested ... |
236 |
More may be added in the future. |
|
237 |
(the code is prepared for things like SNA or decNet; |
|
238 |
however, right now, this code is empty and needs a little work. |
|
3989 | 239 |
Implementing those is pretty straightforward, once the address |
525 | 240 |
data structures are known.) |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
241 |
|
28 | 242 |
Due to historic reasons (I started this class, before I got hold of some |
243 |
code using ST-80 Sockets i.e. RemoteInvocation), there is some old interface |
|
3481 | 244 |
which is still supported. |
245 |
This may vanish; use the #family:type: or #newTCPxxx and #newUDPxxx interfaces, |
|
71 | 246 |
together with the bind/listen and accept calls, |
247 |
which are meant to be compatible to ST-80's UnixSocketAccessor interface. |
|
248 |
||
3481 | 249 |
TODO: cleanup historic leftovers, |
3989 | 250 |
change to raise more signals on errors. |
259 | 251 |
|
252 |
[author:] |
|
3989 | 253 |
Claus Gittinger |
48 | 254 |
" |
255 |
! |
|
28 | 256 |
|
48 | 257 |
examples |
258 |
" |
|
28 | 259 |
example (get help info from an nntp server): |
3985 | 260 |
[exBegin] |
261 |
|sock| |
|
262 |
||
263 |
sock := Socket newTCPclientToHost:'smtp.exept.de' port:'smtp'. |
|
264 |
sock isNil ifTrue:[ |
|
265 |
self warn:'no smtp daemon is running'. |
|
266 |
^ self |
|
267 |
]. |
|
268 |
Transcript showCR:sock nextLine. |
|
269 |
||
270 |
sock nextPutAll:'HELO STX socket test'; cr. |
|
271 |
Transcript showCR:sock nextLine. |
|
272 |
sock close |
|
273 |
[exEnd] |
|
209 | 274 |
|
28 | 275 |
|
209 | 276 |
example (connect to finger daemon, get users entry): |
3985 | 277 |
[exBegin] |
278 |
|sock entry| |
|
279 |
||
280 |
sock := Socket newTCPclientToHost:'localhost' port:'finger'. |
|
281 |
sock isNil ifTrue:[ |
|
282 |
self warn:'no finger daemon is running'. |
|
283 |
^ self |
|
284 |
]. |
|
285 |
sock useCRLF:true. |
|
286 |
sock buffered:false. |
|
287 |
sock isNil ifTrue:[ |
|
288 |
Transcript showCR:'cannot connect to local finger daemon' |
|
289 |
] ifFalse:[ |
|
290 |
sock nextPutAll:(OperatingSystem getLoginName). |
|
291 |
sock cr. |
|
292 |
||
293 |
entry := sock nextLine. |
|
294 |
Transcript showCR:entry. |
|
295 |
||
296 |
sock close |
|
297 |
] |
|
298 |
[exEnd] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
299 |
|
28 | 300 |
example (connect to an ftp server): |
3985 | 301 |
[exBegin] |
302 |
|sock| |
|
303 |
||
304 |
sock := Socket newTCPclientToHost:'www.exept.de' port:'ftp'. |
|
305 |
||
306 |
sock buffered:false. |
|
307 |
Transcript showCR:sock nextLine. |
|
308 |
sock nextPutAll:('USER ' , 'anonymous'); cr. |
|
309 |
Transcript showCR:sock nextLine. |
|
310 |
sock nextPutAll:('PASS ' , 'fooBar'); cr. |
|
311 |
Transcript showCR:sock nextLine. |
|
312 |
sock nextPutAll:'HELP'; cr. |
|
313 |
[ |
|
314 |
|line| |
|
315 |
line := sock nextLine. |
|
316 |
Transcript showCR:line. |
|
317 |
(line at:4) = $- |
|
318 |
] whileTrue. |
|
319 |
sock close. |
|
320 |
||
4192 | 321 |
'don't know enough of the ftp protocol to continue here ...' |
3985 | 322 |
[exEnd] |
28 | 323 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
324 |
|
93 | 325 |
example (connect to an snmp server [UDP]): |
3985 | 326 |
Note: this is not a real connection, only the destination address is |
327 |
being fixed. |
|
328 |
[exBegin] |
|
329 |
|sock port| |
|
330 |
||
331 |
sock := Socket newUDP. |
|
332 |
port := Socket portOfService:'snmp'. |
|
333 |
sock connectTo:'localhost' port:port. |
|
334 |
sock buffered:false. |
|
335 |
Transcript showCR:'got it'. |
|
336 |
sock close. |
|
337 |
[exEnd] |
|
42 | 338 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
339 |
|
42 | 340 |
example (await connection from a client and read some data): |
3985 | 341 |
[exBegin] |
342 |
|connectSock sock| |
|
343 |
||
344 |
connectSock := Socket newTCPserverAtPort:9998. |
|
345 |
connectSock isNil ifTrue:[ |
|
346 |
Transcript showCR:'socket setup failed.'. |
|
347 |
] ifFalse:[ |
|
348 |
Transcript showCR:'listen ..'. |
|
349 |
(connectSock listenFor:5) ifFalse:[ |
|
350 |
Transcript showCR:'listen failed.'. |
|
351 |
] ifTrue:[ |
|
352 |
Transcript showCR:'wait'. |
|
353 |
connectSock readWait. |
|
354 |
Transcript showCR:'accept'. |
|
355 |
sock := connectSock accept. |
|
356 |
sock isNil ifTrue:[ |
|
357 |
Transcript showCR:'accept failed.'. |
|
358 |
] ifFalse:[ |
|
359 |
sock buffered:false. |
|
360 |
Transcript showCR:'server: got it'. |
|
361 |
'can now do transfer via sock'. |
|
362 |
Transcript showCR:'read'. |
|
363 |
Transcript showCR:('got: ' , sock nextLine). |
|
364 |
||
365 |
Transcript showCR:'close'. |
|
366 |
sock close |
|
367 |
]. |
|
368 |
connectSock close. |
|
369 |
] |
|
370 |
] |
|
371 |
[exEnd] |
|
42 | 372 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
373 |
|
42 | 374 |
example (connect to above server and send some data): |
3985 | 375 |
[exBegin] |
376 |
|sock| |
|
377 |
||
378 |
sock := Socket newTCPclientToHost:'localhost' port:9998. |
|
379 |
sock isNil ifTrue:[ |
|
380 |
Transcript showCR:'nope' |
|
381 |
] ifFalse:[ |
|
382 |
sock buffered:false. |
|
383 |
Transcript showCR:'client: got it'. |
|
384 |
'can now do transfer via sock'. |
|
385 |
Transcript showCR:'sending <hello>'. |
|
386 |
sock nextPutLine:'hello'. |
|
387 |
sock close |
|
388 |
] |
|
389 |
[exEnd] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
390 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
391 |
example: UNIX domain socket (await connection from a client and read some data): |
1009 | 392 |
|
3985 | 393 |
|connectSock sock| |
394 |
||
395 |
'/tmp/ud_socket' asFilename remove. |
|
396 |
connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'. |
|
397 |
connectSock isNil ifTrue:[ |
|
398 |
Transcript showCR:'socket setup failed.'. |
|
399 |
] ifFalse:[ |
|
400 |
Transcript showCR:'listen ..'. |
|
401 |
(connectSock listenFor:5) ifFalse:[ |
|
402 |
Transcript showCR:'listen failed.'. |
|
403 |
] ifTrue:[ |
|
404 |
Transcript showCR:'wait'. |
|
405 |
connectSock buffered:false. |
|
406 |
connectSock readWait. |
|
407 |
Transcript showCR:'accept'. |
|
408 |
sock := connectSock accept. |
|
409 |
sock isNil ifTrue:[ |
|
410 |
Transcript showCR:'accept failed.'. |
|
411 |
] ifFalse:[ |
|
412 |
sock buffered:false. |
|
413 |
Transcript showCR:'server: got it'. |
|
414 |
'can now do transfer via sock'. |
|
415 |
Transcript showCR:'read'. |
|
416 |
Transcript showCR:('got: ' , sock nextLine). |
|
417 |
||
418 |
Transcript showCR:'close'. |
|
419 |
sock close |
|
420 |
]. |
|
421 |
connectSock close. |
|
422 |
] |
|
423 |
] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
424 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
425 |
|
118 | 426 |
example (connect to above server and send some data; |
3985 | 427 |
Notice, this fails, if above server code is executed in the same ST/X image |
428 |
(at least on LINUX), since the OS does not correctly handle |
|
429 |
a connect from within an interrupted accept system call |
|
430 |
On SGI's SVR4, this works ok |
|
431 |
[exBegin] |
|
432 |
|sock| |
|
433 |
||
434 |
sock := Socket newUNIXclientTo:'/tmp/ud_socket'. |
|
435 |
sock isNil ifTrue:[ |
|
436 |
Transcript showCR:'nope' |
|
437 |
] ifFalse:[ |
|
438 |
sock buffered:false. |
|
439 |
Transcript showCR:'client: got it'. |
|
440 |
'can now do transfer via sock'. |
|
441 |
Transcript showCR:'sending <hello>'. |
|
442 |
sock nextPutLine:'hello'. |
|
443 |
sock close |
|
444 |
] |
|
445 |
[exEnd] |
|
1007 | 446 |
|
447 |
||
448 |
example (UDP await packet from a client and read some data): |
|
3985 | 449 |
[exBegin] |
450 |
|udpSock sock addr n dataBuffer| |
|
451 |
||
452 |
udpSock := Socket newUDPserverAtPort:9999. |
|
453 |
udpSock isNil ifTrue:[ |
|
454 |
Transcript showCR:'socket setup failed.'. |
|
455 |
] ifFalse:[ |
|
456 |
Transcript showCR:'wait'. |
|
457 |
udpSock readWait. |
|
458 |
||
459 |
addr := IPSocketAddress new. |
|
460 |
dataBuffer := ByteArray new:1000. |
|
461 |
n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size. |
|
462 |
n > 0 ifTrue:[ |
|
463 |
Transcript showCR:('got: ' , n printString , 'bytes from ' , addr printString). |
|
464 |
Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString). |
|
465 |
] ifFalse:[ |
|
466 |
Transcript showCR:'read failed'. |
|
467 |
]. |
|
468 |
||
469 |
Transcript showCR:'close'. |
|
470 |
udpSock close |
|
471 |
] |
|
472 |
[exEnd] |
|
1007 | 473 |
example (connect to above UDP server and send some data; |
3985 | 474 |
[exBegin] |
475 |
|sock| |
|
476 |
||
477 |
sock := Socket newUDP. |
|
478 |
sock isNil ifTrue:[ |
|
479 |
Transcript showCR:'nope' |
|
480 |
] ifFalse:[ |
|
481 |
sock sendTo:(IPSocketAddress new hostName:'localhost' port:9999) buffer:'hello world'. |
|
482 |
sock close |
|
483 |
] |
|
484 |
[exEnd] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
485 |
|
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
486 |
example: pingWalk (try to ping hosts on the local network) |
2824 | 487 |
Note: it dosen't use ICMP ping, but tries to reache the echo service, |
3985 | 488 |
which is disabled on most OS. |
489 |
[exBegin] |
|
490 |
|myAddress list top hosts walkProcess port| |
|
491 |
||
492 |
myAddress := OperatingSystem getNetworkAddresses |
|
493 |
keysAndValuesSelect:[:eachIFName :eachAddress| |
|
494 |
eachAddress isLocal not |
|
495 |
and:[eachIFName = 'wlan0'] |
|
496 |
]. |
|
497 |
myAddress := myAddress first hostAddress. |
|
498 |
||
499 |
port := Socket portOfService:'echo'. |
|
500 |
port isNil ifTrue:[ |
|
501 |
self error:'dont know echo port'. |
|
502 |
^ self |
|
503 |
]. |
|
504 |
||
505 |
top := StandardSystemView new. |
|
506 |
top label:'PING net walk'. |
|
507 |
||
508 |
list := ScrollableView for:ListView in:top. |
|
509 |
list origin:0.0@0.0 corner:1.0@1.0. |
|
510 |
||
511 |
top openAndWait. |
|
512 |
||
513 |
walkProcess := [ |
|
514 |
|l low hi direction tryHostID dottedName hostName conn addr| |
|
515 |
||
516 |
l := SortedCollection new. |
|
517 |
||
518 |
' only works with type C-net |
|
519 |
the code below could simply do 1 to:254 do:[:hostID } |
|
520 |
but, to probe likely hosts earlier, the probing is done |
|
521 |
ping-pong like around my ip-address (assuming, that other machines |
|
522 |
have numbers around my own)'. |
|
523 |
||
524 |
low := hi := (myAddress at:4). |
|
525 |
direction := 1. |
|
526 |
||
527 |
[low > 0 or:[hi < 255]] whileTrue:[ |
|
528 |
direction > 0 ifTrue:[ |
|
529 |
hi := hi + 1. |
|
530 |
tryHostID := hi. |
|
531 |
direction := -1. |
|
532 |
] ifFalse:[ |
|
533 |
low := low - 1. |
|
534 |
tryHostID := low. |
|
535 |
direction := 1. |
|
536 |
]. |
|
537 |
(tryHostID between:1 and:254) ifTrue:[ |
|
538 |
dottedName := (myAddress at:1) printString |
|
539 |
, '.' , (myAddress at:2) printString |
|
540 |
, '.' , (myAddress at:3) printString |
|
541 |
, '.' , tryHostID printString. |
|
542 |
||
543 |
top label:'PING net walk - trying ' , dottedName. |
|
544 |
||
545 |
top windowGroup withCursor:Cursor wait do:[ |
|
546 |
conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000. |
|
547 |
conn notNil ifTrue:[ |
|
548 |
addr := Socket ipAddressOfHost:dottedName. |
|
549 |
hostName := Socket hostWithIpAddress:addr. |
|
550 |
hostName isNil ifTrue:[ |
|
551 |
hostName :='?' |
|
552 |
]. |
|
553 |
l add:(dottedName paddedTo:15 with:Character space) |
|
554 |
, ' ' |
|
555 |
, (hostName paddedTo:15 with:Character space) |
|
556 |
, ' up & reachable'. |
|
557 |
list list:l. |
|
558 |
conn close. |
|
559 |
] |
|
560 |
]. |
|
561 |
]. |
|
562 |
]. |
|
563 |
top label:'PING reachable hosts'. |
|
564 |
] forkAt:(Processor userBackgroundPriority). |
|
565 |
walkProcess name:'ping net walker'. |
|
566 |
[exEnd] |
|
567 |
||
568 |
||
569 |
This example creates a simple UDP server that accepts |
|
570 |
single packets from anybody and broadcasts them to all |
|
571 |
clients that have connected so far. |
|
572 |
||
573 |
[exBegin] |
|
574 |
| socket address buffer msgSize clients | |
|
575 |
clients := Set new. |
|
576 |
address := IPSocketAddress new. |
|
577 |
buffer := String new: 1024. |
|
578 |
||
579 |
socket := self newUDPserverAtPort: 6666. |
|
580 |
||
581 |
Transcript showCR: 'server starting'. |
|
582 |
||
583 |
[ |
|
584 |
[true] whileTrue: [ |
|
585 |
(socket readWaitWithTimeoutMs: 200) ifFalse: [ |
|
586 |
msgSize := socket |
|
587 |
receiveFrom: address |
|
588 |
buffer: buffer |
|
589 |
start: 1 |
|
590 |
for: buffer size. |
|
591 |
||
592 |
clients add: address copy. |
|
593 |
clients do: [ :clientAddress | |
|
594 |
socket |
|
595 |
sendTo: clientAddress |
|
596 |
buffer: buffer |
|
597 |
start: 1 |
|
598 |
for: msgSize]] |
|
599 |
] |
|
600 |
] ensure:[ |
|
601 |
Transcript showCR: 'server shutting down'. |
|
602 |
socket close |
|
603 |
] |
|
604 |
[exEnd] |
|
817 | 605 |
|
606 |
send a datagram to above server: |
|
3985 | 607 |
[exBegin] |
608 |
||
609 |
| socket address buffer host msg | |
|
610 |
||
611 |
host := Dialog |
|
612 |
request: 'What is the name of the server''s host?' |
|
613 |
initialAnswer: 'localhost'. |
|
614 |
||
615 |
socket := self newUDP. |
|
616 |
||
617 |
address := IPSocketAddress hostName: host port: 6666. |
|
618 |
||
619 |
buffer := ByteArray new: 1000. |
|
620 |
[ |
|
621 |
[(msg := Dialog request: 'Say something') isEmpty] whileFalse:[ |
|
622 |
| replySize stream | |
|
623 |
||
624 |
socket writeWait. |
|
625 |
stream := buffer writeStream. |
|
626 |
stream nextPutAll: msg. |
|
627 |
socket sendTo:address buffer:buffer start:1 for:stream position. |
|
628 |
socket readWait. |
|
629 |
||
630 |
replySize := socket receiveFrom:address buffer:buffer. |
|
631 |
replySize > 0 ifTrue: [ |
|
632 |
Transcript cr; nextPutAll: 'Server acknowledged: '. |
|
633 |
Transcript show: ((buffer copyFrom: 1 to: replySize) asString) |
|
634 |
] |
|
635 |
] |
|
636 |
] ensure: [socket close]. |
|
637 |
Transcript cr |
|
638 |
[exEnd] |
|
1316 | 639 |
|
640 |
||
641 |
loopBack: |
|
3985 | 642 |
[exBegin] |
643 |
||
644 |
|readerTask readingSocket writingSocket| |
|
645 |
||
646 |
readingSocket := self newTCPserverAtPort:9999. |
|
647 |
readerTask := |
|
648 |
[ |
|
649 |
|connection| |
|
650 |
||
651 |
readingSocket listenFor:1. |
|
652 |
connection := readingSocket accept. |
|
653 |
readingSocket close. |
|
654 |
[connection atEnd] whileFalse:[ |
|
655 |
Transcript showCR:(connection nextLine). |
|
656 |
]. |
|
657 |
connection close. |
|
658 |
] fork. |
|
659 |
||
660 |
Delay waitForSeconds:1. |
|
661 |
writingSocket := self newTCPclientToHost:'localhost' port:9999. |
|
662 |
writingSocket nextPutLine:'Hello'. |
|
663 |
writingSocket nextPutLine:'World'. |
|
664 |
writingSocket close. |
|
665 |
[exEnd] |
|
25 | 666 |
" |
667 |
! ! |
|
668 |
||
1462 | 669 |
!Socket class methodsFor:'instance creation'! |
126 | 670 |
|
3059 | 671 |
bindTo:aSocketAddress type:aTypeSymbol |
672 |
"create a socket for a specific type |
|
673 |
and bind it to aSocketAddress. |
|
674 |
Type must be: |
|
3217 | 675 |
#stream, #datagram or #raw |
3059 | 676 |
|
677 |
Neither connect nor connect-wait is done." |
|
678 |
||
679 |
|newSock| |
|
680 |
||
681 |
newSock := self domain:aSocketAddress domain type:aTypeSymbol. |
|
682 |
[ |
|
3217 | 683 |
newSock bindTo:aSocketAddress reuseAddress:true. |
3059 | 684 |
] ifCurtailed:[ |
3217 | 685 |
newSock close. |
3059 | 686 |
]. |
687 |
^ newSock |
|
688 |
||
689 |
||
690 |
" |
|
3217 | 691 |
Socket bindTo:(IPSocketAddress anyHost port:8081) type:#stream. |
692 |
Socket bindTo:(IPv6SocketAddress anyHost port:8081) type:#datagram. |
|
3059 | 693 |
" |
694 |
! |
|
695 |
||
126 | 696 |
domain:domainSymbol type:type |
697 |
"create a socket for domain and type - |
|
698 |
neither any connect nor binding is done. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
699 |
Domain must be one of the symbols: |
3217 | 700 |
#inet, #unix, #appletalk, #decnet, #xns, ...; |
819 | 701 |
Type must be: |
3217 | 702 |
#stream, #datagram or #raw |
3059 | 703 |
|
704 |
XXX: currently only the #AF_INET and #AF_INET6 and #AF_UNIX domains are supported" |
|
126 | 705 |
|
706 |
^ self new domain:domainSymbol type:type |
|
707 |
||
708 |
" |
|
3059 | 709 |
Socket domain:#AF_INET type:#stream |
710 |
Socket domain:#AF_INET type:#datagram |
|
711 |
Socket domain:#AF_INET6 type:#stream |
|
712 |
Socket domain:#AF_INET6 type:#datagram |
|
713 |
Socket domain:#AF_UNIX type:#stream |
|
126 | 714 |
" |
715 |
! |
|
716 |
||
717 |
newTCP |
|
718 |
"create a TCP socket - no binding or other setup is done, |
|
719 |
neither connect nor connect-wait is done." |
|
720 |
||
3059 | 721 |
^ self new domain:#'AF_INET' type:#stream |
722 |
||
723 |
" |
|
724 |
Socket newTCP |
|
725 |
" |
|
126 | 726 |
! |
727 |
||
728 |
newTCP:aServiceOrNil |
|
729 |
"create a TCP socket for a service - |
|
730 |
neither connect nor connect-wait is done." |
|
731 |
||
3059 | 732 |
|newSock socketAddress socketAddressClass| |
126 | 733 |
|
734 |
newSock := self newTCP. |
|
735 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
3217 | 736 |
[ |
737 |
socketAddressClass := newSock socketAddressClass. |
|
738 |
socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream. |
|
739 |
socketAddress hostAddress:socketAddressClass anyAddress. |
|
740 |
newSock bindTo:socketAddress reuseAddress:true. |
|
741 |
] ifCurtailed:[ |
|
742 |
newSock close. |
|
743 |
] |
|
126 | 744 |
]. |
745 |
^ newSock |
|
746 |
||
747 |
||
1911 | 748 |
" |
3217 | 749 |
Socket newTCP:'http-alt'. |
750 |
Socket newTCP:9996. |
|
1911 | 751 |
" |
126 | 752 |
! |
753 |
||
576 | 754 |
newTCPclientToAddress:aHostAddress port:aService |
755 |
"create a new TCP client socket connecting to a service. |
|
756 |
Return a socket instance if ok, nil on failure. |
|
757 |
Block until a connection is established (but only the current thread; |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
758 |
not the whole smalltalk). |
576 | 759 |
See also: #newTCPclientToAddress:port:withTimeout:" |
760 |
||
761 |
^ self newTCPclientToAddress:aHostAddress port:aService withTimeout:nil |
|
762 |
! |
|
763 |
||
3059 | 764 |
newTCPclientToAddress:aSocketAddressOrByteArray port:aService withTimeout:millis |
576 | 765 |
"create a new TCP client socket connecting to a service. |
766 |
Return a socket instance if ok, nil on failure. |
|
767 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
811 | 768 |
and return nil." |
576 | 769 |
|
3059 | 770 |
|port socketAddress| |
771 |
||
772 |
aService isString ifTrue:[ |
|
3217 | 773 |
port := self portOfService:aService protocol:#tcp. |
3059 | 774 |
] ifFalse:[ |
3217 | 775 |
port := aService. |
3059 | 776 |
]. |
3217 | 777 |
socketAddress := aSocketAddressOrByteArray isSocketAddress |
778 |
ifTrue:[aSocketAddressOrByteArray] |
|
779 |
ifFalse:[ |
|
780 |
"Passing ByteArrays is obsolete and only supported for IPv4" |
|
781 |
IPSocketAddress hostAddress:aSocketAddressOrByteArray |
|
782 |
]. |
|
3059 | 783 |
port notNil ifTrue:[ |
3217 | 784 |
socketAddress port:port. |
3059 | 785 |
]. |
786 |
^ self newTCPclientToAddress:socketAddress withTimeout:millis |
|
576 | 787 |
! |
788 |
||
3059 | 789 |
newTCPclientToAddress:aSocketAddress withTimeout:millis |
126 | 790 |
"create a new TCP client socket connecting to a service. |
791 |
Return a socket instance if ok, nil on failure. |
|
3059 | 792 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
793 |
and return nil." |
|
794 |
||
795 |
|socket| |
|
796 |
||
797 |
socket := self domain:aSocketAddress domain type:#stream. |
|
798 |
(socket connectTo:aSocketAddress withTimeout:millis) ifFalse:[ |
|
3217 | 799 |
socket close. |
800 |
^ nil |
|
3059 | 801 |
]. |
802 |
^ socket. |
|
803 |
||
804 |
" |
|
3217 | 805 |
self newTCPclientToAddress:(IPv6SocketAddress hostName:'www.exept.de' port:80) withTimeout:nil |
3059 | 806 |
" |
807 |
! |
|
808 |
||
809 |
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName |
|
810 |
"create a new TCP client socket connecting to a service. |
|
3985 | 811 |
If hostNameOrAddress is a string, try all the resolved addresses regardless |
3988 | 812 |
whether for IPv4 or IPv6. |
3059 | 813 |
Return a socket instance if ok, nil on failure. |
126 | 814 |
Block until a connection is established (but only the current thread; |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
815 |
not the whole smalltalk). |
126 | 816 |
See also: #newTCPclientToHost:port:withTimeout:" |
817 |
||
3590 | 818 |
^ self |
3985 | 819 |
newTCPclientToHost:hostNameOrAddress |
820 |
port:aPortOrServiceName |
|
821 |
domain:nil |
|
822 |
withTimeout:nil |
|
126 | 823 |
|
824 |
" |
|
3059 | 825 |
Socket newTCPclientToHost:'www.exept.de' port:'http' |
126 | 826 |
" |
827 |
||
828 |
"Created: 31.10.1995 / 18:54:11 / cg" |
|
829 |
! |
|
830 |
||
3059 | 831 |
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil withTimeout:millis |
832 |
"create a new TCP client socket connecting to a service on hostNameOrAddress. |
|
3985 | 833 |
If hostNameOrAddress is a string, try all the resolved addresses. |
126 | 834 |
Return a socket instance if ok, nil on failure. |
3059 | 835 |
Set aDomainSymbolOrNil to #AF_INET of #AF_INET6 to connect via a defined protocol. |
836 |
Set aDomainSymbolOrNil to nil, to try all protocols. |
|
126 | 837 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
3059 | 838 |
and return nil." |
839 |
||
840 |
|socket addressInfoList| |
|
841 |
||
842 |
hostNameOrAddress isString ifFalse:[ |
|
3985 | 843 |
^ self newTCPclientToAddress:hostNameOrAddress port:aPortOrServiceName withTimeout:millis. |
126 | 844 |
]. |
3059 | 845 |
|
3217 | 846 |
addressInfoList := SocketAddress |
3985 | 847 |
getAddressInfo:hostNameOrAddress |
848 |
serviceName:aPortOrServiceName |
|
849 |
domain:aDomainSymbolOrNil |
|
850 |
type:#stream |
|
851 |
protocol:nil |
|
852 |
flags:0. |
|
3059 | 853 |
|
854 |
addressInfoList do:[:eachAddressInfo| |
|
3985 | 855 |
|domainSymbol lastDomainSymbol| |
856 |
||
857 |
domainSymbol := eachAddressInfo domain. |
|
858 |
domainSymbol ~~ lastDomainSymbol ifTrue:[ |
|
859 |
socket notNil ifTrue:[ |
|
860 |
socket close. |
|
861 |
]. |
|
862 |
socket := self new domain:domainSymbol type:#stream. |
|
863 |
lastDomainSymbol := domainSymbol. |
|
864 |
]. |
|
865 |
(socket connectTo:eachAddressInfo socketAddress withTimeout:millis) ifTrue:[ |
|
866 |
^ socket. |
|
867 |
]. |
|
3059 | 868 |
]. |
869 |
socket notNil ifTrue:[ |
|
3985 | 870 |
socket close. |
3059 | 871 |
]. |
872 |
^ nil. |
|
873 |
||
126 | 874 |
" |
3059 | 875 |
Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET' withTimeout:1000. |
876 |
Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET6' withTimeout:1000. |
|
877 |
Socket newTCPclientToHost:'www.exept.de' port:80 domain:nil withTimeout:1000. |
|
126 | 878 |
Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:1000 |
879 |
" |
|
604 | 880 |
|
881 |
"Modified: / 16.1.1998 / 09:47:06 / stefan" |
|
126 | 882 |
! |
883 |
||
3217 | 884 |
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName withTimeout:millis |
3059 | 885 |
"create a new TCP client socket connecting to a service on hostNameOrAddress. |
3985 | 886 |
If hostNameOrAddress is a string, try all the resolved addresses regardless |
3988 | 887 |
whether for IPv4 or IPv6. |
3059 | 888 |
Return a socket instance if ok, nil on failure. |
889 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
890 |
and return nil." |
|
3217 | 891 |
|
892 |
^ self |
|
3985 | 893 |
newTCPclientToHost:hostNameOrAddress |
894 |
port:aPortOrServiceName |
|
895 |
domain:self defaultIpDomainForConnect |
|
896 |
withTimeout:millis |
|
3059 | 897 |
! |
898 |
||
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
899 |
newTCPserverAtAnonymousPort |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
900 |
"create a new TCP server socket providing service on |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
901 |
a new anonymous port. The portNr is assigned by the OS." |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
902 |
|
811 | 903 |
^ self newTCPserverAtPort:0 |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
904 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
905 |
! |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
906 |
|
126 | 907 |
newTCPserverAtPort:aService |
908 |
"create a new TCP server socket providing service." |
|
909 |
||
1911 | 910 |
^ self newTCP:aService |
126 | 911 |
! |
912 |
||
913 |
newUDP |
|
914 |
"create a UDP socket - no binding or other setup is done, |
|
915 |
neither connect nor connect-wait is done." |
|
916 |
||
3059 | 917 |
^ self new domain:#'AF_INET' type:#datagram |
126 | 918 |
|
919 |
"Socket newUDP" |
|
920 |
! |
|
921 |
||
922 |
newUDP:aServiceOrNil |
|
923 |
"create a UDP socket for a service - |
|
924 |
neither connect nor connect-wait is done." |
|
925 |
||
3059 | 926 |
|newSock socketAddressClass socketAddress| |
126 | 927 |
|
928 |
newSock := self newUDP. |
|
929 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
3217 | 930 |
[ |
931 |
socketAddressClass := newSock socketAddressClass. |
|
932 |
socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream. |
|
933 |
socketAddress hostAddress:socketAddressClass anyAddress. |
|
934 |
newSock bindTo:socketAddress reuseAddress:true. |
|
935 |
] ifCurtailed:[ |
|
936 |
newSock close. |
|
937 |
] |
|
126 | 938 |
]. |
939 |
^ newSock |
|
940 |
||
1911 | 941 |
" |
3217 | 942 |
Socket newUDP:4444. |
943 |
Socket newUDP:'activesync'. |
|
1911 | 944 |
" |
126 | 945 |
! |
946 |
||
947 |
newUDPserverAtPort:aService |
|
948 |
"create a new UDP server socket providing service." |
|
949 |
||
1911 | 950 |
^ self newUDP:aService |
126 | 951 |
! |
952 |
||
953 |
newUNIX |
|
954 |
"create a UNIX domain socket - no binding or other setup is done, |
|
811 | 955 |
neither connect nor connect-wait is done. |
956 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
957 |
return nil." |
|
126 | 958 |
|
3059 | 959 |
^ self new domain:#'AF_UNIX' type:#stream |
126 | 960 |
|
961 |
" |
|
962 |
Socket newUNIX |
|
963 |
" |
|
964 |
! |
|
965 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
966 |
newUNIXclientTo:pathName |
126 | 967 |
"create a new UNIX client socket connecting to a pathname. |
968 |
Return a socket instance if ok, nil on failure. |
|
969 |
Block until a connection is established (but only the current thread; |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
970 |
not the whole smalltalk). |
811 | 971 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
972 |
return nil. |
|
126 | 973 |
See also: #newUNIXclientTo:withTimeout:" |
974 |
||
975 |
^ self newUNIXclientTo:pathName withTimeout:nil |
|
976 |
||
977 |
! |
|
978 |
||
979 |
newUNIXclientTo:pathName withTimeout:millis |
|
980 |
"create a new UNIX client socket connecting to a pathname. |
|
981 |
Return a socket instance if ok, nil on failure. |
|
982 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
811 | 983 |
and return nil. |
984 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
985 |
return nil." |
|
126 | 986 |
|
987 |
|newSock| |
|
988 |
||
989 |
newSock := self newUNIX. |
|
990 |
newSock notNil ifTrue:[ |
|
3217 | 991 |
(newSock connectTo:(UDSocketAddress name:pathName) withTimeout:millis) ifFalse:[ |
992 |
newSock close. |
|
993 |
^ nil |
|
994 |
] |
|
126 | 995 |
]. |
996 |
^ newSock |
|
997 |
||
998 |
" |
|
3217 | 999 |
Socket newUNIXclientTo:'/tmp/foo' |
126 | 1000 |
" |
1001 |
! |
|
1002 |
||
1003 |
newUNIXserverAt:pathName |
|
811 | 1004 |
"create a new UNIX server socket providing service at a pathname. |
1005 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
1006 |
return nil." |
|
126 | 1007 |
|
3059 | 1008 |
^ self bindTo:(UDSocketAddress name:pathName) type:#stream. |
126 | 1009 |
|
1010 |
" |
|
1011 |
|s s2| |
|
1012 |
||
3059 | 1013 |
'/tmp/foo' asFilename remove. |
126 | 1014 |
s := Socket newUNIXserverAt:'/tmp/foo'. |
1015 |
s listenFor:5. |
|
1016 |
s2 := s accept. |
|
1017 |
" |
|
1018 |
! ! |
|
1019 |
||
1462 | 1020 |
!Socket class methodsFor:'Compatibility-ST80'! |
1021 |
||
1022 |
family:domainSymbol type:typeSymbol |
|
1023 |
"create a socket for domain and type - ST80 simply uses a different name. |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1024 |
Domain must be one of the symbols: #inet, #unix, #appletalk or #ns; |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1025 |
Type must be #stream, #datagram or #raw." |
1462 | 1026 |
|
1027 |
^ self domain:domainSymbol type:typeSymbol |
|
1028 |
||
1029 |
" |
|
3059 | 1030 |
Socket family:#AF_INET type:#stream |
1031 |
Socket family:#AF_INET type:#datagram |
|
1032 |
Socket family:#AF_UNIX type:#stream |
|
1462 | 1033 |
" |
1034 |
! |
|
1035 |
||
1036 |
getHostname |
|
3462 | 1037 |
"return the computer's hostname string" |
1462 | 1038 |
|
1039 |
^ OperatingSystem getHostName |
|
1040 |
||
1041 |
"Created: / 27.2.1998 / 02:32:17 / cg" |
|
1042 |
! |
|
1043 |
||
1044 |
sockStream |
|
1045 |
"return the type code for stream sockets" |
|
1046 |
||
1047 |
^ #stream |
|
1048 |
! ! |
|
1049 |
||
1600 | 1050 |
!Socket class methodsFor:'Compatibility-Squeak'! |
1051 |
||
2900 | 1052 |
deadlineSecs:seconds |
1053 |
^ Timestamp now addSeconds:seconds |
|
1054 |
! |
|
1055 |
||
1600 | 1056 |
initializeNetwork |
1057 |
"/ intentionally left blank here |
|
1058 |
! |
|
1059 |
||
3485 | 1060 |
standardDeadline |
1061 |
"a standard timeout in seconds for connection setup; |
|
1062 |
not really used in ST/X code (but by some code ported from squeak)" |
|
1063 |
||
1064 |
^ 30 |
|
1065 |
! |
|
1066 |
||
2292 | 1067 |
standardTimeout |
3485 | 1068 |
"a standard timeout in seconds for transfers; |
1069 |
not really used in ST/X code (but by some code ported from squeak)" |
|
1070 |
||
2292 | 1071 |
^ 30 |
1072 |
! |
|
1073 |
||
1600 | 1074 |
wildcardPort |
1075 |
^ nil |
|
1076 |
! ! |
|
1077 |
||
1462 | 1078 |
!Socket class methodsFor:'Compatibility-VW'! |
1079 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1080 |
AF_INET |
1462 | 1081 |
^ #AF_INET |
1082 |
! |
|
1083 |
||
1084 |
SOCK_STREAM |
|
1085 |
^ #SOCK_STREAM |
|
1086 |
! ! |
|
1087 |
||
1088 |
!Socket class methodsFor:'Signal constants'! |
|
1089 |
||
1090 |
brokenConnectionSignal |
|
1091 |
"return the signal used to tell broken connections. |
|
1092 |
Since in unix, this is the same as the broken pipe signal, |
|
1093 |
return that one. |
|
1094 |
(for other Operatingsystems, this may change ..)" |
|
1095 |
||
1096 |
^ PipeStream brokenPipeSignal |
|
1097 |
! |
|
1098 |
||
1099 |
invalidArgumentsSignal |
|
1100 |
"dummy for compatibility" |
|
1101 |
||
1102 |
^ self errorSignal |
|
1103 |
! ! |
|
1104 |
||
1105 |
!Socket class methodsFor:'debugging'! |
|
1106 |
||
1107 |
debug:aBoolean |
|
1108 |
"turn on/off internal debugprints. |
|
1109 |
This method is for ST/X debugging only and |
|
1110 |
may be removed in later versions" |
|
1111 |
||
1112 |
%{ /* NOCONTEXT */ |
|
1113 |
||
1114 |
__debugging__ = (aBoolean == true); |
|
1115 |
%} |
|
1116 |
" |
|
1117 |
Socket debug:true |
|
1118 |
Socket debug:false |
|
1119 |
" |
|
1120 |
! ! |
|
1121 |
||
3059 | 1122 |
!Socket class methodsFor:'defaults'! |
1123 |
||
1124 |
defaultIpDomainForConnect |
|
1125 |
"answer the domain used to look up host names for connect: |
|
3217 | 1126 |
#AF_INET use only IPv4 |
1127 |
#AF_INET6 use only IPv6 |
|
1128 |
nil use both IPv4 and IPv6" |
|
3059 | 1129 |
|
1130 |
^ #AF_INET |
|
1131 |
! ! |
|
1132 |
||
1911 | 1133 |
!Socket class methodsFor:'obsolete'! |
1134 |
||
1135 |
connectTo:service on:host |
|
1136 |
<resource: #obsolete> |
|
1137 |
"standard & easy client setup: |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1138 |
create new client tcp socket, bind and connect; |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1139 |
return the socket. |
1911 | 1140 |
The thread blocks (interruptable), until the connection is established." |
1141 |
||
1142 |
^ self newTCPclientToHost:host port:(self portOfService:service). |
|
1143 |
||
1144 |
" |
|
1145 |
Socket connectTo:9995 on:'clam' |
|
1146 |
Socket connectTo:4711 on:'exept' |
|
1147 |
Socket connectTo:'finger' on:'clam' |
|
1148 |
Socket connectTo:'ftp' on:'exept' |
|
1149 |
Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER') |
|
1150 |
" |
|
1151 |
! |
|
1152 |
||
1153 |
networkLongOrderIsMSB |
|
1154 |
"return the well known fact, that network byte order is most significant byte first" |
|
1155 |
||
1156 |
<resource: #obsolete> |
|
1157 |
||
1158 |
^ true |
|
1159 |
! |
|
1160 |
||
1161 |
networkShortOrderIsMSB |
|
1162 |
"return the well known fact, that network byte order is most significant byte first" |
|
1163 |
||
1164 |
<resource: #obsolete> |
|
1165 |
||
1166 |
^ true |
|
1167 |
! |
|
1168 |
||
1169 |
provide:aService |
|
1170 |
"standard & easy server setup: |
|
1171 |
create a new TCP server socket providing a service." |
|
1172 |
||
1173 |
<resource:#obsolete> |
|
1174 |
||
1175 |
|newSock| |
|
1176 |
||
1177 |
self obsoleteMethodWarning:'use #newTCPserverAtPort: / #listen'. |
|
1178 |
||
1179 |
newSock := self newTCPserverAtPort:(self portOfService:aService). |
|
1180 |
newSock notNil ifTrue:[ |
|
1181 |
newSock listenFor:5. |
|
1182 |
]. |
|
1183 |
^ newSock |
|
1184 |
||
1185 |
" |
|
1186 |
Socket provide:9995 |
|
1187 |
(Socket provide:9996) accept |
|
1188 |
Socket provide:'nntp' |
|
1189 |
" |
|
1190 |
! ! |
|
1191 |
||
1192 |
!Socket class methodsFor:'obsolete host queries'! |
|
525 | 1193 |
|
1194 |
appletalkAddressOfHost:aHostName |
|
1911 | 1195 |
<resource: #obsolete> |
525 | 1196 |
"return the APPLETALK address for a hostname as a byteArray, |
819 | 1197 |
where the network bytes come first (no matter what the local byteorder is) |
1198 |
followed by the node byte. |
|
525 | 1199 |
If the host is unknown, return nil. |
812 | 1200 |
This is the reverse operation to #hostWithAppletalkAddress:. |
1201 |
WARNING: untested code - I have no appletalk to test this." |
|
525 | 1202 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1203 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1204 |
handle:[:ex | |
1343 | 1205 |
^ nil |
1206 |
] |
|
1207 |
do:[ |
|
1208 |
^ (AppletalkSocketAddress hostName:aHostName) address |
|
1209 |
] |
|
525 | 1210 |
! |
1211 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1212 |
hostWithAppletalkAddress:addrByteArray |
1911 | 1213 |
<resource: #obsolete> |
525 | 1214 |
"return the hostname for an APPLETALK address. |
1215 |
The address is supposed to be a byteArray consisting of 3 bytes, |
|
1216 |
the network bytes come first (no matter what the local byteorder is). |
|
1217 |
The last byte is the node number. |
|
1218 |
Nil is returned for an unknown host or if its not an appletalk host. |
|
812 | 1219 |
This is is the reverse operation to #appletalkAddressOfHost:. |
1220 |
WARNING: untested code - I have no appletalk to test this." |
|
525 | 1221 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1222 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1223 |
handle:[:ex | |
1343 | 1224 |
^ nil |
1225 |
] |
|
1226 |
do:[ |
|
1227 |
^ (AppletalkSocketAddress hostAddress:addrByteArray) hostName |
|
1228 |
] |
|
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1229 |
|
525 | 1230 |
" |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1231 |
Socket appletalkAddressOfHost:'yourAppleHere' |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1232 |
Socket hostWithAppletalkAddress:#[1 2 3] |
525 | 1233 |
" |
126 | 1234 |
! |
1235 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1236 |
hostWithIpAddress:addrByteArray |
1911 | 1237 |
<resource: #obsolete> |
126 | 1238 |
"return the hostname for an IP (internet-) address. |
1239 |
The address is supposed to be a byteArray consisting of 4 bytes, |
|
1240 |
the network bytes come first (no matter what the local byteorder is). |
|
525 | 1241 |
Nil is returned for an unknown host or if its not an internet host. |
126 | 1242 |
This is the reverse operation to #ipAddressOfHost:." |
1243 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1244 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1245 |
handle:[:ex | |
1343 | 1246 |
^ nil |
1247 |
] |
|
1248 |
do:[ |
|
1249 |
^ (IPSocketAddress hostAddress:addrByteArray) hostName |
|
1250 |
] |
|
126 | 1251 |
|
1252 |
" |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1253 |
Socket ipAddressOfHost:'clam' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1254 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1255 |
Socket ipAddressOfHost:'porty' |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1256 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty') |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1257 |
Socket hostWithIpAddress:#[1 2 3 4] |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1258 |
Socket hostWithIpAddress:#[127 0 0 1] |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1259 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'1.2.3.4') |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1260 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'www.altavista.com') |
126 | 1261 |
" |
1262 |
! |
|
1263 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1264 |
hostWithIpV6Address:addrByteArray |
1911 | 1265 |
<resource: #obsolete> |
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1266 |
"return the hostname for an IPv6 (internet-) address. |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1267 |
The address is supposed to be a byteArray consisting ??? bytes, |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1268 |
the network bytes come first (no matter what the local byteorder is). |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1269 |
Nil is returned for an unknown host or if its not an internet host. |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1270 |
This is the reverse operation to #ipV6AddressOfHost:." |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1271 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1272 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1273 |
handle:[:ex | |
1343 | 1274 |
^ nil |
1275 |
] |
|
1276 |
do:[ |
|
1277 |
^ (IPv6SocketAddress hostAddress:addrByteArray) hostName |
|
1278 |
] |
|
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1279 |
|
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1280 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1281 |
Socket ipV6AddressOfHost:'clam' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1282 |
Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1283 |
Socket ipV6AddressOfHost:'porty' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1284 |
Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1285 |
Socket hostWithIpV6Address:#[1 2 3 4 5 6 7 8 9 10 11 12 13 14] |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1286 |
Socket ipV6AddressOfHost:'www.exept.de' |
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1287 |
" |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1288 |
! |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1289 |
|
6 | 1290 |
ipAddressOfHost:aHostName |
1911 | 1291 |
<resource: #obsolete> |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1292 |
"return the IP (internet-) number for a hostname as a byteArray, |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1293 |
where the network bytes come first (no matter what the cpus byteOrder is). |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1294 |
If the host is unknown, return nil. |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1295 |
This is the reverse operation to #hostWithIpAddress:." |
6 | 1296 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1297 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1298 |
handle:[:ex | |
1343 | 1299 |
^ nil |
1300 |
] |
|
1301 |
do:[ |
|
1302 |
^ (IPSocketAddress hostName:aHostName) address |
|
1303 |
] |
|
21 | 1304 |
|
71 | 1305 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1306 |
Socket ipAddressOfHost:'localhost' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1307 |
Socket ipAddressOfHost:'exept' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1308 |
Socket ipAddressOfHost:'1.2.3.4' |
1274
f257a1b16b4d
use naemLookup from SocketAddress for ipv6
Claus Gittinger <cg@exept.de>
parents:
1263
diff
changeset
|
1309 |
Socket ipAddressOfHost:'193.15.16.17' |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1310 |
Socket ipAddressOfHost:'josef' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1311 |
Socket ipAddressOfHost:'styx.com' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1312 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1313 |
Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1') |
71 | 1314 |
" |
819 | 1315 |
! |
1316 |
||
1317 |
ipV6AddressOfHost:aHostName |
|
1911 | 1318 |
<resource: #obsolete> |
819 | 1319 |
"return the IPv6 (internet-) number for a hostname as a byteArray, |
1320 |
where the network bytes come first (no matter what the cpus byteOrder is). |
|
1321 |
If the host is unknown, return nil. |
|
1322 |
This is the reverse operation to #hostWithIpV6Address:." |
|
1323 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1324 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1325 |
handle:[:ex | |
1343 | 1326 |
^ nil |
1327 |
] |
|
1328 |
do:[ |
|
1329 |
^ (IPv6SocketAddress hostName:aHostName) address |
|
1330 |
] |
|
819 | 1331 |
|
1332 |
" |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1333 |
Socket ipV6AddressOfHost:'localhost' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1334 |
Socket ipV6AddressOfHost:'exept' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1335 |
Socket ipV6AddressOfHost:'exept.exept.de' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1336 |
Socket ipV6AddressOfHost:'www.google.de' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1337 |
Socket ipV6AddressOfHost:'1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1338 |
Socket ipV6AddressOfHost:'josef' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1339 |
Socket ipV6AddressOfHost:'styx.com' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1340 |
Socket hostWithIpV6Address:(Socket ipV6AddressOfHost:'localhost') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1341 |
Socket ipV6AddressOfHost:(Socket hostV6WithIpAddress:'127.0.0.1') |
819 | 1342 |
" |
531 | 1343 |
! ! |
1344 |
||
1345 |
!Socket class methodsFor:'queries'! |
|
1346 |
||
1260
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1347 |
peerFromDomain:domain name:peerName port:port |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1348 |
|addrClass| |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1349 |
|
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1350 |
addrClass := self socketAddressClassForDomain:domain. |
1263
23161346a162
no longer callBack into sockets getHostName/getAddress code.
Claus Gittinger <cg@exept.de>
parents:
1260
diff
changeset
|
1351 |
^ addrClass hostName:peerName serviceName:port type:nil |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1352 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1353 |
|
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1354 |
peerNameFromDomain:domain peer:peer |
1260
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1355 |
|addrClass| |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1356 |
|
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1357 |
addrClass := self socketAddressClassForDomain:domain. |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1358 |
^ addrClass peerNameFromPeer:peer |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1359 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1360 |
|
0 | 1361 |
portOfService:aNameOrNumber |
819 | 1362 |
"returns the port-number for a given IP-service |
0 | 1363 |
or nil if no such service exists; |
1364 |
- used to convert service names to portNumbers" |
|
1365 |
||
91 | 1366 |
^ self portOfService:aNameOrNumber protocol:nil |
1367 |
||
28 | 1368 |
" |
1369 |
Socket portOfService:'finger' |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1370 |
Socket portOfService:'nntp' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1371 |
Socket portOfService:'echo' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1372 |
Socket portOfService:'snmp' |
28 | 1373 |
" |
0 | 1374 |
! |
1375 |
||
91 | 1376 |
portOfService:aNameOrNumber protocol:aProtocol |
819 | 1377 |
"returns the port-number for a given IP-service |
91 | 1378 |
or nil if no such service exists; |
1379 |
- used to convert service names to portNumbers" |
|
1380 |
||
2909 | 1381 |
%{ /* UNLIMITEDSTACK(noWIN32) */ |
418 | 1382 |
#ifndef NO_SOCKET |
91 | 1383 |
struct servent *servent = NULL; |
1384 |
char *protocol; |
|
1385 |
int tryBoth = 0; |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1386 |
short portNo; |
91 | 1387 |
|
604 | 1388 |
if (__isSmallInteger(aNameOrNumber)) { |
2340 | 1389 |
RETURN ( aNameOrNumber ); |
92 | 1390 |
} |
1391 |
||
2331 | 1392 |
if (__isStringLike(aProtocol)) { |
2340 | 1393 |
protocol = (char *)__stringVal(aProtocol); |
604 | 1394 |
} else { |
2340 | 1395 |
protocol = "tcp"; |
1396 |
tryBoth = 1; |
|
91 | 1397 |
} |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1398 |
|
2331 | 1399 |
if (__isStringLike(aNameOrNumber)) { |
2340 | 1400 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), protocol); |
1401 |
if (servent != NULL) { |
|
1402 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
|
1403 |
} |
|
1404 |
if (tryBoth) { |
|
1405 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp"); |
|
1406 |
if (servent != NULL) { |
|
1407 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
|
1408 |
} |
|
1409 |
} |
|
1410 |
RETURN ( nil ); |
|
91 | 1411 |
} |
418 | 1412 |
#endif |
91 | 1413 |
RETURN ( nil ); |
1414 |
%} |
|
1415 |
" |
|
1416 |
Socket portOfService:'echo' protocol:'udp' |
|
1417 |
Socket portOfService:'echo' protocol:'tcp' |
|
1418 |
" |
|
1419 |
! |
|
1420 |
||
0 | 1421 |
protocolOfService:aNameOrNumber |
819 | 1422 |
"returns the protocol (as string) for a given IP-service |
28 | 1423 |
or nil if no such service exists." |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1424 |
|
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1425 |
%{ /* UNLIMITEDSTACK(noWIN32) */ |
418 | 1426 |
#ifndef NO_SOCKET |
0 | 1427 |
struct servent *servent = NULL; |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1428 |
short portNo; |
0 | 1429 |
|
58 | 1430 |
if (__isSmallInteger(aNameOrNumber)) { |
2340 | 1431 |
portNo = __intVal(aNameOrNumber); |
1432 |
servent = getservbyport(htons(portNo), "tcp") ; |
|
1433 |
if (servent == NULL) { |
|
1434 |
servent = getservbyport(htons(portNo), "udp") ; |
|
1435 |
if (servent == NULL) { |
|
1436 |
RETURN ( nil ); |
|
1437 |
} |
|
1438 |
} |
|
0 | 1439 |
} else { |
2340 | 1440 |
if (__isStringLike(aNameOrNumber)) { |
1441 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "tcp"); |
|
1442 |
if (servent == NULL) { |
|
1443 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp"); |
|
1444 |
if (servent == NULL) { |
|
1445 |
RETURN ( nil ); |
|
1446 |
} |
|
1447 |
} |
|
1448 |
} |
|
0 | 1449 |
} |
1450 |
if (servent) { |
|
2340 | 1451 |
RETURN ( __MKSTRING(servent->s_proto) ); |
0 | 1452 |
} |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1453 |
#endif /* !NO_SOCKET */ |
0 | 1454 |
RETURN ( nil ); |
1455 |
%} |
|
28 | 1456 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1457 |
Socket protocolOfService:'finger' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1458 |
Socket protocolOfService:'nntp' |
28 | 1459 |
Socket protocolOfService:'xxx' |
1460 |
Socket protocolOfService:79 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1461 |
Socket protocolOfService:'snmp' |
28 | 1462 |
" |
1463 |
! |
|
1464 |
||
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1465 |
socketAddressClassForDomain:domain |
1230
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1466 |
^ SocketAddress knownClassFromCode:domain |
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1467 |
|
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1468 |
" |
3059 | 1469 |
self socketAddressClassForDomain:#AF_INET |
1470 |
self socketAddressClassForDomain:#AF_UNIX |
|
1230
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1471 |
" |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1472 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1473 |
|
532 | 1474 |
supportedProtocolFamilies |
1475 |
"return a collection of supported protocol families. |
|
1476 |
This list specifies what the Socket class supports - |
|
1477 |
socket creation may still fail, if your system was built |
|
1478 |
without it." |
|
1479 |
||
1280
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
1480 |
^ OperatingSystem supportedProtocolFamilies |
532 | 1481 |
|
1482 |
" |
|
1483 |
Socket supportedProtocolFamilies |
|
1484 |
" |
|
559
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1485 |
! |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1486 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1487 |
typeOfProtocol:aProtocol |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1488 |
"given a protocols name (i.e. tcp, udp etc) return the connection type. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1489 |
This method needs more ... - or is there a way to get this from the system ?" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1490 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1491 |
(aProtocol = 'tcp') ifTrue:[^ #stream]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1492 |
(aProtocol = 'udp') ifTrue:[^ #datagram]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1493 |
(aProtocol = 'ip') ifTrue:[^ #raw]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1494 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1495 |
unix domain |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1496 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1497 |
(aProtocol = 'ud') ifTrue:[^ #stream]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1498 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1499 |
add x25 stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1500 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1501 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1502 |
add appletalk stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1503 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1504 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1505 |
add other stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1506 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1507 |
^ nil |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1508 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1509 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1510 |
Socket typeOfProtocol:'tcp' |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1511 |
Socket typeOfProtocol:'ucp' |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1512 |
Socket typeOfProtocol:(Socket protocolOfService:'nntp') |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1513 |
Socket typeOfProtocol:(Socket protocolOfService:'echo') |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1514 |
" |
28 | 1515 |
! ! |
1516 |
||
1202 | 1517 |
!Socket methodsFor:'Compatibility-Dolphin'! |
1110 | 1518 |
|
1519 |
setReceiveTimeout: milliseconds |
|
1520 |
self receiveTimeout:(milliseconds / 1000) |
|
1521 |
! |
|
1522 |
||
1523 |
setSendTimeout: milliseconds |
|
1524 |
self sendTimeout:(milliseconds / 1000) |
|
1525 |
! ! |
|
1526 |
||
1202 | 1527 |
!Socket methodsFor:'Compatibility-ST80'! |
28 | 1528 |
|
887 | 1529 |
acceptNonBlock |
1530 |
^ self accept |
|
1531 |
! |
|
1532 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1533 |
ioConnection |
887 | 1534 |
^ self |
1535 |
! |
|
1536 |
||
126 | 1537 |
notReadySignal |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1538 |
"ST-80 mimicry. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1539 |
for now - this is not yet raised" |
71 | 1540 |
|
126 | 1541 |
^ Signal new |
486 | 1542 |
! |
1543 |
||
576 | 1544 |
readAppendStream |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1545 |
"ST-80 mimicry. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1546 |
In ST-80, socket is not a stream, but referes to one. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1547 |
ST-80 code therefore uses 'Socket readWriteStream' to access |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1548 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1549 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1550 |
this method returns the receiver, for transparency" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1551 |
|
576 | 1552 |
^ self |
1553 |
! |
|
1554 |
||
486 | 1555 |
readStream |
1556 |
"ST-80 mimicry. |
|
1557 |
In ST-80, socket is not a stream, but referes to one. |
|
1558 |
ST-80 code therefore uses 'Socket readStream' to access |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1559 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1560 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1561 |
this method returns the receiver, for transparency" |
486 | 1562 |
|
1563 |
^ self |
|
1564 |
||
1565 |
"Created: 24.1.1997 / 23:52:57 / cg" |
|
1566 |
! |
|
1567 |
||
1568 |
writeStream |
|
1569 |
"ST-80 mimicry. |
|
1570 |
In ST-80, socket is not a stream, but referes to one. |
|
1571 |
ST-80 code therefore uses 'Socket writeStream' to access |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1572 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1573 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1574 |
this method returns the receiver, for transparency" |
486 | 1575 |
|
1576 |
^ self |
|
1577 |
||
1578 |
"Created: 24.1.1997 / 10:34:35 / cg" |
|
1579 |
"Modified: 24.1.1997 / 23:52:52 / cg" |
|
71 | 1580 |
! ! |
1581 |
||
1202 | 1582 |
!Socket methodsFor:'Compatibility-Squeak'! |
771 | 1583 |
|
1462 | 1584 |
address |
1585 |
^ self getSocketAddress |
|
1586 |
! |
|
1587 |
||
2308
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1588 |
connectToHostNamed:hostName port:portNr |
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1589 |
self connectTo:hostName port:portNr |
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1590 |
! |
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1591 |
|
1462 | 1592 |
dataAvailable |
1593 |
^ self canReadWithoutBlocking |
|
1594 |
! |
|
1595 |
||
1886 | 1596 |
destroy |
1597 |
self close |
|
1598 |
||
1599 |
"Created: / 04-06-2007 / 21:29:03 / cg" |
|
1600 |
! |
|
1601 |
||
1462 | 1602 |
listenOn:aPortNr |
1882 | 1603 |
self listenOn:aPortNr backlogSize:5 |
1604 |
||
1605 |
"Modified: / 31-05-2007 / 17:59:53 / cg" |
|
1606 |
! |
|
1607 |
||
1608 |
listenOn:aPortNr backlogSize:aNumber |
|
1462 | 1609 |
self bindTo:aPortNr address:nil. |
1935 | 1610 |
self listenFor:aNumber |
1882 | 1611 |
|
1612 |
"Created: / 31-05-2007 / 17:59:47 / cg" |
|
1462 | 1613 |
! |
1614 |
||
1885 | 1615 |
peerName |
1616 |
"return my peer (i.e. ipAddr + port); |
|
1617 |
May return nil if not yet setup completely." |
|
1618 |
||
1619 |
^ self getPeer |
|
1620 |
! |
|
1621 |
||
1622 |
sendData: aStringOrByteArray |
|
3155
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1623 |
"Send all of the data in the given array, even if it requires multiple calls to send it all. |
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1624 |
Return the number of bytes sent." |
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1625 |
|
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1626 |
^ self nextPutBytes:aStringOrByteArray size from:aStringOrByteArray startingAt:1. |
1600 | 1627 |
! |
1628 |
||
1462 | 1629 |
setOption: optionName value: optionValue |
2899 | 1630 |
^ self setSocketOption:(optionName asSymbol) argument:optionValue argument:nil |
1887 | 1631 |
|
1632 |
"Modified: / 04-06-2007 / 21:23:19 / cg" |
|
1600 | 1633 |
! |
1634 |
||
2904 | 1635 |
waitForConnectionUntil:aTimestamp |
1636 |
"return true if connected" |
|
1637 |
||
1638 |
self readWaitWithTimeoutMs: (aTimestamp millisecondDeltaFrom:Timestamp now). |
|
1639 |
^ self isConnected |
|
771 | 1640 |
! ! |
1641 |
||
1935 | 1642 |
!Socket methodsFor:'accepting connections'! |
1643 |
||
1644 |
accept |
|
1645 |
"create a new TCP socket from accepting on the receiver. |
|
1646 |
This method will suspend the current process if no connection is waiting. |
|
1647 |
For ST-80 compatibility" |
|
1648 |
||
3633 | 1649 |
^ self waitForNewConnectionWithTimeout:nil |
1935 | 1650 |
|
1651 |
" |
|
1652 |
|sock newSock| |
|
1653 |
||
1654 |
sock := Socket provide:8004. |
|
1655 |
sock listenFor:5. |
|
1656 |
newSock := sock accept. |
|
1657 |
" |
|
1658 |
! |
|
1659 |
||
1660 |
blockingAccept |
|
1661 |
"create a new TCP socket from accepting on the receiver. |
|
1662 |
This method will suspend the smalltalk image with all smalltalk processes if no connection is waiting. |
|
1663 |
For ST-80 compatibility" |
|
1664 |
||
1665 |
|newSock| |
|
1666 |
||
1667 |
newSock := self class new. |
|
1668 |
(newSock primAcceptOn:self blocking:true) ifFalse:[ |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1669 |
"should raise an error here" |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1670 |
^ nil |
1935 | 1671 |
]. |
1672 |
^ newSock |
|
1673 |
! ! |
|
1674 |
||
1675 |
!Socket methodsFor:'binding'! |
|
1676 |
||
1677 |
bindAnonymously |
|
1678 |
"bind to any address. A free port will be allocated. |
|
1679 |
Our own socket address will be determined after conection set up. |
|
1680 |
This is the default after the socket has been created" |
|
1681 |
||
3059 | 1682 |
^ self bindTo:nil reuseAddress:false |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1683 |
|
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1684 |
" |
1938 | 1685 |
self newTCP bindAnonymously; listenFor:1; yourself |
4206 | 1686 |
self newTCP bindAnonymously; port |
1938 | 1687 |
" |
1935 | 1688 |
! |
1689 |
||
3059 | 1690 |
bindAnonymouslyToAddress:aSocketAddress |
1935 | 1691 |
"bind to address addressString. |
1692 |
A free port will be allocated" |
|
1693 |
||
1694 |
^ self |
|
3217 | 1695 |
bindTo:0 |
1696 |
address:aSocketAddress |
|
1697 |
reuseAddress:false |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1698 |
|
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1699 |
" |
3059 | 1700 |
self newTCP bindAnonymouslyToAddress:IPSocketAddress localHost; listenFor:1; yourself |
1938 | 1701 |
" |
1935 | 1702 |
! |
1703 |
||
1704 |
bindTo:aSocketAddress |
|
1705 |
"ST80 compatible bind, expecting a socketAddress argument. |
|
1706 |
The socketAddress object (an instance of SocketAddress) |
|
1707 |
is supposed to respond to #portOrName and #address requests." |
|
1708 |
||
3059 | 1709 |
^ self bindTo:aSocketAddress reuseAddress:true |
1935 | 1710 |
! |
1711 |
||
1712 |
bindTo:portNrOrNameString address:addressString |
|
3059 | 1713 |
"Old interface: bind the socket to an address |
1714 |
- returns true if ok, false otherwise. |
|
1715 |
||
1716 |
The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain: |
|
3217 | 1717 |
Best use a SocketAddress |
1718 |
For backward compatibility: |
|
1719 |
AF_INET domain can also use (4byte) byteArray like internet numbers, |
|
1720 |
AF_UNIX domain cab use pathname strings." |
|
1935 | 1721 |
|
1722 |
^ self |
|
3217 | 1723 |
bindTo:portNrOrNameString |
1724 |
address:addressString |
|
1725 |
reuseAddress:true |
|
1935 | 1726 |
! |
1727 |
||
1728 |
bindTo:portNrOrNameOrNil address:hostOrPathNameOrSocketAddrOrNil reuseAddress:reuse |
|
3059 | 1729 |
"Old interface: bind the socket to an address |
1730 |
- returns true if ok, false otherwise. |
|
1731 |
||
1732 |
The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain: |
|
3217 | 1733 |
Best use a SocketAddress |
1734 |
For backward compatibility: |
|
1735 |
AF_INET domain can also use (4byte) byteArray like internet numbers, |
|
1736 |
AF_UNIX domain can use pathname strings. |
|
1935 | 1737 |
|
1738 |
The reuse boolean argument controls if the SO_REUSEADDR socket option |
|
1739 |
is to be set (to avoid the 'bind: address in use' error). |
|
1740 |
" |
|
1741 |
||
3059 | 1742 |
|socketAddress| |
1935 | 1743 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1744 |
handle isNil ifTrue:[ |
3217 | 1745 |
^ self errorNotOpen |
1935 | 1746 |
]. |
1747 |
||
1748 |
hostOrPathNameOrSocketAddrOrNil isNil ifTrue:[ |
|
3217 | 1749 |
socketAddress := self socketAddressClass anyHost. |
1935 | 1750 |
] ifFalse:[ |
3217 | 1751 |
(hostOrPathNameOrSocketAddrOrNil isSocketAddress) ifTrue:[ |
1752 |
socketAddress := hostOrPathNameOrSocketAddrOrNil. |
|
1753 |
] ifFalse:[ |
|
1754 |
"backward compatibility: support for byteArray and string arg" |
|
1755 |
hostOrPathNameOrSocketAddrOrNil isString ifTrue:[ |
|
1756 |
socketAddress := self socketAddressClass hostName:hostOrPathNameOrSocketAddrOrNil. |
|
1757 |
] ifFalse:[ |
|
1758 |
hostOrPathNameOrSocketAddrOrNil isByteCollection ifFalse:[ |
|
1759 |
^ self error:'bindTo: bad host (socketAddress) argument' |
|
1760 |
]. |
|
1761 |
socketAddress := self socketAddressClass hostAddress:hostOrPathNameOrSocketAddrOrNil. |
|
1762 |
]. |
|
1763 |
]. |
|
1935 | 1764 |
]. |
1765 |
portNrOrNameOrNil notNil ifTrue:[ |
|
3217 | 1766 |
socketAddress port:portNrOrNameOrNil. |
1935 | 1767 |
]. |
3059 | 1768 |
|
1769 |
^ self bindTo:socketAddress reuseAddress:reuse. |
|
1770 |
||
1771 |
" |
|
1772 |
(Socket domain:#'AF_INET' type:#stream) |
|
3217 | 1773 |
bindTo:2144 address:nil; yourself |
3059 | 1774 |
" |
1775 |
! |
|
1776 |
||
1777 |
bindTo:aSocketAddress reuseAddress:reuse |
|
1778 |
"Bind the socket to aSocketAddress - returns true if ok, false otherwise. |
|
1779 |
||
1780 |
The reuse boolean argument controls if the SO_REUSEADDR socket option |
|
1781 |
is to be set (to avoid the 'bind: address in use' error). |
|
1782 |
||
3137 | 1783 |
You can bind to nil to bind to anyHost and assign a random port." |
3059 | 1784 |
|
1785 |
|ok error socketAddress| |
|
1786 |
||
1787 |
handle isNil ifTrue:[ |
|
4203 | 1788 |
^ self errorNotOpen |
3059 | 1789 |
]. |
1790 |
socketAddress := aSocketAddress. |
|
1791 |
socketAddress isNil ifTrue:[ |
|
4203 | 1792 |
"ok, get a all zero socket address, so it is for anyHost |
1793 |
and the port will be assigned" |
|
1794 |
socketAddress := self socketAddressClass new. |
|
3513 | 1795 |
]. |
1796 |
domain == #'AF_INET6' ifTrue:[ |
|
4203 | 1797 |
"accept also IPv4 connections on IPv6 sockets (this is off by default for windows" |
1798 |
self setSocketOption:#'IPV6_V6ONLY' argument:false argument:nil. |
|
1935 | 1799 |
]. |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1800 |
ok := false. |
1935 | 1801 |
|
1802 |
%{ /* STACK: 100000 */ |
|
1803 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1804 |
OBJ fp = __INST(handle); |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1805 |
SOCKET sock; |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1806 |
union sockaddr_u sa; |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1807 |
int sockaddr_size; |
2909 | 1808 |
INT ret; |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1809 |
int sockAddrOffs; |
1935 | 1810 |
|
3059 | 1811 |
if (fp == nil) { |
4203 | 1812 |
goto getOutOfHere; |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1813 |
} |
3059 | 1814 |
|
1815 |
if (! __isBytes(socketAddress)) { |
|
4203 | 1816 |
error = __mkSmallInteger(-1); |
1817 |
goto getOutOfHere; |
|
1935 | 1818 |
} |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1819 |
/* get the socket-address */ |
3059 | 1820 |
if (__isNonNilObject(socketAddress)){ |
4203 | 1821 |
int nIndex; |
1822 |
OBJ cls = __qClass(socketAddress); |
|
1823 |
||
1824 |
sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1825 |
nIndex = __qSize(socketAddress) - OHDR_SIZE; |
|
1826 |
sockaddr_size = nIndex - sockAddrOffs; |
|
1827 |
if (sockaddr_size > sizeof(sa)) { |
|
1828 |
error=__mkSmallInteger(-2); |
|
1829 |
goto getOutOfHere; |
|
1830 |
} |
|
1831 |
memcpy(&sa, __byteArrayVal(socketAddress) + sockAddrOffs, sockaddr_size); |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1832 |
} |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1833 |
|
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1834 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
1935 | 1835 |
|
1836 |
# ifdef SO_REUSEADDR |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1837 |
if (reuse == true) { |
4203 | 1838 |
int on = 1; |
1839 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) { |
|
1840 |
DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n")); |
|
1841 |
} |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1842 |
} |
1935 | 1843 |
# endif /* SO_REUSEADDR */ |
1844 |
||
1845 |
# ifdef BIND_BLOCKS |
|
1846 |
# ifdef DO_WRAP_CALLS |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1847 |
do { |
4203 | 1848 |
__threadErrno = 0; |
1849 |
ret = STX_WSA_NOINT_CALL3("bind", bind, sock, &sa, sockaddr_size); |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1850 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3126 | 1851 |
if (ret < 0) { |
4203 | 1852 |
errno = __threadErrno; |
3126 | 1853 |
} |
1935 | 1854 |
# else |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1855 |
__BEGIN_INTERRUPTABLE__ |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1856 |
do { |
4203 | 1857 |
ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size); |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1858 |
} while ((ret < 0) && (errno == EINTR)); |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1859 |
__END_INTERRUPTABLE__ |
1935 | 1860 |
# endif |
1861 |
# else |
|
3126 | 1862 |
errno = 0; |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1863 |
ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size); |
1935 | 1864 |
# endif |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1865 |
if (ret < 0) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
1866 |
# ifdef __win32__ |
4203 | 1867 |
if (errno == 0) { |
1868 |
errno = WSAGetLastError(); |
|
1869 |
} |
|
3126 | 1870 |
# endif |
4203 | 1871 |
DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno)); |
1872 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1873 |
goto getOutOfHere; |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1874 |
} else { |
4203 | 1875 |
ok = true; |
1935 | 1876 |
} |
1877 |
#endif /* NO_SOCKET */ |
|
1878 |
||
1879 |
getOutOfHere: ; |
|
1880 |
%}. |
|
4234 | 1881 |
ok ifFalse:[ |
4203 | 1882 |
|errorHolder errorString| |
1883 |
||
1884 |
error isInteger ifTrue:[ |
|
1885 |
errorHolder := OperatingSystem errorHolderForNumber:error. |
|
1886 |
errorString := errorHolder errorString. |
|
1887 |
] ifFalse:[ |
|
1888 |
errorString := error. |
|
1889 |
]. |
|
1890 |
OpenError newException |
|
1891 |
errorString:('cannot bind socket to address: %1 (%2)' |
|
1892 |
bindWith:socketAddress |
|
1893 |
with:errorString); |
|
1894 |
errorCode:error; |
|
1895 |
osErrorHolder:errorHolder; |
|
1896 |
parameter:self; |
|
1897 |
raiseRequest. |
|
1898 |
"maybe someone catches the error and binds to some other port..." |
|
1899 |
^ true. |
|
1935 | 1900 |
]. |
1901 |
||
3059 | 1902 |
port := socketAddress port. |
1903 |
port == 0 ifTrue:[ |
|
4203 | 1904 |
"this is a bind to a random port, now we can get the real port" |
1905 |
port := self getFullSocketAddress port. |
|
3059 | 1906 |
]. |
1935 | 1907 |
^ true |
1908 |
||
1909 |
" |
|
3059 | 1910 |
(Socket domain:#'AF_INET' type:#stream) |
4203 | 1911 |
bindTo:(IPSocketAddress anyHost port:445) reuseAddress:false; |
1912 |
yourself. |
|
3059 | 1913 |
|
1914 |
(Socket domain:#'AF_INET' type:#stream) |
|
4203 | 1915 |
bindTo:139 reuseAddress:false; |
1916 |
yourself. |
|
3059 | 1917 |
|
1918 |
(Socket domain:#'AF_INET6' type:#stream) |
|
4203 | 1919 |
bindTo:nil reuseAddress:false; |
1920 |
yourself. |
|
3059 | 1921 |
|
1922 |
(Socket domain:#'AF_INET' type:#stream) |
|
4203 | 1923 |
bindTo:(IPSocketAddress localHost port:2122) reuseAddress:false; |
1924 |
yourself. |
|
3059 | 1925 |
|
1926 |
(Socket domain:#'AF_UNIX' type:#stream) |
|
4203 | 1927 |
bindTo:nil reuseAddress:false; |
1928 |
yourself. |
|
1935 | 1929 |
" |
1930 |
! |
|
1931 |
||
1932 |
listenFor:aNumber |
|
1933 |
"start listening; return true if ok, false on error |
|
1934 |
aNumber is the number of connect requests, that may be queued on the socket" |
|
1935 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1936 |
handle isNil ifTrue:[ |
3217 | 1937 |
^ self errorNotOpen |
1935 | 1938 |
]. |
1939 |
%{ |
|
1940 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1941 |
OBJ fp = __INST(handle); |
1935 | 1942 |
SOCKET sock; |
1943 |
int ret; |
|
1944 |
||
1945 |
if (! __isSmallInteger(aNumber)) { |
|
3217 | 1946 |
DBGPRINTF(("SOCKET: invalid arg\n")); |
1947 |
RETURN (false); |
|
1935 | 1948 |
} |
1949 |
||
1950 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1951 |
||
1952 |
#ifdef LISTEN_BLOCKS |
|
1953 |
# ifdef DO_WRAP_CALLS |
|
1954 |
do { |
|
3217 | 1955 |
__threadErrno = 0; |
1956 |
ret = STX_WSA_NOINT_CALL2("listen", listen, sock, __intVal(aNumber)); |
|
1935 | 1957 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3126 | 1958 |
if (ret < 0) { |
3217 | 1959 |
errno = __threadErrno; |
3126 | 1960 |
} |
1935 | 1961 |
# else |
1962 |
__BEGIN_INTERRUPTABLE__ |
|
1963 |
do { |
|
3217 | 1964 |
ret = listen(sock, __intVal(aNumber)); |
1935 | 1965 |
} while ((ret < 0) && (errno == EINTR)); |
1966 |
__END_INTERRUPTABLE__ |
|
1967 |
# endif |
|
1968 |
#else |
|
3126 | 1969 |
errno = 0; |
1935 | 1970 |
ret = listen(sock, __intVal(aNumber)); |
1971 |
#endif |
|
1972 |
||
1973 |
if (ret < 0) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
1974 |
# ifdef __win32__ |
3217 | 1975 |
if (errno == 0) { |
1976 |
errno = WSAGetLastError(); |
|
1977 |
} |
|
3126 | 1978 |
# endif |
3217 | 1979 |
DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno)); |
1980 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1981 |
RETURN (false); |
|
1935 | 1982 |
} |
1983 |
#else |
|
1984 |
RETURN (false); |
|
1985 |
#endif |
|
1986 |
%}. |
|
3157 | 1987 |
listening := aNumber. |
1935 | 1988 |
^ true |
1989 |
! ! |
|
1990 |
||
1576
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
1991 |
!Socket methodsFor:'closing'! |
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
1992 |
|
4172 | 1993 |
abortAndClose |
1994 |
"immediately abort the connection: |
|
1995 |
discard buffered data and close the stream" |
|
1996 |
||
1997 |
self linger:0. |
|
1998 |
self close. |
|
3224 | 1999 |
! |
2000 |
||
1885 | 2001 |
shutDown |
4172 | 2002 |
"shutDown (initiate a graceful close) |
2003 |
and close (free the filedescriptor) the socket. |
|
2004 |
The close will return immediately and buffered data will be sent in the |
|
2005 |
background, unless you set linger" |
|
1885 | 2006 |
|
2007 |
self shutdown:2. |
|
2008 |
self close |
|
1576
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2009 |
! |
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2010 |
|
1885 | 2011 |
shutDownInput |
2012 |
"shutDown the input side of the socket. |
|
2013 |
Any read on the socket will signal end-of-file from now on. |
|
1839 | 2014 |
The other side MAY be informed, that no more data will be accepted |
1576
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2015 |
(e.g. setting the TCP-Windowsize to 0)" |
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2016 |
|
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2017 |
self shutdown:0. |
1885 | 2018 |
! |
2019 |
||
2020 |
shutDownOutput |
|
2021 |
"shutDown the output side of the socket. |
|
2022 |
Any write to the socket will signal end-of-file from now on. |
|
4172 | 2023 |
An orderly release (TCP FIN) will be initiated after the last buffered data |
2024 |
has been sent, so the other side will get a end-of-file condition eventually. |
|
2025 |
If you set linger > 0, the operation will wait until buffered data |
|
2026 |
has been delivered to the peer. |
|
2027 |
Otherwise the operation returns immediately." |
|
1343 | 2028 |
|
1884 | 2029 |
self shutdown:1. |
1885 | 2030 |
! ! |
2031 |
||
1935 | 2032 |
!Socket methodsFor:'connecting'! |
2033 |
||
3059 | 2034 |
connectTo:aSocketAddress |
2035 |
^ self connectTo:aSocketAddress withTimeout:nil |
|
2036 |
! |
|
2037 |
||
1935 | 2038 |
connectTo:hostOrPathName port:portNrOrName |
3655 | 2039 |
<resource: #obsolete> |
3059 | 2040 |
"Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr. |
2041 |
For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray, |
|
2042 |
but it is recommended to pass SocketAddress instances. |
|
1935 | 2043 |
|
2044 |
Return true if ok, false otherwise. |
|
2045 |
The current process will block (but not the whole Smalltalk) until the connection is established. |
|
2046 |
See also: #connectTo:port:withTimeout: for a somewhat nicer interface." |
|
2047 |
||
2048 |
^ self connectTo:hostOrPathName port:portNrOrName withTimeout:nil |
|
2049 |
! |
|
2050 |
||
2051 |
connectTo:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil withTimeout:timeout |
|
3655 | 2052 |
<resource: #obsolete> |
3059 | 2053 |
"Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr. |
2054 |
For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray, |
|
2055 |
but it is recommended to pass SocketAddress instances. |
|
1935 | 2056 |
|
2057 |
Return true if ok, false otherwise. |
|
2058 |
The current process will block (but not the whole Smalltalk) until the connection is established, |
|
4038 | 2059 |
or timeout milliseconds have passed." |
1935 | 2060 |
|
3059 | 2061 |
|domainClass socketAddress| |
2062 |
||
2063 |
(hostOrPathNameOrSocketAddr isSocketAddress) ifTrue:[ |
|
4038 | 2064 |
socketAddress := hostOrPathNameOrSocketAddr. |
2065 |
portNrOrNameOrNil notNil ifTrue:[ |
|
2066 |
socketAddress port:portNrOrNameOrNil. |
|
2067 |
]. |
|
3059 | 2068 |
] ifFalse:[ |
4038 | 2069 |
"backward compatibility: support for byteArray and string arg" |
2070 |
domainClass := self class socketAddressClassForDomain:domain. |
|
2071 |
domainClass isNil ifTrue:[ |
|
2072 |
^ self error:'invalid (unsupported) domain'. |
|
2073 |
]. |
|
2074 |
||
2075 |
hostOrPathNameOrSocketAddr isString ifTrue:[ |
|
2076 |
socketAddress := domainClass hostName:hostOrPathNameOrSocketAddr serviceName:portNrOrNameOrNil type:#SOCK_STREAM. |
|
2077 |
peerName := hostOrPathNameOrSocketAddr. |
|
2078 |
] ifFalse:[ |
|
2079 |
hostOrPathNameOrSocketAddr isByteCollection ifFalse:[ |
|
2080 |
^ self error:'connectTo: bad host (socketAddress) argument' |
|
2081 |
]. |
|
2082 |
socketAddress := domainClass hostAddress:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil. |
|
2083 |
]. |
|
3059 | 2084 |
]. |
2085 |
||
2086 |
^ self connectTo:socketAddress withTimeout:timeout. |
|
2087 |
||
2088 |
" |
|
2089 |
|sock| |
|
2090 |
sock := Socket newTCP. |
|
2091 |
sock connectTo:'localhost' port:21 withTimeout:1000. |
|
2092 |
sock |
|
2093 |
||
2094 |
|sock| |
|
2095 |
sock := Socket newTCP. |
|
2096 |
sock connectTo:'localhost' port:9876 withTimeout:2000. |
|
2097 |
sock |
|
2098 |
" |
|
2099 |
! |
|
2100 |
||
2101 |
connectTo:aSocketAddress withTimeout:timeout |
|
2102 |
"Connect to a SocketAddress. |
|
2103 |
||
2104 |
Return true if ok, false otherwise. |
|
2105 |
The current process will block (but not the whole Smalltalk) until the connection is established, |
|
4038 | 2106 |
or timeout milliseconds have passed." |
3059 | 2107 |
|
2108 |
|isAsync err| |
|
1935 | 2109 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2110 |
handle isNil ifTrue:[ |
4032 | 2111 |
^ self errorNotOpen |
1935 | 2112 |
]. |
4234 | 2113 |
isAsync := false. |
1935 | 2114 |
|
2115 |
%{ /* STACK: 100000 */ |
|
2116 |
#ifndef NO_SOCKET |
|
3123 | 2117 |
//#define DUMP_ADDRESS 1 |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2118 |
OBJ fp = __INST(handle); |
1935 | 2119 |
union sockaddr_u sa; |
2120 |
SOCKET sock; |
|
2121 |
int a; |
|
2909 | 2122 |
INT ret; |
2123 |
int oldFlags; |
|
3741 | 2124 |
# ifdef __MINGW64__ |
2125 |
unsigned long on = 1; |
|
2126 |
# else |
|
1935 | 2127 |
int on = 1; |
3741 | 2128 |
#endif |
1935 | 2129 |
int sockaddr_size; |
2130 |
||
3059 | 2131 |
if (!__isNonNilObject(aSocketAddress) || !__isBytes(aSocketAddress)) { |
4032 | 2132 |
DBGPRINTF(("SOCKET: invalid socketAddress\n")); |
2133 |
err = @symbol(argumentError); |
|
2134 |
goto out; |
|
1935 | 2135 |
} |
2136 |
||
2137 |
{ |
|
4032 | 2138 |
int sockAddrOffs = 0; |
2139 |
int nIndex =__byteArraySize(aSocketAddress); |
|
2140 |
OBJ cls = __qClass(aSocketAddress); |
|
2141 |
||
2142 |
//if (cls != @global(ByteArray)) |
|
2143 |
// sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2144 |
sockaddr_size = nIndex - sockAddrOffs; |
|
2145 |
if (sockaddr_size > sizeof(sa)) { |
|
2146 |
DBGPRINTF(("SOCKET: invalid (short) socketAddress\n")); |
|
2147 |
err = @symbol(argumentError); |
|
2148 |
goto out; |
|
2149 |
} |
|
2150 |
memcpy(&sa, __byteArrayVal(aSocketAddress) + sockAddrOffs, sockaddr_size); |
|
1935 | 2151 |
} |
2152 |
||
2153 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2154 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2155 |
#ifdef __win32__ |
3657 | 2156 |
ioctlsocket(sock, FIONBIO, &on); |
2157 |
#elif defined(O_NONBLOCK) |
|
1935 | 2158 |
/* |
2159 |
* set to non-blocking and wait later |
|
2160 |
*/ |
|
2161 |
oldFlags = fcntl(sock, F_GETFL, 0); |
|
2162 |
/* on SUNOS4.x, would use fcntl(osfd, F_SETFL, flags | FNDELAY); */ |
|
2163 |
fcntl(sock, F_SETFL, oldFlags | O_NONBLOCK); |
|
2164 |
# endif |
|
2165 |
||
2166 |
/* |
|
2167 |
* connect |
|
2168 |
*/ |
|
3123 | 2169 |
|
4039 | 2170 |
// we do not use wrap calls any longer, because they have problems with aborting. |
3657 | 2171 |
// we use nonblocking accept() instead. |
2172 |
# if 0 && defined(DO_WRAP_CALLS) |
|
3217 | 2173 |
// __setWrapCallDebugging(1,1); |
2414 | 2174 |
|
3123 | 2175 |
|
1935 | 2176 |
do { |
4032 | 2177 |
DBGFPRINTF((stderr, "SOCKET: (sock=%d) connect...\n", sock)); |
2178 |
ret = STX_WSA_NOINT_CALL3("connect", connect, sock, &sa, (INT)sockaddr_size); |
|
2179 |
DBGFPRINTF((stderr, "SOCKET: connect(%d) -> %"_ld_" (%d)\n", sock, (INT)ret, __threadErrno)); |
|
1935 | 2180 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3123 | 2181 |
|
3077 | 2182 |
if (ret < 0) { |
4032 | 2183 |
int optLen = sizeof(errno); |
2184 |
errno = __threadErrno; |
|
3123 | 2185 |
#if 0 |
4032 | 2186 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errno, &optLen) == SOCKET_ERROR) { |
2187 |
DBGFPRINTF((stderr, "SOCKET: getsockopt(SO_ERROR) failed: %d\n", WSAGetLastError())); |
|
2188 |
} |
|
3657 | 2189 |
#endif // 0 |
3077 | 2190 |
} |
2191 |
||
2192 |
// __setWrapCallDebugging(1,0); |
|
3657 | 2193 |
# else // !DO_WRAP_CALLS |
2194 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2195 |
# if !defined(__win32__) && !defined(O_NONBLOCK) |
1935 | 2196 |
__BEGIN_INTERRUPTABLE__ |
3657 | 2197 |
# endif |
1935 | 2198 |
do { |
4032 | 2199 |
ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size); |
1935 | 2200 |
} while ((ret < 0) |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2201 |
# ifdef __win32__ |
4032 | 2202 |
&& (errno = WSAGetLastError()) |
3657 | 2203 |
# endif |
4032 | 2204 |
&& ((errno == EINTR) |
3657 | 2205 |
# ifdef EAGAIN |
4032 | 2206 |
|| (errno == EAGAIN) |
3657 | 2207 |
# endif |
4032 | 2208 |
)); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2209 |
# if !defined(__win32__) && !defined(O_NONBLOCK) |
1935 | 2210 |
__END_INTERRUPTABLE__ |
3657 | 2211 |
# endif |
2212 |
#endif |
|
2213 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2214 |
#if defined(__win32__) && !defined(EINPROGRESS) |
3657 | 2215 |
# define EINPROGRESSS WSAEINPROGRESSS |
2216 |
# define EALREADY WSAEALREADY |
|
1935 | 2217 |
#endif |
2218 |
||
2219 |
if (ret < 0) { |
|
2220 |
# if defined(EINPROGRESS) || defined(EALREADY) |
|
4032 | 2221 |
if (0 |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2222 |
# ifdef __win32__ |
4032 | 2223 |
|| (errno == WSAEWOULDBLOCK) |
3657 | 2224 |
# endif |
1935 | 2225 |
# ifdef EINPROGRESS |
4032 | 2226 |
|| (errno == EINPROGRESS) |
1935 | 2227 |
# endif |
2228 |
# ifdef EALREADY |
|
4032 | 2229 |
|| (errno == EALREADY) |
1935 | 2230 |
# endif |
4032 | 2231 |
) { |
2232 |
/* |
|
2233 |
* This was a nonblocking operation that will take some time. |
|
2234 |
* Do a select on read to get informed when the operation is ready. |
|
2235 |
*/ |
|
2236 |
DBGFPRINTF((stderr, "SOCKET: isAsync is true\n")); |
|
2237 |
isAsync = true; |
|
2238 |
} else |
|
1935 | 2239 |
# endif /* EINPROGRESS or EALREADY */ |
4032 | 2240 |
{ |
2241 |
DBGFPRINTF((stderr, "SOCKET: connect failed ret=%"_ld_" errno=%d __threadErrno=%d\n", |
|
2242 |
(INT)ret, errno, __threadErrno )); |
|
1935 | 2243 |
# ifdef DUMP_ADDRESS |
4032 | 2244 |
{ |
2245 |
unsigned char *cp = (unsigned char *)(&sa); |
|
2246 |
int i; |
|
2247 |
||
2248 |
console_printf("address data:\n"); |
|
2249 |
for (i=0; i<sockaddr_size; i++) { |
|
2250 |
console_printf(" %02x\n", *cp++); |
|
2251 |
} |
|
2252 |
} |
|
1935 | 2253 |
# endif |
4032 | 2254 |
err = __MKSMALLINT(errno); |
2255 |
} |
|
1935 | 2256 |
} |
2257 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2258 |
# ifdef __win32__ |
3657 | 2259 |
{ |
4032 | 2260 |
int off = 0; |
2261 |
ioctlsocket(sock, FIONBIO, &off); |
|
3657 | 2262 |
} |
2263 |
# elif defined(O_NONBLOCK) // Linux / Unix |
|
1935 | 2264 |
fcntl(sock, F_SETFL, oldFlags); |
2265 |
# endif |
|
2266 |
||
2267 |
# else /* NO_SOCKET */ |
|
3077 | 2268 |
err = @symbol(notSupported); |
1935 | 2269 |
# endif /* NO_SOCKET */ |
3077 | 2270 |
out:; |
1935 | 2271 |
%}. |
3217 | 2272 |
|
3077 | 2273 |
err notNil ifTrue:[ |
4032 | 2274 |
err isSymbol ifTrue:[ |
2275 |
self primitiveFailed:err. |
|
2276 |
]. |
|
2277 |
lastErrorNumber := err. |
|
2278 |
^ false. |
|
2279 |
"/ Once we will raise an exception instead of returning false (and have to change some code above): |
|
3077 | 2280 |
"/ (OperatingSystem errorHolderForNumber:err) reportError. |
2281 |
]. |
|
4234 | 2282 |
isAsync ifTrue:[ |
4032 | 2283 |
(self writeExceptionWaitWithTimeoutMs:timeout) ifTrue:[ |
2284 |
"/ a timeout occurred |
|
2285 |
"/ should I cancel the connect? |
|
2286 |
lastErrorNumber := OperatingSystem errorNumberFor:#ETIMEDOUT. |
|
2287 |
^ false. |
|
2288 |
]. |
|
2289 |
err := self getSocketError. |
|
2290 |
err ~~ 0 ifTrue:[ |
|
2291 |
lastErrorNumber := err. |
|
2292 |
^ false. |
|
2293 |
]. |
|
1935 | 2294 |
]. |
2295 |
||
1940
2ea7d2c1a2e6
changed #connectTo:port:withTimeout: - add commented code tah may be used in the future
Stefan Vogel <sv@exept.de>
parents:
1939
diff
changeset
|
2296 |
|
3059 | 2297 |
peer := aSocketAddress. |
1935 | 2298 |
port isNil ifTrue:[ |
4032 | 2299 |
"socket has not been explicitly bound, |
2300 |
after connect it has been bound implicitly - fetch the port" |
|
2301 |
port := self getFullSocketAddress port. |
|
1935 | 2302 |
]. |
2303 |
^ true |
|
2304 |
||
2305 |
" |
|
2306 |
|sock| |
|
2307 |
sock := Socket newTCP. |
|
3059 | 2308 |
sock connectTo:(IPSocketAddress localHost port:21) withTimeout:1000. |
1935 | 2309 |
sock |
2310 |
||
2311 |
|sock| |
|
2312 |
sock := Socket newTCP. |
|
3059 | 2313 |
sock connectTo:(IPSocketAddress localHost port:9876) withTimeout:2000. |
1935 | 2314 |
sock |
2315 |
" |
|
2316 |
! ! |
|
2317 |
||
1885 | 2318 |
!Socket methodsFor:'datagram transmission'! |
2319 |
||
2320 |
receiveBuffer:aDataBuffer start:startIndex for:nBytes |
|
2321 |
"receive data |
|
2322 |
Return the number of bytes received, or a negative number on error. |
|
2323 |
On error, the unix error code is left in the lastErrorNumber |
|
2324 |
instance variable. |
|
2325 |
The thread blocks until data arrives - you may want to wait before |
|
2326 |
receiving, using #readWait or #readWaitWithTimeout:." |
|
2327 |
||
3126 | 2328 |
|nReceived error| |
1885 | 2329 |
|
2330 |
%{ |
|
2331 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2332 |
OBJ fp = __INST(handle); |
1885 | 2333 |
|
2334 |
if (fp != nil) { |
|
3657 | 2335 |
SOCKET sock; |
2336 |
INT objSize, offs; |
|
2337 |
INT n; |
|
2338 |
char *extPtr; |
|
2339 |
unsigned char *buffer; |
|
2340 |
unsigned char *allocatedBuffer = NULL; |
|
2341 |
INT flags = 0; |
|
2342 |
||
2343 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2344 |
||
2345 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2346 |
if (__isSmallInteger(nBytes)) { |
|
2347 |
if (__intVal(nBytes) < objSize) { |
|
2348 |
objSize = __intVal(nBytes); |
|
2349 |
} |
|
2350 |
} |
|
1885 | 2351 |
|
2352 |
# ifdef DO_WRAP_CALLS |
|
3657 | 2353 |
if (extPtr) { |
2354 |
buffer = extPtr + offs; |
|
2355 |
} else { |
|
2356 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
2357 |
} |
|
2358 |
||
2359 |
do { |
|
2360 |
__threadErrno = 0; |
|
2361 |
n = (INT)STX_WSA_NOINT_CALL4("recv", recv, sock, buffer, objSize, flags); |
|
2362 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2363 |
if (n < 0) { |
|
2364 |
errno = __threadErrno; |
|
2365 |
} |
|
2366 |
||
2367 |
if (allocatedBuffer) { |
|
2368 |
if (n > 0) { |
|
2369 |
bcopy(allocatedBuffer, (char *)__InstPtr(aDataBuffer) + offs, n); |
|
2370 |
} |
|
2371 |
free(allocatedBuffer); |
|
2372 |
} |
|
1885 | 2373 |
# else |
3657 | 2374 |
__BEGIN_INTERRUPTABLE__ |
2375 |
do { |
|
2376 |
if (extPtr) { |
|
2377 |
n = recv(sock, extPtr + offs, objSize, flags); |
|
2378 |
} else { |
|
2379 |
n = recv(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, flags); |
|
2380 |
} |
|
2381 |
} while ((n < 0) && (errno == EINTR)); |
|
2382 |
__END_INTERRUPTABLE__ |
|
1885 | 2383 |
# endif |
2384 |
||
3657 | 2385 |
if (n < 0) { |
2386 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2387 |
} else { |
|
2388 |
RETURN(__MKSMALLINT(n)); |
|
2389 |
} |
|
1885 | 2390 |
} |
2391 |
#endif |
|
2392 |
bad: ; |
|
2393 |
%}. |
|
3126 | 2394 |
error notNil ifTrue:[ |
3657 | 2395 |
^ self readError:error. |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2396 |
]. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2397 |
" |
1282 | 2398 |
arrive here if you try to receive into an invalid buffer (i.e. not ByteArray-like) |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2399 |
" |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2400 |
self primitiveFailed |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2401 |
! |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2402 |
|
1885 | 2403 |
receiveFrom:anAddressBuffer buffer:aDataBuffer |
2404 |
"receive datagramm data - put address of originating host into |
|
2405 |
anAddressBuffer, data into aBuffer. |
|
2406 |
Both must be ByteArray-like. The addressBuffer must |
|
2407 |
provide space for a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2408 |
Return the number of bytes received, or a negative number on error. |
|
2409 |
On error, the unix error code is left in the lastErrorNumber |
|
126 | 2410 |
instance variable." |
28 | 2411 |
|
3615 | 2412 |
^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:1 for:(aDataBuffer size) flags:0 |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
2413 |
! |
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
2414 |
|
1885 | 2415 |
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes |
3615 | 2416 |
^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:0 |
2417 |
! |
|
2418 |
||
2419 |
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:flags |
|
1885 | 2420 |
"receive datagramm data |
2421 |
- put address of originating host into anAddressBuffer, data into aBuffer. |
|
2422 |
For backward compatibility, the addressBuffer may be a non-SocketAddress; |
|
2423 |
then, it must be a byteArray with appropriate size for the addressBytes. |
|
2424 |
||
2425 |
Return the number of bytes received, or a negative number on error. |
|
2426 |
On error, the unix error code is left in the lastErrorNumber |
|
2427 |
instance variable. |
|
2428 |
The thread blocks until data arrives - you may want to wait before |
|
2429 |
receiving, using #readWait or #readWaitWithTimeout:." |
|
2430 |
||
3126 | 2431 |
|domainClass addr addrLen nReceived error| |
1885 | 2432 |
|
2433 |
domainClass := self class socketAddressClassForDomain:domain. |
|
2434 |
domainClass isNil ifTrue:[ |
|
4234 | 2435 |
^ self error:'invalid (unsupported) domain'. |
1885 | 2436 |
]. |
4234 | 2437 |
anAddressBuffer isSocketAddress ifTrue:[ |
2438 |
anAddressBuffer class == domainClass ifFalse:[ |
|
2439 |
^ self error:'addressBuffer class mismatch (domain)'. |
|
2440 |
]. |
|
2441 |
addr := anAddressBuffer. |
|
1885 | 2442 |
] ifFalse:[ |
4234 | 2443 |
anAddressBuffer notNil ifTrue:[ |
2444 |
addr := domainClass new. |
|
2445 |
]. |
|
1885 | 2446 |
]. |
2447 |
||
2448 |
%{ |
|
2449 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2450 |
OBJ fp = __INST(handle); |
1885 | 2451 |
|
2452 |
if (fp != nil) { |
|
4234 | 2453 |
SOCKET sock; |
2454 |
size_t objSize; |
|
2455 |
union sockaddr_u sa; |
|
2456 |
socklen_t alen = 0; |
|
2457 |
INT n, offs; |
|
2458 |
int _flags = __longIntVal(flags); |
|
2459 |
char *extPtr; |
|
2460 |
unsigned char *allocatedBuffer = NULL, *buffer = NULL; |
|
2461 |
||
2462 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2463 |
||
2464 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2465 |
if (__isSmallInteger(nBytes)) { |
|
2466 |
if (__intVal(nBytes) < objSize) { |
|
2467 |
objSize = __intVal(nBytes); |
|
2468 |
} |
|
2469 |
} |
|
1885 | 2470 |
# ifdef DO_WRAP_CALLS |
4234 | 2471 |
if (extPtr) { |
2472 |
buffer = extPtr + offs; |
|
2473 |
} else { |
|
2474 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
2475 |
} |
|
2476 |
||
2477 |
do { |
|
2478 |
__threadErrno = 0; |
|
2479 |
alen = sizeof(sa); |
|
2480 |
n = (INT)STX_WSA_NOINT_CALL6("recvfrom", recvfrom, sock, buffer, objSize, _flags, (struct sockaddr *)&sa, &alen); |
|
2481 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2482 |
if (n < 0) { |
|
2483 |
errno = __threadErrno; |
|
2484 |
} |
|
2485 |
||
2486 |
if (allocatedBuffer) { |
|
2487 |
if (n > 0) { |
|
2488 |
memcpy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, n); |
|
2489 |
} |
|
2490 |
free(allocatedBuffer); |
|
2491 |
} |
|
1885 | 2492 |
# else |
4234 | 2493 |
__BEGIN_INTERRUPTABLE__ |
2494 |
do { |
|
2495 |
alen = sizeof(sa); |
|
2496 |
if (extPtr) { |
|
2497 |
n = recvfrom(sock, extPtr + offs, objSize, _flags, (struct sockaddr *) &sa, &alen); |
|
2498 |
} else { |
|
2499 |
n = recvfrom(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, (struct sockaddr *) &sa, &alen); |
|
2500 |
} |
|
2501 |
} while ((n < 0) && (errno == EINTR)); |
|
2502 |
__END_INTERRUPTABLE__ |
|
1885 | 2503 |
# endif |
2504 |
||
4234 | 2505 |
if (n >= 0) { |
2506 |
if (__isNonNilObject(addr)) { |
|
2507 |
char *addrPtr; |
|
2508 |
OBJ oClass; |
|
2509 |
int nInstVars, nInstBytes, objSize; |
|
2510 |
||
2511 |
oClass = __qClass(addr); |
|
2512 |
if (! __isBytes(addr) ) |
|
2513 |
goto bad; |
|
2514 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2515 |
nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ)); |
|
2516 |
objSize = __qSize(addr) - nInstBytes; |
|
2517 |
addrPtr = (char *)__InstPtr(addr) + nInstBytes; |
|
2518 |
if (objSize < alen) |
|
2519 |
goto bad; |
|
2520 |
||
2521 |
/* |
|
2522 |
* extract the datagrams address |
|
2523 |
*/ |
|
2524 |
memcpy(addrPtr, (char *)&sa, alen); |
|
2525 |
addrLen = __MKSMALLINT(alen); |
|
2526 |
} |
|
2527 |
} |
|
2528 |
if (n < 0) { |
|
2529 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2530 |
} |
|
2531 |
nReceived = __MKSMALLINT(n); |
|
1885 | 2532 |
} |
2533 |
#endif |
|
2534 |
bad: ; |
|
2535 |
%}. |
|
3126 | 2536 |
error notNil ifTrue:[ |
4234 | 2537 |
^ self readError:error. |
3126 | 2538 |
]. |
2539 |
||
1885 | 2540 |
nReceived notNil ifTrue:[ |
4234 | 2541 |
addrLen notNil ifTrue:[ |
2542 |
(addr == anAddressBuffer) ifFalse:[ |
|
2543 |
self obsoleteFeatureWarning:'please use a socketAddress argument'. |
|
2544 |
||
2545 |
"can be a ByteArray for backward compatibility" |
|
2546 |
anAddressBuffer replaceFrom:1 to:addrLen with:(addr hostAddress). |
|
2547 |
]. |
|
2548 |
]. |
|
2549 |
^ nReceived |
|
1885 | 2550 |
]. |
2551 |
" |
|
2552 |
arrive here if you try to receive into an invalid buffer |
|
2553 |
(i.e. not ByteArray-like), |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2554 |
or if the addressBuffer is nonNil AND not a SocketAddress/ByteArray |
126 | 2555 |
or if the addressBuffer is nonNil AND too small. |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2556 |
" |
126 | 2557 |
self primitiveFailed |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2558 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2559 |
|
1885 | 2560 |
sendBuffer:aDataBuffer start:startIndex for:nBytes flags:flags |
2561 |
"send data. |
|
2562 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2563 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2564 |
Return the number of bytes transmitted, or a negative number on error. |
|
2565 |
On error, the unix error code is left in the lastErrorNumber |
|
2566 |
instance variable." |
|
2567 |
||
3126 | 2568 |
|error| |
1885 | 2569 |
|
2570 |
%{ |
|
2571 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2572 |
OBJ fp = __INST(handle); |
1885 | 2573 |
|
2574 |
if ((fp != nil) |
|
2575 |
&& __isSmallInteger(startIndex) |
|
2576 |
&& __isSmallInteger(nBytes)) { |
|
3657 | 2577 |
SOCKET sock; |
2578 |
INT objSize, n, offs; |
|
2579 |
char *extPtr; |
|
2580 |
int _flags = __longIntVal(flags); |
|
2581 |
unsigned long norder; |
|
2582 |
unsigned char *buffer, *allocatedBuffer = NULL; |
|
2583 |
||
2584 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2585 |
||
2586 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2587 |
if (__isSmallInteger(nBytes)) { |
|
2588 |
if (__intVal(nBytes) < objSize) { |
|
2589 |
objSize = __intVal(nBytes); |
|
2590 |
} |
|
2591 |
} |
|
1885 | 2592 |
|
2593 |
# ifdef DGRAM_DEBUG |
|
3657 | 2594 |
console_printf("sending %d bytes ...\n", nBytes); |
1885 | 2595 |
# endif |
2596 |
||
2597 |
#ifdef DO_WRAP_CALLS |
|
3657 | 2598 |
if (extPtr) { |
2599 |
buffer = extPtr + offs; |
|
2600 |
} else { |
|
2601 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
2602 |
bcopy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, objSize); |
|
2603 |
} |
|
2604 |
||
2605 |
do { |
|
2606 |
__threadErrno = 0; |
|
2607 |
n = (INT)STX_WSA_NOINT_CALL4("send", send, sock, buffer, objSize, _flags); |
|
2608 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2609 |
if (n < 0) { |
|
2610 |
errno = __threadErrno; |
|
2611 |
} |
|
2612 |
||
2613 |
if (allocatedBuffer) { |
|
2614 |
free(allocatedBuffer); |
|
2615 |
} |
|
1885 | 2616 |
#else |
3657 | 2617 |
__BEGIN_INTERRUPTABLE__ |
2618 |
do { |
|
2619 |
if (extPtr) { |
|
2620 |
n = send(sock, extPtr + offs, objSize, _flags); |
|
2621 |
} else { |
|
2622 |
n = send(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags); |
|
2623 |
} |
|
2624 |
} while ((n < 0) && (errno == EINTR)); |
|
2625 |
__END_INTERRUPTABLE__ |
|
1885 | 2626 |
#endif |
2627 |
||
3657 | 2628 |
if (n < 0) { |
2629 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2630 |
} else { |
|
2631 |
RETURN (__MKSMALLINT(n)); |
|
2632 |
} |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2633 |
} |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2634 |
#endif |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2635 |
bad: ; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2636 |
%}. |
3126 | 2637 |
error notNil ifTrue:[ |
3657 | 2638 |
self writeError:error. |
3126 | 2639 |
]. |
2640 |
||
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2641 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2642 |
arrive here if you try to send from an invalid buffer (i.e. not ByteArray-like), |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2643 |
" |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2644 |
self primitiveFailed |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2645 |
! |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2646 |
|
1885 | 2647 |
sendTo:anAddressBuffer buffer:buffer |
2648 |
"send datagramm data - fetch address of destination host from |
|
2649 |
anAddressBuffer, data from aDataBuffer. |
|
2650 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2651 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2652 |
Return the number of bytes transmitted, or a negative number on error. |
|
2653 |
On error, the unix error code is left in the lastErrorNumber |
|
126 | 2654 |
instance variable. |
2655 |
Flags is currently ignored; it is there for ST-80 compatibility." |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2656 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2657 |
^ self sendTo:anAddressBuffer buffer:buffer start:1 for:buffer size flags:0 |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2658 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2659 |
|
1885 | 2660 |
sendTo:anAddressBuffer buffer:buffer start:startIndex for:count |
2661 |
"send datagramm data - fetch address of destination host from |
|
2662 |
anAddressBuffer, data from aDataBuffer. |
|
2663 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2664 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2665 |
Return the number of bytes transmitted, or a negative number on error. |
|
2666 |
On error, the unix error code is left in the lastErrorNumber |
|
2667 |
instance variable. |
|
817 | 2668 |
Flags is currently ignored; it is there for ST-80 compatibility." |
2669 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2670 |
^ self sendTo:anAddressBuffer buffer:buffer start:startIndex for:count flags:0 |
1885 | 2671 |
! |
2672 |
||
2673 |
sendTo:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:flags |
|
2674 |
"send datagramm data - fetch address of destination host from |
|
2675 |
anAddressBuffer, data from aDataBuffer starting at startIndex, |
|
2676 |
sending count bytes. |
|
2677 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2678 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2679 |
Return the number of bytes transmitted, or a negative number on error. |
|
2680 |
On error, the unix error code is left in the lastErrorNumber |
|
2681 |
instance variable." |
|
2682 |
||
3126 | 2683 |
|domainClass addr error| |
1885 | 2684 |
|
4234 | 2685 |
anAddressBuffer isSocketAddress ifTrue:[ |
2686 |
addr := anAddressBuffer. |
|
1885 | 2687 |
] ifFalse:[ |
4234 | 2688 |
anAddressBuffer isByteArray ifFalse:[ |
2689 |
^ self error:'bad socketAddress argument' |
|
2690 |
]. |
|
2691 |
domainClass := self class socketAddressClassForDomain:domain. |
|
2692 |
domainClass isNil ifTrue:[ |
|
2693 |
^ self error:'invalid (unsupported) domain'. |
|
2694 |
]. |
|
2695 |
addr := domainClass hostAddress:anAddressBuffer. |
|
1885 | 2696 |
]. |
2697 |
%{ |
|
2698 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2699 |
OBJ fp = __INST(handle); |
1885 | 2700 |
|
2701 |
if ((fp != nil) |
|
2702 |
&& __isSmallInteger(startIndex) |
|
2703 |
&& __isSmallInteger(nBytes)) { |
|
4234 | 2704 |
SOCKET sock; |
2705 |
INT objSize; |
|
2706 |
struct sockaddr *sockaddr_ptr; |
|
2707 |
union sockaddr_u sa; |
|
2708 |
socklen_t sockaddr_size, alen = 0; |
|
2709 |
INT sockAddrOffs; |
|
2710 |
INT n, offs; |
|
2711 |
char *extPtr; |
|
2712 |
int _flags = __longIntVal(flags); |
|
2713 |
unsigned long norder; |
|
2714 |
unsigned char *buffer; |
|
2715 |
unsigned char *allocatedBuffer = NULL; |
|
2716 |
||
2717 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2718 |
||
2719 |
if (! __isBytes(addr)) { |
|
2720 |
sockaddr_size = 0; |
|
2721 |
sockaddr_ptr = (struct sockaddr *)0; |
|
2722 |
} else { |
|
2723 |
int nIndex; |
|
2724 |
OBJ cls; |
|
2725 |
||
2726 |
sockAddrOffs = 0; |
|
2727 |
if ((cls = __qClass(addr)) != @global(ByteArray)) |
|
2728 |
sockAddrOffs += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2729 |
nIndex = __qSize(addr) - OHDR_SIZE; |
|
2730 |
sockaddr_size = nIndex - sockAddrOffs; |
|
2731 |
if (sockaddr_size > sizeof(sa)) { |
|
2732 |
console_fprintf(stderr, "Socket [warning]: bad socketAddr\n"); |
|
2733 |
goto bad; |
|
2734 |
} |
|
2735 |
memcpy(&sa, (__byteArrayVal(addr) + sockAddrOffs), sockaddr_size); |
|
2736 |
sockaddr_ptr = (struct sockaddr *)(&sa); |
|
2737 |
} |
|
2738 |
||
2739 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2740 |
if (__isSmallInteger(nBytes)) { |
|
2741 |
if (__intVal(nBytes) < objSize) { |
|
2742 |
objSize = __intVal(nBytes); |
|
2743 |
} |
|
2744 |
} |
|
1885 | 2745 |
|
2746 |
#ifdef DO_WRAP_CALLS |
|
4234 | 2747 |
if (extPtr) { |
2748 |
buffer = extPtr + offs; |
|
2749 |
} else { |
|
2750 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
2751 |
bcopy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, objSize); |
|
2752 |
} |
|
2753 |
||
2754 |
do { |
|
2755 |
__threadErrno = 0; |
|
2756 |
n = (INT)STX_WSA_NOINT_CALL6("sendto", sendto, sock, buffer, objSize, _flags, sockaddr_ptr, sockaddr_size); |
|
2757 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2758 |
if (n < 0) { |
|
2759 |
errno = __threadErrno; |
|
2760 |
} |
|
2761 |
||
2762 |
if (allocatedBuffer) { |
|
2763 |
free(allocatedBuffer); |
|
2764 |
} |
|
1885 | 2765 |
#else |
4234 | 2766 |
__BEGIN_INTERRUPTABLE__ |
2767 |
do { |
|
2768 |
if (extPtr) { |
|
2769 |
n = sendto(sock, extPtr + offs, objSize, _flags, sockaddr_ptr, sockaddr_size); |
|
2770 |
} else { |
|
2771 |
n = sendto(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, sockaddr_ptr, sockaddr_size); |
|
2772 |
} |
|
2773 |
} while ((n < 0) && (errno == EINTR)); |
|
2774 |
__END_INTERRUPTABLE__ |
|
1885 | 2775 |
#endif |
2776 |
||
4234 | 2777 |
if (n < 0) { |
2778 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2779 |
} else { |
|
2780 |
RETURN (__MKSMALLINT(n)); |
|
2781 |
} |
|
1885 | 2782 |
} |
2783 |
#endif |
|
2784 |
bad: ; |
|
2785 |
%}. |
|
3126 | 2786 |
error notNil ifTrue:[ |
4234 | 2787 |
self writeError:error. |
3126 | 2788 |
]. |
2789 |
||
1885 | 2790 |
" |
2791 |
arrive here if you try to send from an invalid buffer |
|
2792 |
(i.e. not ByteArray-like), |
|
2793 |
or if the addressBuffer is nonNil AND not a ByteArray/String |
|
2794 |
or if the addressBuffer is nonNil AND too small. |
|
2795 |
" |
|
2796 |
self primitiveFailed |
|
0 | 2797 |
! ! |
2798 |
||
4172 | 2799 |
!Socket methodsFor:'finalization'! |
2800 |
||
2801 |
finalize |
|
2802 |
self linger:0. "/ do an abortive release - discard buffered data |
|
2803 |
self closeFile. "/ does not wait due to abortive release. |
|
2804 |
! ! |
|
2805 |
||
3484 | 2806 |
!Socket methodsFor:'initialization'! |
2807 |
||
2808 |
initialize |
|
2809 |
super initialize. |
|
2810 |
"/ transparent |
|
2811 |
eolMode := nil. |
|
2812 |
! ! |
|
2813 |
||
3123 | 2814 |
!Socket methodsFor:'low level'! |
1885 | 2815 |
|
1935 | 2816 |
getSocketAdress |
2817 |
"BAD SPELLING, of #getSocketAddress, kept for compatibility with swazoo" |
|
2818 |
||
2819 |
<resource: #obsolete> |
|
2820 |
||
2821 |
^ self getSocketAddress |
|
2822 |
! |
|
2823 |
||
1885 | 2824 |
getSocketError |
2825 |
"get the SO_ERROR form the socket, which indicates the |
|
2826 |
result of an asynchronous operation" |
|
2827 |
||
2828 |
%{ |
|
2829 |
#ifndef NO_SOCKET |
|
2830 |
OBJ fp; |
|
2831 |
int err; |
|
2832 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2833 |
fp = __INST(handle); |
1885 | 2834 |
if (fp == nil) { |
2835 |
err = EBADF; |
|
2836 |
} else { |
|
2837 |
unsigned int sz; |
|
2838 |
SOCKET sock; |
|
2839 |
||
2840 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2841 |
sz = sizeof(err); |
|
2909 | 2842 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)(&err), &sz) < 0) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2843 |
# ifdef __win32__ |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
2844 |
errno = WSAGetLastError(); |
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2845 |
# endif |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
2846 |
err = errno; |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
2847 |
} |
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2848 |
} |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2849 |
|
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2850 |
RETURN(__MKSMALLINT(err)); |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2851 |
#endif |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2852 |
%} |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2853 |
! |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2854 |
|
1935 | 2855 |
listenWithBacklog:aNumber |
2856 |
<resource: #obsolete> |
|
2857 |
"same as listenFor: - backward compatibility with old ST/X" |
|
3059 | 2858 |
self obsoleteMethodWarning. |
1935 | 2859 |
^ self listenFor:aNumber |
1885 | 2860 |
! |
2861 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2862 |
primAcceptOn:aServerSocket blocking:blocking |
3059 | 2863 |
"accept a connection on a server port (created with:'Socket>>newTCP:') |
2864 |
usage is: (Socket basicNew acceptOn:(Socket newTCP:9999)). |
|
1935 | 2865 |
Return the true if ok; false if not. |
2866 |
If blocking is true, the accept() syscall (and the whole smalltalk image) |
|
2867 |
will block, until a connection is accepted. |
|
2868 |
If blocking is false, this call will return immediately, if there is no connection pending." |
|
2869 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2870 |
|serverSocketHandle addr domainClass newHandle| |
1935 | 2871 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2872 |
handle notNil ifTrue:[ |
4180 | 2873 |
^ self errorAlreadyOpen. |
2874 |
]. |
|
2875 |
serverSocketHandle := aServerSocket fileHandle. |
|
2876 |
serverSocketHandle isNil ifTrue:[ |
|
2877 |
"socket is not open" |
|
2878 |
^ false |
|
1935 | 2879 |
]. |
2880 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2881 |
domain := aServerSocket domain. |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2882 |
socketType := aServerSocket type. |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2883 |
handleType := aServerSocket handleType. |
3059 | 2884 |
"unix domain sockets do not return a valid peer name on accept" |
1935 | 2885 |
domainClass := self class socketAddressClassForDomain:domain. |
2886 |
domainClass isNil ifTrue:[ |
|
4180 | 2887 |
^ self error:'invalid (unsupported) domain'. |
1935 | 2888 |
]. |
2889 |
addr := domainClass new. |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2890 |
newHandle := OperatingSystem socketAccessor new. |
1935 | 2891 |
|
2892 |
%{ /* STACK: 100000 */ |
|
1885 | 2893 |
#ifndef NO_SOCKET |
1935 | 2894 |
FILE *fp; |
2895 |
int flags; |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2896 |
SOCKET serverSocket, newSock; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2897 |
int _fd; |
1935 | 2898 |
union sockaddr_u sa; |
3059 | 2899 |
unsigned int alen; |
2900 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2901 |
# ifdef __win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2902 |
serverSocket = SOCKET_FROM_FILE_OBJECT(serverSocketHandle); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2903 |
# else |
3247 | 2904 |
serverSocket = __intVal(serverSocketHandle); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2905 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2906 |
|
1935 | 2907 |
# if defined(O_NONBLOCK) && defined(SET_NDELAY) |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2908 |
if (blocking == false) { |
4180 | 2909 |
flags = fcntl(serverSocket, F_GETFL); |
2910 |
fcntl(serverSocket, F_SETFL, flags | O_NONBLOCK); |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2911 |
} |
1935 | 2912 |
# endif |
2913 |
||
1885 | 2914 |
# ifdef DO_WRAP_CALLS |
2915 |
do { |
|
4180 | 2916 |
__threadErrno = 0; |
2917 |
alen = sizeof(sa); |
|
2918 |
newSock = (SOCKET)STX_WSA_CALL3("accept", accept, serverSocket, &sa, &alen); |
|
1935 | 2919 |
} while ((newSock < 0) && (__threadErrno == EINTR)); |
3127 | 2920 |
if (newSock < 0) { |
4180 | 2921 |
errno = __threadErrno; |
3126 | 2922 |
} |
1885 | 2923 |
# else |
2924 |
__BEGIN_INTERRUPTABLE__ |
|
2925 |
do { |
|
4180 | 2926 |
alen = sizeof(sa); |
2927 |
newSock = accept(serverSocket, (struct sockaddr *) &sa, &alen); |
|
1935 | 2928 |
} while ((newSock < 0) && (errno == EINTR)); |
1885 | 2929 |
__END_INTERRUPTABLE__ |
2930 |
# endif |
|
1935 | 2931 |
DBGFPRINTF((stderr, "SOCKET: accept newSock=%d\n", newSock)); |
2932 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2933 |
# if defined(O_NDELAY) && defined(SET_NDELAY) |
1935 | 2934 |
if (blocking == false) { |
4180 | 2935 |
fcntl(serverSocket, F_SETFL, flags); |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2936 |
} |
1935 | 2937 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2938 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2939 |
if (newSock == -1) { |
4180 | 2940 |
DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno)); |
2941 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2942 |
RETURN (false); |
|
1935 | 2943 |
} |
2944 |
||
2945 |
if (__isNonNilObject(addr)) { |
|
4180 | 2946 |
OBJ oClass = __qClass(addr); |
2947 |
int nInstVars, nInstBytes, objSize; |
|
2948 |
char *addrP; |
|
2949 |
||
2950 |
if (! __isBytes(addr) ) { |
|
2951 |
DBGPRINTF(("SOCKET: bad addr\n")); |
|
2952 |
closesocket(newSock); |
|
2953 |
RETURN (false); |
|
2954 |
} |
|
2955 |
||
2956 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2957 |
nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ)); |
|
2958 |
objSize = __qSize(addr) - nInstBytes; |
|
2959 |
addrP = (char *)__InstPtr(addr) + nInstBytes; |
|
2960 |
if (objSize < alen) { |
|
2961 |
DBGPRINTF(("SOCKET: bad addr\n")); |
|
2962 |
closesocket(newSock); |
|
2963 |
RETURN (false); |
|
2964 |
} |
|
2965 |
||
2966 |
/* |
|
2967 |
* extract the partners address |
|
2968 |
*/ |
|
2969 |
memcpy(addrP, (char *)&sa, alen); |
|
1935 | 2970 |
} |
2971 |
||
2972 |
/* |
|
2973 |
* make it a FILE * |
|
2974 |
*/ |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2975 |
# ifdef __win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2976 |
# if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ )) |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2977 |
__stxWrapApiEnterCritical(); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2978 |
_fd = _open_osfhandle((long)newSock, 0); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2979 |
__stxWrapApiLeaveCritical(); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2980 |
DBGPRINTF(("SOCKET: sock=%d fd=%d\n", newSock, _fd)); |
2773 | 2981 |
# else |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2982 |
_fd = (int)newSock; |
2773 | 2983 |
# endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2984 |
# else // ! __win32__ |
1935 | 2985 |
fp = fdopen(newSock, "r+"); |
2986 |
if (! fp) { |
|
4180 | 2987 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
2988 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2989 |
closesocket(newSock); |
|
2990 |
DBGFPRINTF((stderr, "SOCKET: close (fdopen failed) (%d)\n", newSock)); |
|
2991 |
RETURN (false); |
|
1935 | 2992 |
} |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2993 |
# endif // ! __win32__ |
1935 | 2994 |
|
2995 |
if ((@global(FileOpenTrace) == true) || __debugging__) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2996 |
# ifdef __win32__ |
4180 | 2997 |
console_fprintf(stderr, "fdopen [Socket accept] -> fd: %d (H: %"_lx_")\n", _fd, (INT)newSock); |
1935 | 2998 |
# else |
4180 | 2999 |
console_fprintf(stderr, "fdopen [Socket accept] -> %"_lx_" (fd: %d)\n", (INT)fp, newSock); |
1935 | 3000 |
# endif |
3001 |
} |
|
3002 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3003 |
# ifdef __win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3004 |
__externalAddressVal(newHandle) = _fd; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3005 |
__INST(handleType) = @symbol(socketHandle); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3006 |
# else |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3007 |
__externalAddressVal(newHandle) = fp; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3008 |
__INST(handleType) = @symbol(socketFilePointer); |
1935 | 3009 |
# endif |
3010 |
#endif /* not NO_SOCKET */ |
|
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3011 |
%}. |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3012 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3013 |
handle := newHandle. |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3014 |
buffered := false. |
1935 | 3015 |
mode := #readwrite. |
3016 |
binary := false. |
|
3620 | 3017 |
self registerForFinalization. |
3059 | 3018 |
peer := addr. |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3019 |
port := aServerSocket port. |
1935 | 3020 |
|
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3021 |
^ true |
2551 | 3022 |
! |
1885 | 3023 |
|
3024 |
setSocketOption:option argument:arg1 argument:arg2 |
|
3513 | 3025 |
|ok error| |
1885 | 3026 |
|
2551 | 3027 |
handle isNil ifTrue:[ |
3590 | 3028 |
^ self errorNotOpen |
2551 | 3029 |
]. |
3030 |
||
1885 | 3031 |
%{ /* STACK: 32000 */ |
2818 | 3032 |
#ifndef NO_SOCKET |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3033 |
OBJ fp = __INST(handle); |
1885 | 3034 |
|
3035 |
if (fp != nil) { |
|
3590 | 3036 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
3037 |
int opt = -1; |
|
3038 |
int level = -1; |
|
3039 |
int usize = -1; |
|
3040 |
int ret; |
|
3041 |
union u { |
|
3042 |
BOOL u_bool; |
|
3043 |
int u_int; |
|
3044 |
struct linger u_linger; |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3045 |
# ifdef IP_ADD_MEMBERSHIP |
3590 | 3046 |
struct ip_mreq u_ip_mreq; |
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3047 |
# endif |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3048 |
# if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO) |
3590 | 3049 |
struct timeval u_tv; |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3050 |
# endif |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3051 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3052 |
|
3590 | 3053 |
} u; |
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3054 |
|
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3055 |
# ifdef IP_ADD_MEMBERSHIP |
3590 | 3056 |
if (option == @symbol(IP_DROP_MEMBERSHIP)) { |
3057 |
opt = IP_DROP_MEMBERSHIP; |
|
3058 |
goto dropOrAdd; |
|
3059 |
} |
|
3060 |
if (option == @symbol(IP_ADD_MEMBERSHIP)) { |
|
3061 |
/* add membership to a multicast group */ |
|
3062 |
opt = IP_ADD_MEMBERSHIP; |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3063 |
dropOrAdd: |
3590 | 3064 |
level = IPPROTO_IP; |
3065 |
usize = sizeof(u.u_ip_mreq); |
|
3066 |
if (__isByteArrayLike(arg1) && __isByteArrayLike(arg2)) { |
|
3067 |
memcpy(&u.u_ip_mreq.imr_multiaddr, __byteArrayVal(arg1), sizeof(u.u_ip_mreq.imr_multiaddr)); |
|
3068 |
memcpy(&u.u_ip_mreq.imr_interface, __byteArrayVal(arg2), sizeof(u.u_ip_mreq.imr_interface)); |
|
3069 |
} |
|
3070 |
else |
|
3071 |
goto argError; |
|
3072 |
} |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3073 |
# endif /* IP_ADD_MEMBERSHIP */ |
1885 | 3074 |
|
3075 |
# ifdef SO_BROADCAST |
|
3590 | 3076 |
if (option == @symbol(SO_BROADCAST)) { |
3077 |
/* Enables transmission and receipt of broadcast messages on the socket. */ |
|
3078 |
opt = SO_BROADCAST; |
|
3079 |
level = SOL_SOCKET; |
|
3080 |
usize = sizeof(u.u_bool); |
|
3081 |
if (arg1 == true) u.u_bool = TRUE; |
|
3082 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3083 |
else goto argError; |
|
3084 |
} |
|
1885 | 3085 |
# endif /* SO_BROADCAST */ |
3086 |
||
3087 |
# ifdef SO_CONDITIONAL |
|
3088 |
# if 0 |
|
3590 | 3089 |
if (option == @symbol(SO_CONDITIONAL)) { |
3090 |
/* Enables sockets to delay the acknowledgment of a connection until after the WSAAccept condition function is called. */ |
|
3091 |
opt = SO_CONDITIONAL; |
|
3092 |
level = SOL_SOCKET; |
|
3093 |
usize = sizeof(u.u_bool); |
|
3094 |
if (arg1 == true) u.u_bool = TRUE; |
|
3095 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3096 |
else goto argError; |
|
3097 |
} |
|
1885 | 3098 |
# endif |
3099 |
# endif /* SO_CONDITIONAL */ |
|
3100 |
||
3101 |
# ifdef SO_DEBUG |
|
3590 | 3102 |
if (option == @symbol(SO_DEBUG)) { |
3103 |
/* Records debugging information. */ |
|
3104 |
opt = SO_DEBUG; |
|
3105 |
level = SOL_SOCKET; |
|
3106 |
usize = sizeof(u.u_bool); |
|
3107 |
if (arg1 == true) u.u_bool = TRUE; |
|
3108 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3109 |
else goto argError; |
|
3110 |
} |
|
1885 | 3111 |
# endif /* SO_DEBUG */ |
3112 |
||
3113 |
# ifdef SO_DONTLINGER |
|
3590 | 3114 |
if (option == @symbol(SO_DONTLINGER)) { |
3115 |
/* Does not block close waiting for unsent data to be sent. |
|
3116 |
Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. */ |
|
3117 |
opt = SO_DONTLINGER; |
|
3118 |
level = SOL_SOCKET; |
|
3119 |
usize = sizeof(u.u_bool); |
|
3120 |
if (arg1 == true) u.u_bool = TRUE; |
|
3121 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3122 |
else goto argError; |
|
3123 |
} |
|
1885 | 3124 |
# endif /* SO_DONTLINGER */ |
3125 |
||
3126 |
# ifdef SO_DONTROUTE |
|
3590 | 3127 |
if (option == @symbol(SO_DONTROUTE)) { |
3128 |
/* Does not route: sends directly to interface. |
|
3129 |
Succeeds but is ignored on AF_INET sockets; |
|
3130 |
fails on AF_INET6 sockets with WSAENOPROTOOPT. |
|
3131 |
Not supported on ATM sockets (results in an error). */ |
|
3132 |
opt = SO_DONTROUTE; |
|
3133 |
level = SOL_SOCKET; |
|
3134 |
usize = sizeof(u.u_bool); |
|
3135 |
if (arg1 == true) u.u_bool = TRUE; |
|
3136 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3137 |
else goto argError; |
|
3138 |
} |
|
1885 | 3139 |
# endif /* SO_DONTROUTE */ |
3140 |
||
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3141 |
#if defined(IPPROTO_TCP) && defined(TCP_NODELAY) |
3590 | 3142 |
if (option == @symbol(TCP_NODELAY)) { |
3143 |
/* enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) */ |
|
3144 |
opt = TCP_NODELAY; |
|
3145 |
level = IPPROTO_TCP; |
|
3146 |
usize = sizeof(u.u_bool); |
|
3147 |
if (arg1 == true) u.u_bool = TRUE; |
|
3148 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3149 |
else goto argError; |
|
3150 |
} |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3151 |
# endif /* TCP_NODELAY */ |
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3152 |
|
1885 | 3153 |
# ifdef SO_KEEPALIVE |
3590 | 3154 |
if (option == @symbol(SO_KEEPALIVE)) { |
3155 |
/* Sends keep-alives. Not supported on ATM sockets (results in an error). */ |
|
3156 |
opt = SO_KEEPALIVE; |
|
3157 |
level = SOL_SOCKET; |
|
3158 |
usize = sizeof(u.u_bool); |
|
3159 |
if (arg1 == true) u.u_bool = TRUE; |
|
3160 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3161 |
else goto argError; |
|
3162 |
} |
|
1885 | 3163 |
# endif /* SO_KEEPALIVE */ |
3164 |
||
3165 |
# ifdef SO_LINGER |
|
3590 | 3166 |
if (option == @symbol(SO_LINGER)) { |
3167 |
/* Lingers on close if unsent data is present. */ |
|
3168 |
opt = SO_LINGER; |
|
3169 |
level = SOL_SOCKET; |
|
3170 |
usize = sizeof(u.u_linger); |
|
3171 |
if (arg1 == true) u.u_linger.l_onoff = TRUE; |
|
3172 |
else if (arg1 == false) u.u_linger.l_onoff = FALSE; |
|
3173 |
else goto argError; |
|
3174 |
if (arg2 == nil) u.u_linger.l_linger = 0; |
|
3175 |
else if (__isSmallInteger(arg2))u.u_linger.l_linger = __intVal(arg2); |
|
3176 |
else goto argError; |
|
3177 |
DBGPRINTF(("SOCKET: SO_LINGER %d %d\n", u.u_linger.l_onoff, u.u_linger.l_linger)); |
|
3178 |
} |
|
1885 | 3179 |
# endif /* SO_LINGER */ |
3180 |
||
3181 |
# ifdef SO_OOBINLINE |
|
3590 | 3182 |
if (option == @symbol(SO_OOBINLINE)) { |
3183 |
/* Receives OOB data in the normal data stream. */ |
|
3184 |
opt = SO_OOBINLINE; |
|
3185 |
level = SOL_SOCKET; |
|
3186 |
usize = sizeof(u.u_bool); |
|
3187 |
if (arg1 == true) u.u_bool = TRUE; |
|
3188 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3189 |
else goto argError; |
|
3190 |
} |
|
1885 | 3191 |
# endif /* SO_OOBINLINE */ |
3192 |
||
3193 |
# ifdef SO_RCVBUF |
|
3590 | 3194 |
if (option == @symbol(SO_RCVBUF)) { |
3195 |
/* Specifies the total per-socket buffer space reserved for receives. |
|
3196 |
This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */ |
|
3197 |
opt = SO_RCVBUF; |
|
3198 |
level = SOL_SOCKET; |
|
3199 |
usize = sizeof(u.u_int); |
|
3200 |
if (__isSmallInteger(arg1))u.u_int = __intVal(arg1); |
|
3201 |
else goto argError; |
|
3202 |
} |
|
1885 | 3203 |
# endif /* SO_RCVBUF */ |
3204 |
||
3205 |
# ifdef SO_SNDBUF |
|
3590 | 3206 |
if (option == @symbol(SO_SNDBUF)) { |
3207 |
/* Specifies the total per-socket buffer space reserved for sends. |
|
3208 |
This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */ |
|
3209 |
opt = SO_SNDBUF; |
|
3210 |
level = SOL_SOCKET; |
|
3211 |
usize = sizeof(u.u_int); |
|
3212 |
if (__isSmallInteger(arg1))u.u_int = __intVal(arg1); |
|
3213 |
else goto argError; |
|
3214 |
} |
|
1885 | 3215 |
# endif /* SO_SNDBUF */ |
3216 |
||
3217 |
# ifdef SO_REUSEADDR |
|
3590 | 3218 |
if (option == @symbol(SO_REUSEADDR)) { |
3219 |
/* Allows the socket to be bound to an address that is already in use. */ |
|
3220 |
opt = SO_REUSEADDR; |
|
3221 |
level = SOL_SOCKET; |
|
3222 |
usize = sizeof(u.u_bool); |
|
3223 |
if (arg1 == true) u.u_bool = TRUE; |
|
3224 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3225 |
else goto argError; |
|
3226 |
} |
|
1885 | 3227 |
# endif /* SO_OOBINLINE */ |
3228 |
||
3229 |
# ifdef SO_EXCLUSIVEADDRUSE |
|
3590 | 3230 |
if (option == @symbol(SO_EXCLUSIVEADDRUSE)) { |
3231 |
/* Enables a socket to be bound for exclusive access. |
|
3232 |
Does not require administrative privilege. */ |
|
3233 |
opt = SO_EXCLUSIVEADDRUSE; |
|
3234 |
level = SOL_SOCKET; |
|
3235 |
usize = sizeof(u.u_bool); |
|
3236 |
if (arg1 == true) u.u_bool = TRUE; |
|
3237 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3238 |
else goto argError; |
|
3239 |
} |
|
1885 | 3240 |
# endif /* SO_OOBINLINE */ |
3241 |
||
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3242 |
# ifdef SO_RCVTIMEO |
3590 | 3243 |
if ((option == @symbol(SO_RCVTIMEO)) |
3244 |
&& __isSmallInteger(arg1) |
|
3245 |
&& __isSmallInteger(arg2)) { |
|
3246 |
opt = SO_RCVTIMEO; |
|
3247 |
level = SOL_SOCKET; |
|
3248 |
u.u_tv.tv_sec = __intVal(arg1); |
|
3249 |
u.u_tv.tv_usec = __intVal(arg2); |
|
3250 |
usize = sizeof(u.u_tv); |
|
3251 |
} |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3252 |
# endif /* SO_RCVTIMEO */ |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3253 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3254 |
# ifdef SO_SNDTIMEO |
3590 | 3255 |
if ((option == @symbol(SO_SNDTIMEO)) |
3256 |
&& __isSmallInteger(arg1) |
|
3257 |
&& __isSmallInteger(arg2)) { |
|
3258 |
opt = SO_SNDTIMEO; |
|
3259 |
level = SOL_SOCKET; |
|
3260 |
u.u_tv.tv_sec = __intVal(arg1); |
|
3261 |
u.u_tv.tv_usec = __intVal(arg2); |
|
3262 |
usize = sizeof(u.u_tv); |
|
3263 |
} |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3264 |
# endif /* SO_SNDTIMEO */ |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3265 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3266 |
# if !defined(IPV6_V6ONLY) && defined(__win32__) |
3513 | 3267 |
# define IPPROTO_IPV6 41 |
3268 |
# define IPV6_V6ONLY 27 |
|
3269 |
# endif |
|
3270 |
||
3271 |
# if defined(IPV6_V6ONLY) |
|
3590 | 3272 |
if (option == @symbol(IPV6_V6ONLY)) { |
3273 |
opt = IPV6_V6ONLY; |
|
3274 |
level = IPPROTO_IPV6; |
|
3275 |
usize = sizeof(u.u_bool); |
|
3276 |
if (arg1 == true) u.u_bool = TRUE; |
|
3277 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3278 |
else goto argError; |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3279 |
# ifdef __win32__ |
3591 | 3280 |
console_fprintf(stderr, "%d %d %d %d\n", level, opt, usize, u.u_int); |
3514
2d81c5ffa475
win32_printf is only avail in: guess what
Claus Gittinger <cg@exept.de>
parents:
3513
diff
changeset
|
3281 |
# endif |
3590 | 3282 |
} |
3513 | 3283 |
# endif /* IPV6_V6ONLY */ |
3284 |
||
3590 | 3285 |
if (usize == -1) goto argError; |
3286 |
||
3287 |
ok = (setsockopt(sock, level, opt, (char *)&u, usize) >= 0) ? true : false; |
|
3288 |
if (ok == false) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3289 |
# ifdef __win32__ |
3590 | 3290 |
error = __mkSmallInteger(WSAGetLastError()); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3291 |
# else |
3590 | 3292 |
error = __mkSmallInteger(errno); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3293 |
# endif |
3590 | 3294 |
} |
3513 | 3295 |
|
1885 | 3296 |
} |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3297 |
argError: ; |
2818 | 3298 |
#endif /* NO_SOCKET */ |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3299 |
%}. |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3300 |
ok isNil ifTrue:[ |
3590 | 3301 |
self primitiveFailed |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3302 |
]. |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3303 |
ok ifFalse:[ |
3590 | 3304 |
'++++ Info: Socket>>#setSocketOption:... failed. error: ' infoPrint. error infoPrintCR. |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3305 |
]. |
2551 | 3306 |
! |
1885 | 3307 |
|
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3308 |
shutdown:howNum |
1885 | 3309 |
"shutDown the socket - inform it that no more I/O will be performed. |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3310 |
0 - read side (no further reads) |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3311 |
the tcp receive window is closed, so the peer cannot send more data. |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3312 |
1 - write side (no further writes) |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3313 |
first, all queued data will be delivered to the peer. |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3314 |
Then, an orderly release (TCP FIN) is sent to signal the peer, |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3315 |
that we will not send more data. |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3316 |
2 - both read side and write side." |
1885 | 3317 |
|
3318 |
%{ |
|
3319 |
#ifndef NO_SOCKET |
|
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3320 |
OBJ __handle = __INST(handle); |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3321 |
|
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3322 |
if ((__handle != nil) && __isSmallInteger(howNum)) { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3323 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(__handle); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3324 |
INT how = __intVal(howNum); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3325 |
INT ret; |
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3326 |
|
1885 | 3327 |
# ifdef DO_WRAP_CALLS |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3328 |
do { |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3329 |
__threadErrno = 0; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3330 |
DBGFPRINTF((stderr, "SOCKET: shutDown...\n")); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3331 |
ret = (INT)STX_WSA_NOINT_CALL2("shutdown", shutdown, sock, how); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3332 |
DBGFPRINTF((stderr, "SOCKET: shutDown -> %d (%d)\n", ret, __threadErrno)); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3333 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3334 |
# else |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3335 |
__BEGIN_INTERRUPTABLE__ |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3336 |
shutdown(sock, how); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3337 |
__END_INTERRUPTABLE__ |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3338 |
# endif |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3339 |
} |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3340 |
#endif |
1339 | 3341 |
%}. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3342 |
! ! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3343 |
|
1885 | 3344 |
!Socket methodsFor:'printing & storing'! |
3345 |
||
3346 |
printOn:aStream |
|
3530 | 3347 |
aStream nextPutAll:self className; nextPutAll:'('. |
3348 |
domain printOn:aStream. |
|
3349 |
aStream nextPutAll:' protocol='. |
|
1885 | 3350 |
protocol printOn:aStream. |
3351 |
aStream nextPutAll:' port='. |
|
1935 | 3352 |
self port printOn:aStream. |
1462 | 3353 |
aStream nextPutAll:' peer='. |
3354 |
peer printOn:aStream. |
|
1842 | 3355 |
aStream nextPut:$). |
1885 | 3356 |
! ! |
3357 |
||
3358 |
!Socket methodsFor:'queries'! |
|
3359 |
||
3360 |
domain |
|
3059 | 3361 |
"return the sockets addressing domain (i.e. #AF_INET, #AF_INET6, #AF_UNIX, ...)" |
1885 | 3362 |
|
3363 |
^ domain |
|
126 | 3364 |
! |
3365 |
||
3059 | 3366 |
getFullPeerAddress |
1935 | 3367 |
"implemented for swazoo project (primitive code can't be loaded as extension) |
1885 | 3368 |
Answer my own address (I am bound to this address). |
3369 |
Note that this address may change after a connect or accept." |
|
3370 |
||
3059 | 3371 |
|error domainClass addr| |
1885 | 3372 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3373 |
handle isNil ifTrue:[ |
3217 | 3374 |
^ self errorNotOpen |
1885 | 3375 |
]. |
3376 |
||
3377 |
domainClass := self class socketAddressClassForDomain:domain. |
|
3378 |
domainClass isNil ifTrue:[ |
|
3217 | 3379 |
^ self error:'invalid (unsupported) domain'. |
1885 | 3380 |
]. |
3381 |
addr := domainClass new. |
|
3382 |
||
3383 |
%{ |
|
3384 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3385 |
OBJ fp = __INST(handle); |
1885 | 3386 |
SOCKET sock; |
3387 |
int ret; |
|
3388 |
union sockaddr_u sa; |
|
3059 | 3389 |
unsigned int alen = sizeof(sa); |
1885 | 3390 |
char *addrP; |
3391 |
int addrObjSize, nAddrInstBytes; |
|
3059 | 3392 |
OBJ addrClass; |
3393 |
int nAddrInstVars; |
|
1885 | 3394 |
|
3395 |
if (!__isNonNilObject(addr) || !__isBytes(addr)) { |
|
3217 | 3396 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3397 |
error = @symbol(badArgument); |
|
3398 |
goto err; |
|
3059 | 3399 |
} |
3400 |
||
3401 |
addrClass = __qClass(addr); |
|
3402 |
nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars); |
|
3403 |
nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ)); |
|
3404 |
addrObjSize = __qSize(addr) - nAddrInstBytes; |
|
3405 |
||
3406 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
3407 |
ret = getpeername(sock, (struct sockaddr *)&sa, &alen); |
|
3408 |
if (ret < 0) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3409 |
# ifdef __win32__ |
3217 | 3410 |
errno = WSAGetLastError(); |
3059 | 3411 |
# endif |
3217 | 3412 |
DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno)); |
3413 |
error = __MKSMALLINT(errno); |
|
3414 |
goto err; |
|
3059 | 3415 |
} |
3416 |
||
3417 |
if (addrObjSize < alen) { |
|
3217 | 3418 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3419 |
error = @symbol(badArgument); |
|
3420 |
goto err; |
|
3059 | 3421 |
} |
3422 |
||
3423 |
addrP = (char *)__InstPtr(addr) + nAddrInstBytes; |
|
3424 |
memcpy(addrP, (char *)&sa, alen); |
|
3425 |
||
3426 |
err:; |
|
3427 |
#else /* NO_SOCKET */ |
|
3428 |
error = @symbol(notImplemented); |
|
3429 |
#endif /* NO_SOCKET */ |
|
3430 |
%}. |
|
3431 |
error notNil ifTrue:[ |
|
3217 | 3432 |
^ self errorReporter reportOn:error |
3059 | 3433 |
]. |
3434 |
^ addr |
|
3435 |
! |
|
3436 |
||
3437 |
getFullSocketAddress |
|
3438 |
"implemented for swazoo project (primitive code can't be loaded as extension) |
|
3439 |
Answer my own address (I am bound to this address). |
|
3440 |
Note that this address may change after a connect or accept." |
|
3441 |
||
3442 |
|error domainClass addr| |
|
3443 |
||
3444 |
handle isNil ifTrue:[ |
|
3217 | 3445 |
^ self errorNotOpen |
3059 | 3446 |
]. |
3447 |
||
3448 |
domainClass := self class socketAddressClassForDomain:domain. |
|
3449 |
domainClass isNil ifTrue:[ |
|
3217 | 3450 |
^ self error:'invalid (unsupported) domain'. |
3059 | 3451 |
]. |
3452 |
addr := domainClass new. |
|
3453 |
||
3454 |
%{ |
|
3455 |
#ifndef NO_SOCKET |
|
3456 |
OBJ fp = __INST(handle); |
|
3457 |
SOCKET sock; |
|
3458 |
int ret; |
|
3459 |
union sockaddr_u sa; |
|
3460 |
unsigned int alen = sizeof(sa); |
|
3461 |
char *addrP; |
|
3462 |
int addrObjSize, nAddrInstBytes; |
|
3463 |
||
3464 |
if (!__isNonNilObject(addr) || !__isBytes(addr)) { |
|
3217 | 3465 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3466 |
error = @symbol(badArgument); |
|
3467 |
goto err; |
|
1885 | 3468 |
} |
3469 |
||
3470 |
{ |
|
3217 | 3471 |
OBJ addrClass; |
3472 |
int nAddrInstVars; |
|
3473 |
||
3474 |
addrClass = __qClass(addr); |
|
3475 |
nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars); |
|
3476 |
nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ)); |
|
3477 |
addrObjSize = __qSize(addr) - nAddrInstBytes; |
|
1885 | 3478 |
} |
3479 |
||
3480 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
3481 |
ret = getsockname(sock, (struct sockaddr *)&sa, &alen); |
|
3482 |
if (ret < 0) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3483 |
# ifdef __win32__ |
3217 | 3484 |
errno = WSAGetLastError(); |
1885 | 3485 |
# endif |
3217 | 3486 |
DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno)); |
3487 |
error = __MKSMALLINT(errno); |
|
3488 |
goto err; |
|
1885 | 3489 |
} |
3490 |
||
3491 |
if (addrObjSize < alen) { |
|
3217 | 3492 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3493 |
error = @symbol(badArgument); |
|
3494 |
goto err; |
|
1885 | 3495 |
} |
3496 |
||
3497 |
addrP = (char *)__InstPtr(addr) + nAddrInstBytes; |
|
3059 | 3498 |
memcpy(addrP, (char *)&sa, alen); |
1535 | 3499 |
|
3500 |
err:; |
|
3501 |
#else /* NO_SOCKET */ |
|
3502 |
error = @symbol(notImplemented); |
|
3503 |
#endif /* NO_SOCKET */ |
|
3504 |
%}. |
|
3505 |
error notNil ifTrue:[ |
|
3217 | 3506 |
^ self errorReporter reportOn:error |
1535 | 3507 |
]. |
3508 |
^ addr |
|
3509 |
! |
|
3510 |
||
1885 | 3511 |
getName |
3512 |
"return the name; here, we return the ports name" |
|
3513 |
||
1935 | 3514 |
^ self port printString |
126 | 3515 |
! |
3516 |
||
1885 | 3517 |
getPeer |
3518 |
"ST-80 compatibility: return an IPSocketAddress instance representing |
|
3519 |
my hostname/port combination. |
|
3520 |
If you are interested in the hostname, use getPeerName directly." |
|
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
3521 |
|
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
3522 |
^ peer |
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
3523 |
! |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
3524 |
|
1885 | 3525 |
getPeerName |
4091 | 3526 |
"return the peer name; that's the hostname (or dotted name) of the |
25 | 3527 |
partners host after an accept." |
3528 |
||
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
3529 |
peerName isNil ifTrue:[ |
4091 | 3530 |
peerName := self class peerNameFromDomain:domain peer:peer. |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
3531 |
]. |
25 | 3532 |
^ peerName |
28 | 3533 |
! |
3534 |
||
1885 | 3535 |
getSocketAddress |
3536 |
"implemented for swazoo project primitive code cant load as extension |
|
3537 |
answer my own address (I am bound to this address). |
|
1146 | 3538 |
Note that this address may change after connect or accept." |
1029
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
3539 |
|
1535 | 3540 |
^ self getFullSocketAddress hostAddress |
1146 | 3541 |
! |
3542 |
||
1885 | 3543 |
isActive |
3544 |
"return true, if the receiver has a connection" |
|
3545 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3546 |
^ handle notNil |
1885 | 3547 |
! |
3548 |
||
3549 |
isConnected |
|
3550 |
"return true, if the receiver has a connection" |
|
3551 |
||
3552 |
^ self isActive |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3553 |
and:[peer notNil] |
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3554 |
! |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3555 |
|
1885 | 3556 |
port |
1935 | 3557 |
"return the port number (or name for unix-sockets) to which the socket is bound |
3558 |
- so this is the local port." |
|
3559 |
||
3560 |
"/ port isNil ifTrue:[ |
|
3561 |
"/ port := self getFullSocketAddress port. |
|
1505 | 3562 |
"/ ]. |
1935 | 3563 |
|
71 | 3564 |
^ port |
1885 | 3565 |
! |
3566 |
||
3059 | 3567 |
socketAddressClass |
3568 |
"get the matchon SocketAddress class for this socket" |
|
3569 |
||
3570 |
|domainClass| |
|
3571 |
||
3572 |
domainClass := self class socketAddressClassForDomain:domain. |
|
3573 |
domainClass isNil ifTrue:[ |
|
3217 | 3574 |
^ self error:'invalid (unsupported) domain'. |
3059 | 3575 |
]. |
3576 |
^ domainClass. |
|
3577 |
! |
|
3578 |
||
1885 | 3579 |
type |
3580 |
"return the sockets connection type (i.e. #datagram, #stream etc)" |
|
3581 |
||
3582 |
^ socketType |
|
81 | 3583 |
! ! |
3584 |
||
126 | 3585 |
!Socket methodsFor:'socket setup'! |
3586 |
||
1885 | 3587 |
domain:domainArg type:typeArg |
3588 |
"set up socket with domain and type. |
|
3589 |
This is a low level entry; no binding, listening or connect |
|
3590 |
is done. Both arguments must be symbols from one of |
|
3059 | 3591 |
#AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp." |
126 | 3592 |
|
3137 | 3593 |
^ self domain:domainArg type:typeArg protocol:nil |
126 | 3594 |
! |
3595 |
||
1885 | 3596 |
domain:domainArg type:typeArg protocol:protocolNumber |
3597 |
"set up socket with domain, type and protocol number. |
|
3598 |
This is a low level entry; no binding, listening or connect |
|
3599 |
is done. Both arguments must be symbols from one of |
|
3059 | 3600 |
#AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp." |
1885 | 3601 |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3602 |
|domainName domainCode typeCode error newHandle| |
1885 | 3603 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3604 |
handle notNil ifTrue:[ |
4203 | 3605 |
^ self errorAlreadyOpen |
1885 | 3606 |
]. |
3607 |
domainName := SocketAddress domainCodeFromName:domainArg. |
|
3608 |
domainCode := OperatingSystem domainCodeOf:domainName. |
|
3609 |
typeCode := OperatingSystem socketTypeCodeOf:typeArg. |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3610 |
newHandle := OperatingSystem socketAccessor new. |
1885 | 3611 |
%{ |
3612 |
#ifndef NO_SOCKET |
|
3613 |
FILE *fp; |
|
3137 | 3614 |
int dom, typ, proto = 0; |
1885 | 3615 |
int on = 1; |
3616 |
SOCKET sock; |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3617 |
int _fd; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3618 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3619 |
# ifdef __win32__ |
3123 | 3620 |
# ifndef WSA_FLAG_NO_HANDLE_INHERIT |
3621 |
# define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
|
3622 |
# endif |
|
3623 |
static int noInheritFlag = WSA_FLAG_NO_HANDLE_INHERIT; |
|
3624 |
# endif |
|
1885 | 3625 |
|
3626 |
if (! __isSmallInteger(domainCode)) { |
|
4203 | 3627 |
error = @symbol(badArgument1); |
3628 |
goto out; |
|
1885 | 3629 |
} |
3630 |
if (! __isSmallInteger(typeCode)) { |
|
4203 | 3631 |
error = @symbol(badArgument2); |
3632 |
goto out; |
|
1885 | 3633 |
} |
3634 |
if (protocolNumber != nil) { |
|
4203 | 3635 |
if (!__isSmallInteger(protocolNumber)) { |
3636 |
error = @symbol(badArgument3); |
|
3637 |
goto out; |
|
3638 |
} |
|
3639 |
proto = __intVal(protocolNumber); |
|
1885 | 3640 |
} |
3641 |
||
3642 |
/* |
|
3643 |
* get address and protocol-family |
|
3644 |
*/ |
|
3645 |
dom = __intVal(domainCode); |
|
3646 |
typ = __intVal(typeCode); |
|
3647 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3648 |
# ifdef __win32__ |
3123 | 3649 |
sock = WSASocket(dom, typ, proto, 0, 0, noInheritFlag); |
3650 |
if (sock == INVALID_SOCKET && noInheritFlag) { |
|
4203 | 3651 |
// tried to open socket with WSA_FLAG_NO_HANDLE_INHERIT |
3652 |
// This fails on older windows versions, e.g. Windows XP |
|
3653 |
sock = WSASocket(dom, typ, proto, 0, 0, 0); |
|
3654 |
if (sock != INVALID_SOCKET) { |
|
3655 |
// no error without WSA_FLAG_NO_HANDLE_INHERIT, |
|
3656 |
// never use this flag again! |
|
3657 |
noInheritFlag = 0; |
|
3658 |
} |
|
3123 | 3659 |
} |
3660 |
if (sock == INVALID_SOCKET) { |
|
4203 | 3661 |
errno = WSAGetLastError(); |
3123 | 3662 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3663 |
# else // !__win32__ |
3123 | 3664 |
|
1885 | 3665 |
sock = socket(dom, typ, proto); |
3123 | 3666 |
# if defined(EPROTONOSUPPORT) /* for SGI */ |
3667 |
if ((sock < 0) && (proto != 0) && (errno == EPROTONOSUPPORT)) { |
|
4203 | 3668 |
DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n")); |
3669 |
proto = 0; |
|
3670 |
sock = socket(dom, typ, 0); |
|
1885 | 3671 |
} |
3672 |
# endif |
|
3123 | 3673 |
if (sock < 0) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3674 |
# endif // !__win32__ |
3123 | 3675 |
|
4203 | 3676 |
DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno)); |
3677 |
error = __MKSMALLINT(errno); |
|
1885 | 3678 |
} else { |
3123 | 3679 |
# if defined(SET_LINGER_WHEN_CREATING_SOCKET) && defined(SO_LINGER) |
4203 | 3680 |
{ |
3681 |
struct linger l; |
|
3682 |
||
3683 |
l.l_onoff = 1; |
|
3684 |
l.l_linger = 30; |
|
3685 |
setsockopt(sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
|
3686 |
} |
|
1885 | 3687 |
# endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3688 |
# ifdef __win32__ |
4203 | 3689 |
/* |
3690 |
* make it blocking |
|
3691 |
*/ |
|
3692 |
{ |
|
3693 |
unsigned long zero = 0; |
|
3694 |
ioctlsocket(sock, FIONBIO, &zero); |
|
3695 |
} |
|
3696 |
{ |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3697 |
# if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ )) |
4203 | 3698 |
/* |
3699 |
* make it a FILE * |
|
3700 |
*/ |
|
3701 |
__stxWrapApiEnterCritical(); |
|
3702 |
_fd = _open_osfhandle((long)sock, 0); |
|
3703 |
__stxWrapApiLeaveCritical(); |
|
2773 | 3704 |
# else |
4203 | 3705 |
_fd = (int)sock; |
2773 | 3706 |
# endif |
4203 | 3707 |
DBGPRINTF(("SOCKET: sock=%d fd=%d\n", sock, _fd)); |
3708 |
} |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3709 |
# else // !__win32__ |
4203 | 3710 |
fp = fdopen(sock, "r+"); |
3711 |
if (! fp) { |
|
3712 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
|
3713 |
error = __MKSMALLINT(errno); |
|
3714 |
__BEGIN_INTERRUPTABLE__ |
|
3715 |
closesocket(sock); |
|
3716 |
DBGFPRINTF((stderr, "SOCKET: fdopen failed (%d)\n", sock)); |
|
3717 |
__END_INTERRUPTABLE__ |
|
3718 |
goto out; |
|
3719 |
} |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3720 |
# endif // !__win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3721 |
|
4203 | 3722 |
if (@global(FileOpenTrace) == true) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3723 |
# ifdef __win32__ |
4203 | 3724 |
console_fprintf(stderr, "fdopen [Socket create] -> fd: %d (H: %"_lx_")\n", (INT)_fd, (INT)sock); |
3223 | 3725 |
# else |
4203 | 3726 |
console_fprintf(stderr, "fdopen [Socket] -> %"_lx_" (fd: %d)\n", (INT)fp, sock); |
3223 | 3727 |
# endif |
4203 | 3728 |
} |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3729 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3730 |
# ifdef __win32__ |
4203 | 3731 |
__externalAddressVal(newHandle) = _fd; |
3732 |
__INST(handleType) = @symbol(socketHandle); |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3733 |
# else |
4203 | 3734 |
__externalAddressVal(newHandle) = fp; |
3735 |
__INST(handleType) = @symbol(socketFilePointer); |
|
1885 | 3736 |
# endif |
3737 |
} |
|
3738 |
#endif |
|
3739 |
out:; |
|
3740 |
%}. |
|
3741 |
||
3742 |
"all ok?" |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3743 |
handleType notNil ifTrue:[ |
4203 | 3744 |
handle := newHandle. |
3745 |
domain := domainArg. |
|
3746 |
socketType := typeArg. |
|
3747 |
self registerForFinalization. |
|
3748 |
^ self. |
|
1885 | 3749 |
]. |
3217 | 3750 |
error isInteger ifTrue:[ |
4203 | 3751 |
lastErrorNumber := error. |
3752 |
^ self openError:error. |
|
3217 | 3753 |
]. |
3754 |
^ self primitiveFailed:error. |
|
126 | 3755 |
|
3756 |
" |
|
3059 | 3757 |
Socket new domain:#AF_INET type:#stream |
3758 |
Socket new domain:#AF_UNIX type:#stream |
|
126 | 3759 |
" |
28 | 3760 |
! ! |
3761 |
||
4203 | 3762 |
|
28 | 3763 |
!Socket methodsFor:'specials'! |
3764 |
||
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3765 |
linger:anIntegerOrNil |
3224 | 3766 |
"set the linger behavior on close: |
3767 |
anIntegerOrNil == nil: close returns immediately, socket tries |
|
4172 | 3768 |
to send buffered data in background. |
3224 | 3769 |
anIntegerOrNil == 0: close returns immediately, bufferd data is discarded. |
3770 |
anIntegerOrNil > 0: close waits this many seconds for buffered data |
|
4172 | 3771 |
to be delivered, after this time buffered data is |
3772 |
discarded and close returns with an error" |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3773 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3774 |
^ self |
4172 | 3775 |
setSocketOption:#'SO_LINGER' |
3776 |
argument:anIntegerOrNil notNil |
|
3777 |
argument:anIntegerOrNil. |
|
3224 | 3778 |
! |
3779 |
||
1885 | 3780 |
receiveBufferSize |
3781 |
"get the send buffer size - for special applications only. |
|
3782 |
Not all operatingSystems offer this functionality |
|
3783 |
(returns nil, if unsupported)" |
|
3784 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3785 |
handle isNil ifTrue:[ |
1885 | 3786 |
^ self errorNotOpen |
1335 | 3787 |
]. |
1885 | 3788 |
%{ |
3789 |
#if defined(SO_RCVBUF) && defined(SOL_SOCKET) |
|
3790 |
{ |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3791 |
OBJ fp = __INST(handle); |
1885 | 3792 |
SOCKET sock; |
3793 |
int opt; |
|
3794 |
unsigned int size; |
|
3795 |
||
3796 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3797 |
if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&opt, &size) >= 0) { |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3798 |
RETURN( __MKSMALLINT(opt) ); |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3799 |
} |
1335 | 3800 |
} |
3801 |
#endif |
|
3802 |
%}. |
|
3803 |
^ nil |
|
3804 |
! |
|
3805 |
||
1885 | 3806 |
receiveBufferSize:size |
3807 |
"set the receive buffer size - for special applications only. |
|
3808 |
Not all operatingSystems offer this functionality |
|
3809 |
(returns false, if unsupported)" |
|
3810 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3811 |
handle isNil ifTrue:[ |
1885 | 3812 |
^ self errorNotOpen |
3813 |
]. |
|
3814 |
%{ |
|
3815 |
#if defined(SO_RCVBUF) && defined(SOL_SOCKET) |
|
3816 |
if (__isSmallInteger(size)) { |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3817 |
OBJ fp = __INST(handle); |
1885 | 3818 |
SOCKET sock; |
3819 |
int opt; |
|
3820 |
||
3821 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1343 | 3822 |
opt = __intVal(size); |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3823 |
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int)) >= 0 ) { |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3824 |
RETURN(true); |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3825 |
} |
1335 | 3826 |
} |
3827 |
#endif |
|
3828 |
%}. |
|
3829 |
^ false |
|
3830 |
! |
|
3831 |
||
1885 | 3832 |
receiveTimeout |
3833 |
"get the receive timeout in millis - for special applications only. |
|
3834 |
Not all operatingSystems offer this functionality |
|
3835 |
(returns nil, if unsupported)" |
|
3836 |
||
3837 |
|millis| |
|
3838 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3839 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3840 |
^ self errorNotOpen |
1885 | 3841 |
]. |
3842 |
%{ |
|
3843 |
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET) |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3844 |
OBJ fp = __INST(handle); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3845 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3846 |
int len; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3847 |
int __millis; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3848 |
struct timeval tv = {0, 0}; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3849 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3850 |
len = sizeof(struct timeval); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3851 |
if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, &len) == 0) { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3852 |
__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3853 |
millis = __mkSmallInteger(__millis); |
1885 | 3854 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3855 |
console_fprintf(stderr, "getsockopt -> s:%d us:%d -> millis:%d\n", tv.tv_sec, tv.tv_usec, __millis); |
1885 | 3856 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3857 |
} else { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3858 |
console_fprintf(stderr, "Socket [warning]: getsockopt %d failed; errno=%d\n", sock, errno); |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3859 |
} |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3860 |
#endif |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3861 |
%}. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3862 |
millis notNil ifTrue:[^ millis / 1000 ]. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3863 |
^ nil |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3864 |
! |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3865 |
|
1885 | 3866 |
receiveTimeout:seconds |
3867 |
"set the receive timeout - for special applications only. |
|
3868 |
Not all operatingSystems offer this functionality |
|
3869 |
(returns false, if unsupported)" |
|
3870 |
||
3871 |
|millis| |
|
3872 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3873 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3874 |
^ self errorNotOpen |
1885 | 3875 |
]. |
3876 |
millis := (seconds * 1000) rounded. |
|
3877 |
%{ |
|
3878 |
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET) |
|
3879 |
if (__isSmallInteger(millis)) { |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3880 |
OBJ fp = __INST(handle); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3881 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3882 |
int __millis = __intVal(millis); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3883 |
struct timeval tv = {0, 0}; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3884 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3885 |
tv.tv_sec = __millis / 1000; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3886 |
tv.tv_usec = (__millis % 1000) * 1000; |
1885 | 3887 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3888 |
console_fprintf(stderr, "setsockopt -> millis:%d -> s:%d us:%d \n", __millis, tv.tv_sec, tv.tv_usec); |
1885 | 3889 |
# endif |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3890 |
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) { |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3891 |
RETURN(true); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3892 |
} |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3893 |
console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno); |
28 | 3894 |
} |
3895 |
#endif |
|
71 | 3896 |
%}. |
28 | 3897 |
^ false |
81 | 3898 |
! |
3899 |
||
1885 | 3900 |
sendBufferSize |
3901 |
"get the send buffer size - for special applications only. |
|
3902 |
Not all operatingSystems offer this functionality |
|
3903 |
(returns nil, if unsupported)" |
|
3904 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3905 |
handle isNil ifTrue:[ |
1885 | 3906 |
^ self errorNotOpen |
3907 |
]. |
|
3908 |
%{ |
|
3909 |
#if defined(SO_SNDBUF) && defined(SOL_SOCKET) |
|
3910 |
{ |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3911 |
OBJ fp = __INST(handle); |
1885 | 3912 |
SOCKET sock; |
3913 |
int opt; |
|
3914 |
unsigned int size; |
|
3915 |
||
3916 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3917 |
if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&opt, &size) >= 0) { |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3918 |
RETURN( __MKSMALLINT(opt) ); |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3919 |
} |
1335 | 3920 |
} |
3921 |
#endif |
|
3922 |
%}. |
|
3923 |
^ nil |
|
3924 |
! |
|
3925 |
||
1885 | 3926 |
sendBufferSize:size |
3927 |
"set the send buffer size - for special applications only. |
|
3928 |
Not all operatingSystems offer this functionality |
|
3929 |
(returns false, if unsupported)" |
|
3930 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3931 |
handle isNil ifTrue:[ |
1885 | 3932 |
^ self errorNotOpen |
3933 |
]. |
|
3934 |
%{ |
|
3935 |
#if defined(SO_SNDBUF) && defined(SOL_SOCKET) |
|
3936 |
if (__isSmallInteger(size)) { |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3937 |
OBJ fp = __INST(handle); |
1885 | 3938 |
SOCKET sock; |
3939 |
int opt; |
|
3940 |
||
3941 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1343 | 3942 |
opt = __intVal(size); |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3943 |
if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int)) >= 0) { |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3944 |
RETURN(true); |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3945 |
} |
1335 | 3946 |
} |
3947 |
#endif |
|
3948 |
%}. |
|
3949 |
^ false |
|
3950 |
! |
|
3951 |
||
1885 | 3952 |
sendTimeout |
3953 |
"get the send timeout in millis - for special applications only. |
|
3954 |
Not all operatingSystems offer this functionality |
|
3955 |
(returns nil, if unsupported)" |
|
3956 |
||
3957 |
|millis| |
|
3958 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3959 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3960 |
^ self errorNotOpen |
1885 | 3961 |
]. |
3962 |
%{ |
|
3963 |
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET) |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3964 |
OBJ fp = __INST(handle); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3965 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3966 |
int len; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3967 |
int __millis; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3968 |
struct timeval tv = {0, 0}; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3969 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3970 |
len = sizeof(struct timeval); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3971 |
if (getsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, &len) == 0) { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3972 |
__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3973 |
millis = __mkSmallInteger(__millis); |
1885 | 3974 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3975 |
console_fprintf(stderr, "getsockopt -> s:%d us:%d -> millis:%d\n", tv.tv_sec, tv.tv_usec, __millis); |
1885 | 3976 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3977 |
} else { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3978 |
console_fprintf(stderr, "Socket [warning]: getsockopt %d failed; errno=%d\n", sock, errno); |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3979 |
} |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3980 |
#endif |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3981 |
%}. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3982 |
millis notNil ifTrue:[^ millis / 1000 ]. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3983 |
^ nil |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3984 |
! |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3985 |
|
1885 | 3986 |
sendTimeout:seconds |
3987 |
"set the send timeout - for special applications only. |
|
3988 |
Not all operatingSystems offer this functionality |
|
3989 |
(returns false, if unsupported)" |
|
3990 |
||
3991 |
|millis| |
|
3992 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3993 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3994 |
^ self errorNotOpen |
1885 | 3995 |
]. |
3996 |
millis := (seconds * 1000) rounded. |
|
3997 |
%{ |
|
3998 |
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET) |
|
3999 |
if (__isSmallInteger(millis)) { |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4000 |
OBJ fp = __INST(handle); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4001 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4002 |
int __millis = __intVal(millis); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4003 |
struct timeval tv = {0, 0}; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4004 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4005 |
tv.tv_sec = __millis / 1000; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4006 |
tv.tv_usec = (__millis % 1000) * 1000; |
1885 | 4007 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4008 |
console_fprintf(stderr, "setsockopt -> millis:%d -> s:%d us:%d \n", __millis, tv.tv_sec, tv.tv_usec); |
1885 | 4009 |
# endif |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4010 |
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) { |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4011 |
RETURN(true); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4012 |
} |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4013 |
console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno); |
126 | 4014 |
} |
4015 |
#endif |
|
4016 |
%}. |
|
4017 |
^ false |
|
1340 | 4018 |
! |
4019 |
||
1885 | 4020 |
setTCPCork:aBoolean |
4021 |
"enable/disable TCP_CORK (do-not-send-partial-frames) |
|
4022 |
For special applications only. |
|
4023 |
Not all OperatingSystems offer this functionality |
|
4024 |
(returns false, if unsupported)" |
|
4025 |
||
2551 | 4026 |
^ self setSocketOption:#'TCP_CORK' argument:aBoolean argument:nil. |
1885 | 4027 |
! |
4028 |
||
4029 |
setTCPNoDelay:aBoolean |
|
4030 |
"enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) |
|
4031 |
For special applications only. |
|
4032 |
Not all OperatingSystems offer this functionality |
|
4033 |
(returns false, if unsupported)" |
|
4034 |
||
2551 | 4035 |
^ self setSocketOption:#'TCP_NODELAY' argument:aBoolean argument:nil. |
126 | 4036 |
! ! |
82 | 4037 |
|
4203 | 4038 |
|
1935 | 4039 |
!Socket methodsFor:'waiting'! |
4040 |
||
4041 |
waitForNewConnectionOrDataOnAny:otherConnections timeout:timeoutSeconds |
|
4042 |
"suspend the current process, until either a new connection comes |
|
4043 |
in at the receiver, or data arrives on any of the otherConnections. |
|
4044 |
For a new connection, an accept is performed and the new socket is returned. |
|
4045 |
For an old connection, that socket is returned. |
|
4046 |
In any case, the caller gets a socket to operate on as return value, |
|
4032 | 4047 |
or nil, if a timeout occurred. |
1935 | 4048 |
This method implements the inner wait-primitive of a multi-connection |
4049 |
server application." |
|
4050 |
||
4051 |
||
4052 |
|wasBlocked sema| |
|
4053 |
||
4054 |
"first, a quick check if data is already available" |
|
4055 |
self canReadWithoutBlocking ifTrue:[ |
|
4032 | 4056 |
^ self accept. |
1935 | 4057 |
]. |
4058 |
otherConnections do:[:aConnection | |
|
4032 | 4059 |
aConnection canReadWithoutBlocking ifTrue:[ |
4060 |
^ aConnection |
|
4061 |
] |
|
1935 | 4062 |
]. |
4063 |
||
3281 | 4064 |
"check again - prevent incoming interrupts from disturbing our setup" |
4065 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1935 | 4066 |
[ |
4032 | 4067 |
sema := Semaphore new name:'multiReadWait'. |
4068 |
otherConnections do:[:aConnection | |
|
4069 |
Processor signal:sema onInput:(aConnection fileDescriptor). |
|
4070 |
]. |
|
4071 |
Processor signal:sema onInput:(self fileDescriptor). |
|
4072 |
timeoutSeconds notNil ifTrue:[ |
|
4073 |
Processor signal:sema afterSeconds:timeoutSeconds |
|
4074 |
]. |
|
4075 |
Processor activeProcess state:#ioWait. |
|
4076 |
sema wait. |
|
3281 | 4077 |
] ifCurtailed:[ |
4032 | 4078 |
sema notNil ifTrue:[Processor disableSemaphore:sema]. |
4079 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1935 | 4080 |
]. |
3281 | 4081 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1935 | 4082 |
|
4083 |
"see who it was ..." |
|
4084 |
self canReadWithoutBlocking ifTrue:[ |
|
4032 | 4085 |
^ self accept. |
1935 | 4086 |
]. |
4087 |
otherConnections do:[:aConnection | |
|
4032 | 4088 |
aConnection canReadWithoutBlocking ifTrue:[ |
4089 |
^ aConnection |
|
4090 |
] |
|
1935 | 4091 |
]. |
4092 |
||
4093 |
"none - a timeout" |
|
4094 |
^ nil |
|
4095 |
! |
|
4096 |
||
3550 | 4097 |
waitForNewConnectionWithTimeout:timeoutSecondsOrNil |
1935 | 4098 |
"suspend the current process, until a new connection comes |
4099 |
in at the receiver or a timeout occurs. |
|
4100 |
For a new connection, an accept is performed and the new socket is returned. |
|
4032 | 4101 |
Returns nil, if a timeout occurred. |
1935 | 4102 |
This method implements the inner wait-primitive of a single-connection |
4103 |
server application." |
|
4104 |
||
3633 | 4105 |
|newSock| |
4106 |
||
3550 | 4107 |
(self readWaitWithTimeout:timeoutSecondsOrNil) ifTrue:[ |
4032 | 4108 |
"a timeout occurred - no connection within timeout" |
4109 |
^ nil |
|
1935 | 4110 |
]. |
4180 | 4111 |
self isOpen ifFalse:[ |
4112 |
"socket has been closed while waiting" |
|
4113 |
^ nil. |
|
4114 |
]. |
|
3633 | 4115 |
newSock := self class new. |
4116 |
(newSock primAcceptOn:self blocking:false) ifFalse:[ |
|
4032 | 4117 |
"should raise an error here" |
4118 |
^ nil |
|
3633 | 4119 |
]. |
4120 |
^ newSock |
|
1935 | 4121 |
! ! |
4122 |
||
464 | 4123 |
!Socket class methodsFor:'documentation'! |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
4124 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
4125 |
version |
3590 | 4126 |
^ '$Header$' |
2292 | 4127 |
! |
4128 |
||
4129 |
version_CVS |
|
3590 | 4130 |
^ '$Header$' |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
4131 |
! ! |
3985 | 4132 |