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