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