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