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