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