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