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