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