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