Socket.st
author Claus Gittinger <cg@exept.de>
Tue, 28 Jan 2014 13:48:23 +0100
changeset 3151 4f0a7da06c69
parent 3150 302e72416eb5
child 3152 520112185633
permissions -rw-r--r--
class: Socket changed: #sendData: when blocking in a send, yield instead of spinning all during my timeslice
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
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
    30
//#define DEBUG 1
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
 */
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    52
# define CLOSESOCKET_AFTER_FCLOSE
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    53
# define xxCLOSESOCKET_BEFORE_FCLOSE
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    54
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    55
# ifdef __MINGW__
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    56
extern HANDLE _get_osfhandle();
1845
df4d79bfba9d closesocket is still tricky
Claus Gittinger <cg@exept.de>
parents: 1844
diff changeset
    57
# endif
df4d79bfba9d closesocket is still tricky
Claus Gittinger <cg@exept.de>
parents: 1844
diff changeset
    58
2915
896fffb0f02e mingw64
Claus Gittinger <cg@exept.de>
parents: 2909
diff changeset
    59
# if defined( __BORLANDC__ ) || defined( __MINGW__ )
896fffb0f02e mingw64
Claus Gittinger <cg@exept.de>
parents: 2909
diff changeset
    60
#  define SOCKET_FROM_FD(fd)               (SOCKET)(_get_osfhandle(fd))
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    61
# else
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    62
#  define SOCKET_FROM_FD(fd)               (fd)
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    63
# endif
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
    64
#else /* not WIN32 */
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
    65
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
    66
# define SOCKET_FROM_FD(fd)               (fd)
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
    67
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
    68
#endif /* WIN32 */
1503
4d97e4191d04 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1499
diff changeset
    69
1843
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
    70
#define SOCKET_FROM_FILE(f)             (SOCKET_FROM_FD(fileno(f)))
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
    71
#define SOCKET_FROM_FILE_OBJECT(f)      (SOCKET_FROM_FILE(__FILEVal(f)))
1539
c0e89dcb58b3 non blocking stuff done right here;
Claus Gittinger <cg@exept.de>
parents: 1538
diff changeset
    72
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
    73
#ifndef WIN32
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
    74
# define closesocket(sock)    close(sock)
1539
c0e89dcb58b3 non blocking stuff done right here;
Claus Gittinger <cg@exept.de>
parents: 1538
diff changeset
    75
# define SOCKET               int
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
    76
#endif
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
    77
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    78
#include <stdio.h>
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    79
#include <errno.h>
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    80
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    81
#ifdef WANT__TCP_DOT_H
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    82
# include <netinet/tcp.h>
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    83
#endif
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    84
1344
70d73917ac78 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1343
diff changeset
    85
#ifdef WANT__NETDB_DOT_H
70d73917ac78 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1343
diff changeset
    86
# include <netdb.h>
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    87
#endif
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    88
739
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    89
#if defined(TRY_AGAIN) || defined(HOST_NOT_FOUND)
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    90
# define USE_H_ERRNO
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    91
#endif
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    92
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    93
/*
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    94
 * on some systems errno is a macro ... check for it here
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    95
 */
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    96
#ifndef errno
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    97
 extern errno;
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    98
#endif
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    99
648
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   100
#ifdef USE_H_ERRNO
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   101
# ifndef h_errno
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   102
 extern h_errno;
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   103
# endif
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   104
#endif
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   105
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   106
#ifdef DEBUG
1839
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
   107
# define DBGPRINTF(x)    { if (__debugging__) console_printf x; }
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
   108
# define DBGFPRINTF(x)   { if (__debugging__) console_fprintf x; }
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   109
#else
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   110
# define DBGPRINTF(x)    /* as nothing */
821
40b0b69bf4b5 more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents: 820
diff changeset
   111
# define DBGFPRINTF(x)   /* as nothing */
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   112
#endif
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   113
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   114
#ifndef TRUE
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
   115
# define TRUE   1
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   116
#endif
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   117
#ifndef FALSE
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
   118
# define FALSE  0
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   119
#endif
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   120
#ifndef WIN32
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   121
typedef int BOOL;
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
1843
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   124
#ifdef WIN32
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   125
# undef stdout
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   126
# undef stderr
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   127
# define stdout __win32_stdout()
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   128
# define stderr __win32_stderr()
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   129
#endif
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   130
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   131
%}
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   132
! !
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   133
511
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   134
!Socket primitiveVariables!
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   135
%{
1844
decc9d62520d oops - debug
Claus Gittinger <cg@exept.de>
parents: 1843
diff changeset
   136
static int __debugging__ = 0;
511
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   137
%}
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   138
! !
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   139
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   140
!Socket primitiveFunctions!
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   141
%{
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   142
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   143
static int
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   144
setupBufferParameters(aDataBuffer, startIndex, p_extPtr, p_offs, p_objSize)
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   145
    OBJ aDataBuffer, startIndex;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   146
    char **p_extPtr;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   147
    int *p_offs;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   148
    int *p_objSize;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   149
{
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   150
	char *extPtr = 0;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   151
	int sIdx = 0, objSize = 0, offs = 0;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   152
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   153
	sIdx = 0;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   154
	if (__isSmallInteger(startIndex)) {
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   155
	    sIdx = __intVal(startIndex) - 1;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   156
	}
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   157
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   158
	if (__isExternalBytesLike(aDataBuffer)) {
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   159
	    OBJ sz;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   160
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   161
	    extPtr = (char *)(__externalBytesAddress(aDataBuffer));
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   162
	    sz = __externalBytesSize(aDataBuffer);
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   163
	    if (__isSmallInteger(sz)) {
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   164
		objSize = __intVal(sz);
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   165
	    } else {
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   166
		objSize = 0; /* unknown */
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   167
	    }
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   168
	    offs = sIdx;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   169
	} else {
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   170
	    OBJ oClass;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   171
	    int nInstVars, nInstBytes;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   172
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   173
	    extPtr = (char *)0;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   174
	    oClass = __Class(aDataBuffer);
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   175
	    switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   176
		case BYTEARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   177
		    offs = sIdx;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   178
		    break;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   179
		case WORDARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   180
		case SWORDARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   181
		    offs = sIdx * 2;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   182
		    break;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   183
		case LONGARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   184
		case SLONGARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   185
		    offs = sIdx * 4;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   186
		    break;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   187
		case LONGLONGARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   188
		case SLONGLONGARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   189
		    offs = sIdx * 8;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   190
# ifdef __NEED_LONGLONG_ALIGN
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   191
		    offs += 4;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   192
# endif
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   193
		    break;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   194
		case FLOATARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   195
		    offs = sIdx * sizeof(float);
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   196
		    break;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   197
		case DOUBLEARRAY:
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   198
		    offs = sIdx * sizeof(double);
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   199
# ifdef __NEED_DOUBLE_ALIGN
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   200
		    offs += 4;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   201
# endif
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   202
		    break;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   203
		default:
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   204
		    *p_objSize = -1;
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   205
		    return 0;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   206
	    }
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   207
	    nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   208
	    nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   209
	    offs = offs + nInstBytes;
2998
f3b0c290b01b stx macros begin with 2 underlines
Claus Gittinger <cg@exept.de>
parents: 2953
diff changeset
   210
	    objSize = __Size(aDataBuffer) - offs;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   211
	}
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   212
	*p_extPtr = extPtr;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   213
	*p_objSize = objSize;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   214
	*p_offs = offs;
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   215
	return 1;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   216
}
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   217
%}
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   218
! !
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   219
464
600b101a7035 alen in accept
Claus Gittinger <cg@exept.de>
parents: 463
diff changeset
   220
!Socket class methodsFor:'documentation'!
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   221
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   222
copyright
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   223
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   224
 COPYRIGHT (c) 1992 by Claus Gittinger
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   225
	      All Rights Reserved
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   226
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   227
 This software is furnished under a license and may be used
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   228
 only in accordance with the terms of that license and with the
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   229
 inclusion of the above copyright notice.   This software may not
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   230
 be provided or otherwise made available to, or used by, any
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   231
 other person.  No title to or ownership of the software is
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   232
 hereby transferred.
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   233
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   234
!
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   235
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   236
documentation
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   237
"
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
   238
    This class provides access to sockets for interprocess communication.
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   239
    The message protocol is preliminary, until someone tells me how
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   240
    other smalltalk's socket interfaces look like.
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   241
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   242
    Currently, only IP and UNIX domain sockets are really fully
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   243
    tested and supported.
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   244
    Code for appletalk is included, but was never tested ...
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   245
    More may be added in the future.
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   246
    (the code is prepared for things like SNA or decNet;
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   247
     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
   248
     Implementing those is pretty straight forward, once the address
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   249
     data structures are known.)
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
   250
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   251
    Due to historic reasons (I started this class, before I got hold of some
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   252
    code using ST-80 Sockets i.e. RemoteInvocation), there is some old interface
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   253
    still provided.
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
   254
    This will vanish; use the #family:type: or #newTCPxxx and #newUDPxxx interfaces,
71
claus
parents: 63
diff changeset
   255
    together with the bind/listen and accept calls,
claus
parents: 63
diff changeset
   256
    which are meant to be compatible to ST-80's UnixSocketAccessor interface.
claus
parents: 63
diff changeset
   257
claus
parents: 63
diff changeset
   258
    TODO: cleanup historic leftovers, implement other than inet domain stuff.
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   259
	  (mhmh - how can I test those ?)
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   260
	  change to raise more signals on errors.
259
6d36f3ac42a2 documentation
Claus Gittinger <cg@exept.de>
parents: 220
diff changeset
   261
6d36f3ac42a2 documentation
Claus Gittinger <cg@exept.de>
parents: 220
diff changeset
   262
    [author:]
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   263
	Claus Gittinger
48
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   264
"
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   265
!
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   266
48
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   267
examples
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   268
"
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   269
    example (get help info from an nntp server):
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   270
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   271
	|sock host|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   272
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   273
	host := OperatingSystem getEnvironment:'NNTPSERVER'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   274
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   275
	sock := Socket newTCPclientToHost:host port:'nntp'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   276
	sock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   277
	    self warn:'no nntp daemon is running'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   278
	    ^ self
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   279
	].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   280
	Transcript showCR:sock nextLine.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   281
	sock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   282
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   283
	sock nextPutAll:'HELP'; cr.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   284
	[:exit |
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   285
	    |line|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   286
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   287
	    line := sock nextLine.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   288
	    line = '.' ifTrue:[exit value:nil].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   289
	    Transcript showCR:line.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   290
	] loopWithExit.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   291
	sock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   292
									[exEnd]
209
2152fceba563 finger example
Claus Gittinger <cg@exept.de>
parents: 208
diff changeset
   293
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   294
209
2152fceba563 finger example
Claus Gittinger <cg@exept.de>
parents: 208
diff changeset
   295
    example (connect to finger daemon, get users entry):
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   296
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   297
	|sock entry|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   298
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   299
	sock := Socket newTCPclientToHost:'localhost' port:'finger'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   300
	sock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   301
	    self warn:'no finger daemon is running'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   302
	    ^ self
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   303
	].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   304
	sock useCRLF:true.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   305
	sock buffered:false.
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
	    Transcript showCR:'cannot connect to local finger daemon'
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   308
	] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   309
	    sock nextPutAll:(OperatingSystem getLoginName).
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   310
	    sock cr.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   311
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   312
	    entry := sock nextLine.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   313
	    Transcript showCR:entry.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   314
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   315
	    sock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   316
	]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   317
									[exEnd]
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   318
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   319
    example (connect to an ftp server):
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   320
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   321
	|sock|
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
	sock := Socket newTCPclientToHost:'www.exept.de' port:'ftp'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   324
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   325
	sock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   326
	Transcript showCR:sock nextLine.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   327
	sock nextPutAll:('USER ' , 'anonymous'); cr.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   328
	Transcript showCR:sock nextLine.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   329
	sock nextPutAll:('PASS ' , 'fooBar'); cr.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   330
	Transcript showCR:sock nextLine.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   331
	sock nextPutAll:'HELP'; cr.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   332
	[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   333
	    |line|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   334
	    line := sock nextLine.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   335
	    Transcript showCR:line.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   336
	    (line at:4) = $-
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   337
	] whileTrue.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   338
	sock close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   339
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   340
	'dont know enough of the ftp protocol to continue here ...'
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   341
									[exEnd]
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   342
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   343
93
Claus Gittinger <cg@exept.de>
parents: 92
diff changeset
   344
    example (connect to an snmp server [UDP]):
2824
b00dc482dd03 comment/format in: #examples
Stefan Vogel <sv@exept.de>
parents: 2823
diff changeset
   345
    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
   346
	  being fixed.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   347
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   348
	|sock port|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   349
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   350
	sock := Socket newUDP.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   351
	port := Socket portOfService:'snmp'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   352
	sock connectTo:'localhost' port:port.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   353
	sock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   354
	Transcript showCR:'got it'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   355
	sock close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   356
									[exEnd]
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   357
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   358
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   359
    example (await connection from a client and read some data):
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   360
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   361
	|connectSock sock|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   362
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   363
	connectSock := Socket newTCPserverAtPort:9998.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   364
	connectSock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   365
	    Transcript showCR:'socket setup failed.'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   366
	] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   367
	    Transcript showCR:'listen ..'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   368
	    (connectSock listenFor:5) ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   369
		Transcript showCR:'listen failed.'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   370
	    ] ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   371
		Transcript showCR:'wait'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   372
		connectSock readWait.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   373
		Transcript showCR:'accept'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   374
		sock := connectSock accept.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   375
		sock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   376
		    Transcript showCR:'accept failed.'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   377
		] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   378
		    sock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   379
		    Transcript showCR:'server: got it'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   380
		    'can now do transfer via sock'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   381
		    Transcript showCR:'read'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   382
		    Transcript showCR:('got: ' , sock nextLine).
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   383
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   384
		    Transcript showCR:'close'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   385
		    sock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   386
		].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   387
		connectSock close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   388
	    ]
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
									[exEnd]
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   391
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   392
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   393
    example (connect to above server and send some data):
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   394
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   395
	|sock|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   396
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   397
	sock := Socket newTCPclientToHost:'localhost' port:9998.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   398
	sock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   399
	    Transcript showCR:'nope'
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   400
	] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   401
	    sock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   402
	    Transcript showCR:'client: got it'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   403
	    'can now do transfer via sock'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   404
	    Transcript showCR:'sending <hello>'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   405
	    sock nextPutLine:'hello'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   406
	    sock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   407
	]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   408
									[exEnd]
117
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   409
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   410
    example: UNIX domain socket (await connection from a client and read some data):
1009
e0839f923a3c double underlines;
Claus Gittinger <cg@exept.de>
parents: 1007
diff changeset
   411
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   412
	|connectSock sock|
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
	'/tmp/ud_socket' asFilename remove.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   415
	connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   416
	connectSock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   417
	    Transcript showCR:'socket setup failed.'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   418
	] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   419
	    Transcript showCR:'listen ..'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   420
	    (connectSock listenFor:5) ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   421
		Transcript showCR:'listen failed.'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   422
	    ] ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   423
		Transcript showCR:'wait'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   424
		connectSock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   425
		connectSock readWait.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   426
		Transcript showCR:'accept'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   427
		sock := connectSock accept.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   428
		sock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   429
		    Transcript showCR:'accept failed.'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   430
		] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   431
		    sock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   432
		    Transcript showCR:'server: got it'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   433
		    'can now do transfer via sock'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   434
		    Transcript showCR:'read'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   435
		    Transcript showCR:('got: ' , sock nextLine).
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   436
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   437
		    Transcript showCR:'close'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   438
		    sock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   439
		].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   440
		connectSock close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   441
	    ]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   442
	]
117
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   443
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   444
118
142d5c273454 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 117
diff changeset
   445
    example (connect to above server and send some data;
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   446
	     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
   447
		     (at least on LINUX), since the OS does not correctly handle
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   448
		     a connect from within an interrupted accept system call
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   449
		     On SGI's SVR4, this works ok
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   450
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   451
	|sock|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   452
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   453
	sock := Socket newUNIXclientTo:'/tmp/ud_socket'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   454
	sock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   455
	    Transcript showCR:'nope'
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   456
	] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   457
	    sock buffered:false.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   458
	    Transcript showCR:'client: got it'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   459
	    'can now do transfer via sock'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   460
	    Transcript showCR:'sending <hello>'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   461
	    sock nextPutLine:'hello'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   462
	    sock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   463
	]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   464
									[exEnd]
1007
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   465
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   466
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   467
    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
   468
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   469
	|udpSock sock addr n dataBuffer|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   470
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   471
	udpSock := Socket newUDPserverAtPort:9999.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   472
	udpSock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   473
	    Transcript showCR:'socket setup failed.'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   474
	] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   475
	    Transcript showCR:'wait'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   476
	    udpSock readWait.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   477
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   478
	    addr := IPSocketAddress new.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   479
	    dataBuffer := ByteArray new:1000.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   480
	    n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   481
	    n > 0 ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   482
		Transcript showCR:('got: ' , n printString , 'bytes  from ' , addr printString).
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   483
		Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString).
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   484
	    ] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   485
		Transcript showCR:'read failed'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   486
	    ].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   487
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   488
	    Transcript showCR:'close'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   489
	    udpSock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   490
	]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   491
									[exEnd]
1007
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   492
    example (connect to above UDP server and send some data;
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   493
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   494
	|sock|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   495
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   496
	sock := Socket newUDP.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   497
	sock isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   498
	    Transcript showCR:'nope'
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   499
	] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   500
	    sock sendTo:(IPSocketAddress new hostName:'localhost' port:9999) buffer:'hello world'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   501
	    sock close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   502
	]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   503
									[exEnd]
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   504
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   505
    example: pingWalk (try to ping hosts on the local network)
2824
b00dc482dd03 comment/format in: #examples
Stefan Vogel <sv@exept.de>
parents: 2823
diff changeset
   506
    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
   507
	  which is disabled on most OS.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   508
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   509
	|myAddress list top hosts walkProcess port|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   510
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   511
	myAddress := OperatingSystem getNetworkAddresses
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   512
			keysAndValuesSelect:[:eachIFName :eachAddress|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   513
			    eachAddress isLocal not
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   514
			    and:[eachIFName = 'wlan0']
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   515
			].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   516
	myAddress := myAddress first hostAddress.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   517
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   518
	port := Socket portOfService:'echo'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   519
	port isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   520
	    self error:'dont know echo port'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   521
	    ^ self
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   522
	].
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
	top := StandardSystemView new.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   525
	top label:'PING net walk'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   526
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   527
	list := ScrollableView for:ListView in:top.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   528
	list origin:0.0@0.0 corner:1.0@1.0.
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 openAndWait.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   531
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   532
	walkProcess := [
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   533
	    |l low hi direction tryHostID dottedName hostName conn addr|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   534
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   535
	    l := SortedCollection new.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   536
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   537
	    ' only works with type C-net
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   538
	      the code below could simply do 1 to:254 do:[:hostID }
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   539
	      but, to probe likely hosts earlier, the probing is done
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   540
	      ping-pong like around my ip-address (assuming, that other machines
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   541
	      have numbers around my own)'.
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
	    low := hi := (myAddress at:4).
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   544
	    direction := 1.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   545
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   546
	    [low > 0 or:[hi < 255]] whileTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   547
		direction > 0 ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   548
		    hi := hi + 1.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   549
		    tryHostID := hi.
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
		] ifFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   552
		    low := low - 1.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   553
		    tryHostID := low.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   554
		    direction := 1.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   555
		].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   556
		(tryHostID between:1 and:254) ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   557
		    dottedName := (myAddress at:1) printString
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   558
				  , '.' , (myAddress at:2) printString
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   559
				  , '.' , (myAddress at:3) printString
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   560
				  , '.' , tryHostID printString.
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
		    top label:'PING net walk - trying ' , dottedName.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   563
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   564
		    top windowGroup withCursor:Cursor wait do:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   565
			conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   566
			conn notNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   567
			    addr := Socket ipAddressOfHost:dottedName.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   568
			    hostName := Socket hostWithIpAddress:addr.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   569
			    hostName isNil ifTrue:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   570
				hostName :='?'
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   571
			    ].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   572
			    l add:(dottedName paddedTo:15 with:Character space)
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   573
				   , ' '
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   574
				   , (hostName paddedTo:15 with:Character space)
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   575
				   , ' up & reachable'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   576
			    list list:l.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   577
			    conn close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   578
			]
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
		].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   581
	    ].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   582
	    top label:'PING reachable hosts'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   583
	] forkAt:(Processor userBackgroundPriority).
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   584
	walkProcess name:'ping net walker'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   585
									[exEnd]
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
	This example creates a simple UDP server that accepts
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   589
	single packets from anybody and broadcasts them to all
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   590
	clients that have connected so far.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   591
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   592
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   593
	| socket address buffer msgSize clients |
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   594
	clients := Set new.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   595
	address := IPSocketAddress new.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   596
	buffer := String new: 1024.
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
	socket := self newUDPserverAtPort: 6666.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   599
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   600
	Transcript showCR: 'server starting'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   601
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   602
	[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   603
	    [true] whileTrue: [
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   604
		(socket readWaitWithTimeoutMs: 200) ifFalse: [
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   605
		    msgSize := socket
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   606
			    receiveFrom: address
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   607
			    buffer: buffer
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   608
			    start: 1
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   609
			    for: buffer size.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   610
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   611
		    clients add: address copy.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   612
		    clients do: [ :clientAddress |
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   613
			    socket
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   614
				    sendTo: clientAddress
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   615
				    buffer: buffer
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   616
				    start: 1
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   617
				    for: msgSize]]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   618
	    ]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   619
	] ensure:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   620
	    Transcript showCR: 'server shutting down'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   621
	    socket close
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   622
	]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   623
									[exEnd]
817
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
   624
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
   625
   send a datagram to above server:
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   626
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   627
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   628
	| socket address buffer host msg |
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   629
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   630
	host := Dialog
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   631
		request: 'What is the name of the server''s host?'
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   632
		initialAnswer: 'localhost'.
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 := self newUDP.
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
	address := IPSocketAddress hostName: host port: 6666.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   637
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   638
	buffer := ByteArray new: 1000.
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
	    [(msg := Dialog request: 'Say something') isEmpty] whileFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   641
		| replySize stream |
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   642
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   643
		socket writeWait.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   644
		stream := buffer writeStream.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   645
		stream nextPutAll: msg.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   646
		socket sendTo:address buffer:buffer start:1 for:stream position.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   647
		socket readWait.
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
		replySize := socket receiveFrom:address buffer:buffer.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   650
		replySize > 0 ifTrue: [
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   651
		    Transcript cr; nextPutAll: 'Server acknowledged: '.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   652
		    Transcript show: ((buffer copyFrom: 1 to: replySize) asString)
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   653
		]
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
	] ensure: [socket close].
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   656
	Transcript cr
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   657
									[exEnd]
1316
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   658
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   659
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   660
   loopBack:
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   661
									[exBegin]
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   662
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   663
	|readerTask readingSocket writingSocket|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   664
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   665
	readingSocket := self newTCPserverAtPort:9999.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   666
	readerTask :=
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   667
	    [
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   668
		|connection|
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   669
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   670
		readingSocket listenFor:1.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   671
		connection := readingSocket accept.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   672
		readingSocket close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   673
		[connection atEnd] whileFalse:[
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   674
		    Transcript showCR:(connection nextLine).
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
		connection close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   677
	    ] fork.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   678
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   679
	Delay waitForSeconds:1.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   680
	writingSocket := self newTCPclientToHost:'localhost' port:9999.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   681
	writingSocket nextPutLine:'Hello'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   682
	writingSocket nextPutLine:'World'.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   683
	writingSocket close.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
   684
									[exEnd]
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   685
"
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   686
! !
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   687
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
   688
!Socket class methodsFor:'instance creation'!
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   689
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   690
bindTo:aSocketAddress type:aTypeSymbol
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   691
    "create a socket for a specific type
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   692
     and bind it to aSocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   693
     Type must be:
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   694
        #stream, #datagram or #raw
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   695
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   696
     Neither connect nor connect-wait is done."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   697
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   698
    |newSock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   699
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   700
    newSock := self domain:aSocketAddress domain type:aTypeSymbol.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   701
    [
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   702
        newSock bindTo:aSocketAddress reuseAddress:true.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   703
    ] ifCurtailed:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   704
        newSock close.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   705
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   706
    ^ newSock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   707
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   708
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   709
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   710
        Socket bindTo:(IPSocketAddress anyHost port:8081) type:#stream.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   711
        Socket bindTo:(IPv6SocketAddress anyHost port:8081) type:#datagram.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   712
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   713
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   714
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   715
domain:domainSymbol type:type
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   716
    "create a socket for domain and type -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   717
     neither any connect nor binding is done.
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   718
     Domain must be one of the symbols:
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   719
        #inet, #unix, #appletalk, #decnet, #xns, ...;
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
   720
     Type must be:
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   721
        #stream, #datagram or #raw
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   722
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   723
     XXX: currently only the #AF_INET and #AF_INET6 and #AF_UNIX domains are supported"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   724
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   725
    ^ self new domain:domainSymbol type:type
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   726
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   727
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   728
     Socket domain:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   729
     Socket domain:#AF_INET type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   730
     Socket domain:#AF_INET6 type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   731
     Socket domain:#AF_INET6 type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   732
     Socket domain:#AF_UNIX type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   733
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   734
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   735
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   736
new
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   737
    "create a TCP socket"
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   738
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   739
    ^ super new buffered:false
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   740
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   741
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   742
newTCP
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   743
    "create a TCP socket - no binding or other setup is done,
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   744
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   745
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   746
    ^ self new domain:#'AF_INET' type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   747
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   748
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   749
      Socket newTCP
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   750
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   751
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   752
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   753
newTCP:aServiceOrNil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   754
    "create a TCP socket for a service -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   755
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   756
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   757
    |newSock socketAddress socketAddressClass|
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   758
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   759
    newSock := self newTCP.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   760
    (newSock notNil and:[aServiceOrNil notNil]) ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   761
        [
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   762
            socketAddressClass := newSock socketAddressClass.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   763
            socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   764
            socketAddress hostAddress:socketAddressClass anyAddress.
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
   765
            newSock bindTo:socketAddress reuseAddress:true.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   766
        ] ifCurtailed:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   767
            newSock close.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   768
        ]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   769
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   770
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   771
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   772
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   773
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   774
        Socket newTCP:'http-alt'.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   775
        Socket newTCP:9996.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   776
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   777
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   778
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   779
newTCPclientToAddress:aHostAddress port:aService
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   780
    "create a new TCP client socket connecting to a service.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   781
     Return a socket instance if ok, nil on failure.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   782
     Block until a connection is established (but only the current thread;
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   783
     not the whole smalltalk).
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   784
     See also: #newTCPclientToAddress:port:withTimeout:"
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   785
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   786
    ^ self newTCPclientToAddress:aHostAddress port:aService withTimeout:nil
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   787
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   788
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   789
newTCPclientToAddress:aSocketAddressOrByteArray port:aService withTimeout:millis
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   790
    "create a new TCP client socket connecting to a service.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   791
     Return a socket instance if ok, nil on failure.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   792
     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
   793
     and return nil."
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   794
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   795
    |port socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   796
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   797
    aService isString ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   798
        port := self portOfService:aService protocol:#tcp.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   799
    ] ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   800
        port := aService.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   801
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   802
    socketAddress := aSocketAddressOrByteArray isSocketAddress 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   803
                        ifTrue:[aSocketAddressOrByteArray]
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   804
                        ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   805
                            "Passing ByteArrays is obsolete and only supported for IPv4"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   806
                            IPSocketAddress hostAddress:aSocketAddressOrByteArray
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   807
                        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   808
    port notNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   809
        socketAddress port:port.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   810
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   811
    ^ self newTCPclientToAddress:socketAddress withTimeout:millis
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   812
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   813
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   814
newTCPclientToAddress:aSocketAddress withTimeout:millis
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   815
    "create a new TCP client socket connecting to a service.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   816
     Return a socket instance if ok, nil on failure.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   817
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   818
     and return nil."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   819
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   820
    |socket|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   821
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   822
    socket := self domain:aSocketAddress domain type:#stream.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   823
    (socket connectTo:aSocketAddress withTimeout:millis) ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   824
        socket close.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   825
        ^ nil
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   826
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   827
    ^ socket.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   828
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   829
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   830
        self newTCPclientToAddress:(IPv6SocketAddress hostName:'www.exept.de' port:80) withTimeout:nil
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   831
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   832
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   833
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   834
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   835
    "create a new TCP client socket connecting to a service.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   836
     If hostNameOrAddress is a string, try all the resolved adresses regardless
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   837
     whether fpr IPv4 or IPv6.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   838
     Return a socket instance if ok, nil on failure.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   839
     Block until a connection is established (but only the current thread;
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   840
     not the whole smalltalk).
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   841
     See also: #newTCPclientToHost:port:withTimeout:"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   842
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   843
    ^ self newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:nil withTimeout:nil
126
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
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   846
      Socket newTCPclientToHost:'www.exept.de' port:'http'
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   847
    "
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
    "Created: 31.10.1995 / 18:54:11 / cg"
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
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   852
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil withTimeout:millis
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   853
    "create a new TCP client socket connecting to a service on hostNameOrAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   854
     If hostNameOrAddress is a string, try all the resolved adresses.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   855
     Return a socket instance if ok, nil on failure.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   856
     Set aDomainSymbolOrNil to #AF_INET of #AF_INET6 to connect via a defined protocol.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   857
     Set aDomainSymbolOrNil to nil, to try all protocols.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   858
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   859
     and return nil."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   860
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   861
    |socket addressInfoList|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   862
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   863
    hostNameOrAddress isString ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   864
        ^ self newTCPclientToAddress:hostNameOrAddress port:aPortOrServiceName withTimeout:millis.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   865
    ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   866
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   867
    addressInfoList := SocketAddress 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   868
                            getAddressInfo:hostNameOrAddress 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   869
                            serviceName:aPortOrServiceName 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   870
                            domain:aDomainSymbolOrNil 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   871
                            type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   872
                            protocol:nil 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   873
                            flags:0.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   874
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   875
    addressInfoList do:[:eachAddressInfo|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   876
        |domainSymbol lastDomainSymbol|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   877
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   878
        domainSymbol := eachAddressInfo domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   879
        domainSymbol ~~ lastDomainSymbol ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   880
            socket notNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   881
                socket close.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   882
            ].
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
   883
            socket := self new domain:domainSymbol type:#stream.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   884
            lastDomainSymbol := domainSymbol.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   885
        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   886
        (socket connectTo:eachAddressInfo socketAddress withTimeout:millis) ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   887
            ^ socket.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   888
        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   889
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   890
    socket notNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   891
        socket close.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   892
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   893
    ^ nil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   894
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   895
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   896
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET' withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   897
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET6' withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   898
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:nil withTimeout:1000.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   899
      Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:1000
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   900
    "
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
   901
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
   902
    "Modified: / 16.1.1998 / 09:47:06 / stefan"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   903
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   904
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   905
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName withTimeout:millis 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   906
    "create a new TCP client socket connecting to a service on hostNameOrAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   907
     If hostNameOrAddress is a string, try all the resolved adresses regardless
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   908
     whether fpr IPv4 or IPv6.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   909
     Return a socket instance if ok, nil on failure.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   910
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   911
     and return nil."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   912
    
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   913
    ^ self 
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   914
        newTCPclientToHost:hostNameOrAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   915
        port:aPortOrServiceName
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   916
        domain:self defaultIpDomainForConnect
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   917
        withTimeout:millis
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   918
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   919
506
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   920
newTCPserverAtAnonymousPort
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   921
    "create a new TCP server socket providing service on
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   922
     a new anonymous port. The portNr is assigned by the OS."
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   923
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   924
    ^ self newTCPserverAtPort:0
506
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   925
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   926
!
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   927
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   928
newTCPserverAtPort:aService
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   929
    "create a new TCP server socket providing service."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   930
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   931
    ^ self newTCP:aService
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   932
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   933
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   934
newUDP
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   935
    "create a UDP socket - no binding or other setup is done,
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   936
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   937
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   938
    ^ self new domain:#'AF_INET' type:#datagram
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
    "Socket newUDP"
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
newUDP:aServiceOrNil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   944
    "create a UDP socket for a service -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   945
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   946
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   947
    |newSock socketAddressClass socketAddress|
126
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
    newSock := self newUDP.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   950
    (newSock notNil and:[aServiceOrNil notNil]) ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   951
        [
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   952
            socketAddressClass := newSock socketAddressClass.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   953
            socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   954
            socketAddress hostAddress:socketAddressClass anyAddress.
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
   955
            newSock bindTo:socketAddress reuseAddress:true.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   956
        ] ifCurtailed:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   957
            newSock close.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   958
        ]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   959
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   960
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   961
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   962
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   963
        Socket newUDP:4444.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   964
        Socket newUDP:'activesync'.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   965
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   966
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   967
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   968
newUDPserverAtPort:aService
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   969
    "create a new UDP server socket providing service."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   970
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   971
    ^ self newUDP:aService
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   972
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   973
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   974
newUNIX
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   975
    "create a UNIX domain socket - no binding or other setup is done,
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   976
     neither connect nor connect-wait is done.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   977
     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
   978
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   979
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   980
    ^ self new domain:#'AF_UNIX' type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   981
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   982
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   983
     Socket newUNIX
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   984
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   985
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   986
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   987
newUNIXclientTo:pathName
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   988
    "create a new UNIX client socket connecting to a pathname.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   989
     Return a socket instance if ok, nil on failure.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   990
     Block until a connection is established (but only the current thread;
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   991
     not the whole smalltalk).
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   992
     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
   993
     return nil.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   994
     See also: #newUNIXclientTo:withTimeout:"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   995
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   996
    ^ self newUNIXclientTo:pathName withTimeout:nil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   997
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   998
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   999
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1000
newUNIXclientTo:pathName withTimeout:millis
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1001
    "create a new UNIX client socket connecting to a pathname.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1002
     Return a socket instance if ok, nil on failure.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1003
     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
  1004
     and return nil.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1005
     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
  1006
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1007
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1008
    |newSock|
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1009
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1010
    newSock := self newUNIX.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1011
    newSock notNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1012
        (newSock connectTo:(UDSocketAddress name:pathName) withTimeout:millis) ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1013
            newSock close.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1014
            ^ nil
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1015
        ]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1016
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1017
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1018
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1019
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1020
        Socket newUNIXclientTo:'/tmp/foo'
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1021
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1022
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1023
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1024
newUNIXserverAt:pathName
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1025
    "create a new UNIX server socket providing service at a pathname.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1026
     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
  1027
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1028
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1029
    ^ self bindTo:(UDSocketAddress name:pathName) type:#stream.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1030
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1031
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1032
     |s s2|
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1033
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1034
    '/tmp/foo' asFilename remove.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1035
     s := Socket newUNIXserverAt:'/tmp/foo'.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1036
     s listenFor:5.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1037
     s2 := s accept.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1038
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1039
! !
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1040
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1041
!Socket class methodsFor:'Compatibility-ST80'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1042
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1043
family:domainSymbol type:typeSymbol
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1044
    "create a socket for domain and type - ST80 simply uses a different name.
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1045
     Domain must be one of the symbols: #inet, #unix, #appletalk or #ns;
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1046
     Type must be #stream, #datagram or #raw."
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1047
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1048
    ^ self domain:domainSymbol type:typeSymbol
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1049
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1050
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1051
     Socket family:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1052
     Socket family:#AF_INET type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1053
     Socket family:#AF_UNIX type:#stream
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1054
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1055
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1056
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1057
getHostname
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1058
    "return the computers hostname string"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1059
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1060
    ^ OperatingSystem getHostName
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1061
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1062
    "Created: / 27.2.1998 / 02:32:17 / cg"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1063
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1064
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1065
sockStream
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1066
    "return the type code for stream sockets"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1067
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1068
    ^ #stream
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1069
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1070
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1071
!Socket class methodsFor:'Compatibility-Squeak'!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1072
2900
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1073
deadlineSecs:seconds
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1074
    ^ Timestamp now addSeconds:seconds
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1075
!
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1076
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1077
initializeNetwork
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1078
    "/ intentionally left blank here
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1079
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1080
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1081
standardTimeout
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1082
    ^ 30
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1083
!
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1084
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1085
wildcardPort
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1086
    ^ nil
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1087
! !
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1088
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1089
!Socket class methodsFor:'Compatibility-VW'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1090
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1091
AF_INET
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1092
    ^ #AF_INET
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1093
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1094
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1095
SOCK_STREAM
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1096
    ^ #SOCK_STREAM
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1097
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1098
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1099
!Socket class methodsFor:'Signal constants'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1100
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1101
brokenConnectionSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1102
    "return the signal used to tell broken connections.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1103
     Since in unix, this is the same as the broken pipe signal,
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1104
     return that one.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1105
     (for other Operatingsystems, this may change ..)"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1106
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1107
    ^ PipeStream brokenPipeSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1108
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1109
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1110
invalidArgumentsSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1111
    "dummy for compatibility"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1112
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1113
    ^ self errorSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1114
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1115
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1116
!Socket class methodsFor:'debugging'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1117
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1118
debug:aBoolean
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1119
    "turn on/off internal debugprints.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1120
     This method is for ST/X debugging only and
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1121
     may  be removed in later versions"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1122
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1123
%{  /* NOCONTEXT */
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1124
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1125
    __debugging__ = (aBoolean == true);
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1126
%}
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1127
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1128
     Socket debug:true
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1129
     Socket debug:false
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1130
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1131
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1132
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1133
!Socket class methodsFor:'defaults'!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1134
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1135
defaultIpDomainForConnect
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1136
    "answer the domain used to look up host names for connect:
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1137
        #AF_INET    use only IPv4
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1138
        #AF_INET6   use only IPv6
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1139
        nil         use both IPv4 and IPv6"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1140
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1141
    ^ #AF_INET
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1142
! !
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1143
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1144
!Socket class methodsFor:'obsolete'!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1145
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1146
connectTo:service on:host
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1147
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1148
    "standard & easy client setup:
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1149
	create new client tcp socket, bind and connect;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1150
	return the socket.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1151
     The thread blocks (interruptable), until the connection is established."
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1152
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1153
    ^ self newTCPclientToHost:host port:(self portOfService:service).
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1154
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1155
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1156
     Socket connectTo:9995 on:'clam'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1157
     Socket connectTo:4711 on:'exept'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1158
     Socket connectTo:'finger' on:'clam'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1159
     Socket connectTo:'ftp' on:'exept'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1160
     Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER')
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1161
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1162
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1163
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1164
networkLongOrderIsMSB
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1165
    "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
  1166
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1167
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1168
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1169
    ^ true
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1170
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1171
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1172
networkShortOrderIsMSB
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1173
    "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
  1174
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1175
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1176
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1177
    ^ true
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1178
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1179
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1180
provide:aService
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1181
    "standard & easy server setup:
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1182
     create a new TCP server socket providing a service."
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1183
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1184
    <resource:#obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1185
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1186
    |newSock|
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1187
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1188
    self obsoleteMethodWarning:'use #newTCPserverAtPort: / #listen'.
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1189
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1190
    newSock := self newTCPserverAtPort:(self portOfService:aService).
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1191
    newSock notNil ifTrue:[
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1192
	newSock listenFor:5.
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1193
    ].
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1194
    ^ newSock
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1195
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1196
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1197
     Socket provide:9995
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1198
     (Socket provide:9996) accept
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1199
     Socket provide:'nntp'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1200
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1201
! !
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1202
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1203
!Socket class methodsFor:'obsolete host queries'!
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1204
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1205
appletalkAddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1206
    <resource: #obsolete>
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1207
    "return the APPLETALK address for a hostname as a byteArray,
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1208
     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
  1209
     followed by the node byte.
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1210
     If the host is unknown, return nil.
812
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1211
     This is the reverse operation to #hostWithAppletalkAddress:.
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1212
     WARNING: untested code - I have no appletalk to test this."
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1213
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1214
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1215
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1216
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1217
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1218
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1219
	    ^ (AppletalkSocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1220
	]
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1221
!
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1222
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1223
hostWithAppletalkAddress:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1224
    <resource: #obsolete>
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1225
    "return the hostname for an APPLETALK address.
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1226
     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
  1227
     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
  1228
     The last byte is the node number.
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1229
     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
  1230
     This is is the reverse operation to #appletalkAddressOfHost:.
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1231
     WARNING: untested code - I have no appletalk to test this."
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1232
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1233
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1234
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1235
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1236
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1237
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1238
	    ^ (AppletalkSocketAddress hostAddress:addrByteArray) hostName
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1239
	]
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1240
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1241
    "
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1242
     Socket appletalkAddressOfHost:'yourAppleHere'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1243
     Socket hostWithAppletalkAddress:#[1 2 3]
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1244
     "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1245
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1246
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1247
hostWithIpAddress:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1248
    <resource: #obsolete>
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1249
    "return the hostname for an IP (internet-) address.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1250
     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
  1251
     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
  1252
     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
  1253
     This is the reverse operation to #ipAddressOfHost:."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1254
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1255
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1256
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1257
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1258
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1259
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1260
	    ^ (IPSocketAddress hostAddress:addrByteArray) hostName
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1261
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1262
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1263
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1264
     Socket ipAddressOfHost:'clam'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1265
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1266
     Socket ipAddressOfHost:'porty'
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1267
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty')
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1268
     Socket hostWithIpAddress:#[1 2 3 4]
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1269
     Socket hostWithIpAddress:#[127 0 0 1]
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1270
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'1.2.3.4')
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1271
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'www.altavista.com')
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1272
     "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1273
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1274
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1275
hostWithIpV6Address:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1276
    <resource: #obsolete>
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1277
    "return the hostname for an IPv6 (internet-) address.
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1278
     The address is supposed to be a byteArray consisting ??? bytes,
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1279
     the network bytes come first (no matter what the local byteorder is).
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1280
     Nil is returned for an unknown host or if its not an internet host.
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1281
     This is the reverse operation to #ipV6AddressOfHost:."
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1282
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1283
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1284
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1285
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1286
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1287
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1288
	    ^ (IPv6SocketAddress hostAddress:addrByteArray) hostName
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1289
	]
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1290
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1291
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1292
     Socket ipV6AddressOfHost:'clam'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1293
     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1294
     Socket ipV6AddressOfHost:'porty'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1295
     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1296
     Socket hostWithIpV6Address:#[1 2 3 4 5 6 7 8 9 10 11 12 13 14]
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1297
     Socket ipV6AddressOfHost:'www.exept.de'
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1298
     "
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1299
!
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1300
6
claus
parents: 4
diff changeset
  1301
ipAddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1302
    <resource: #obsolete>
98
5e787ba2cb31 use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents: 96
diff changeset
  1303
    "return the IP (internet-) number for a hostname as a byteArray,
5e787ba2cb31 use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents: 96
diff changeset
  1304
     where the network bytes come first (no matter what the cpus byteOrder is).
5e787ba2cb31 use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents: 96
diff changeset
  1305
     If the host is unknown, return nil.
5e787ba2cb31 use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents: 96
diff changeset
  1306
     This is the reverse operation to #hostWithIpAddress:."
6
claus
parents: 4
diff changeset
  1307
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1308
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1309
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1310
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1311
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1312
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1313
	    ^ (IPSocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1314
	]
21
33eb5ffad09d *** empty log message ***
claus
parents: 8
diff changeset
  1315
71
claus
parents: 63
diff changeset
  1316
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1317
     Socket ipAddressOfHost:'localhost'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1318
     Socket ipAddressOfHost:'exept'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1319
     Socket ipAddressOfHost:'1.2.3.4'
1274
f257a1b16b4d use naemLookup from SocketAddress for ipv6
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
  1320
     Socket ipAddressOfHost:'193.15.16.17'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1321
     Socket ipAddressOfHost:'josef'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1322
     Socket ipAddressOfHost:'styx.com'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1323
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1324
     Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1')
71
claus
parents: 63
diff changeset
  1325
    "
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1326
!
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1327
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1328
ipV6AddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1329
    <resource: #obsolete>
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1330
    "return the IPv6 (internet-) number for a hostname as a byteArray,
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1331
     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
  1332
     If the host is unknown, return nil.
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1333
     This is the reverse operation to #hostWithIpV6Address:."
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1334
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1335
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1336
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1337
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1338
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1339
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1340
	    ^ (IPv6SocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1341
	]
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1342
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1343
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1344
     Socket ipV6AddressOfHost:'localhost'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1345
     Socket ipV6AddressOfHost:'exept'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1346
     Socket ipV6AddressOfHost:'exept.exept.de'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1347
     Socket ipV6AddressOfHost:'www.google.de'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1348
     Socket ipV6AddressOfHost:'1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1349
     Socket ipV6AddressOfHost:'josef'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1350
     Socket ipV6AddressOfHost:'styx.com'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1351
     Socket hostWithIpV6Address:(Socket ipV6AddressOfHost:'localhost')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1352
     Socket ipV6AddressOfHost:(Socket hostV6WithIpAddress:'127.0.0.1')
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1353
    "
531
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1354
! !
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1355
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1356
!Socket class methodsFor:'queries'!
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1357
1260
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1358
peerFromDomain:domain name:peerName port:port
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1359
    |addrClass|
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1360
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1361
    addrClass := self socketAddressClassForDomain:domain.
1263
23161346a162 no longer callBack into sockets getHostName/getAddress code.
Claus Gittinger <cg@exept.de>
parents: 1260
diff changeset
  1362
    ^ addrClass hostName:peerName serviceName:port type:nil
1187
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1363
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1364
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1365
peerNameFromDomain:domain peer:peer
1260
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1366
    |addrClass|
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1367
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1368
    addrClass := self socketAddressClassForDomain:domain.
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1369
    ^ addrClass peerNameFromPeer:peer
1187
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1370
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1371
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1372
portOfService:aNameOrNumber
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1373
    "returns the port-number for a given IP-service
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1374
     or nil if no such service exists;
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1375
     - used to convert service names to portNumbers"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1376
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1377
    ^ self portOfService:aNameOrNumber protocol:nil
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1378
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1379
    "
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1380
     Socket portOfService:'finger'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1381
     Socket portOfService:'nntp'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1382
     Socket portOfService:'echo'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1383
     Socket portOfService:'snmp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1384
    "
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1385
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1386
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1387
portOfService:aNameOrNumber protocol:aProtocol
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1388
    "returns the port-number for a given IP-service
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1389
     or nil if no such service exists;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1390
     - used to convert service names to portNumbers"
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1391
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  1392
%{ /* UNLIMITEDSTACK(noWIN32) */
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1393
#ifndef NO_SOCKET
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1394
    struct servent *servent = NULL;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1395
    char *protocol;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1396
    int tryBoth = 0;
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1397
    short portNo;
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1398
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
  1399
    if (__isSmallInteger(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1400
	RETURN ( aNameOrNumber );
92
Claus Gittinger <cg@exept.de>
parents: 91
diff changeset
  1401
    }
Claus Gittinger <cg@exept.de>
parents: 91
diff changeset
  1402
2331
b5280e1b3b9f changed:
Stefan Vogel <sv@exept.de>
parents: 2327
diff changeset
  1403
    if (__isStringLike(aProtocol)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1404
	protocol = (char *)__stringVal(aProtocol);
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
  1405
    } else {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1406
	protocol = "tcp";
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1407
	tryBoth = 1;
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1408
    }
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1409
2331
b5280e1b3b9f changed:
Stefan Vogel <sv@exept.de>
parents: 2327
diff changeset
  1410
    if (__isStringLike(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1411
	servent = getservbyname((char *) __stringVal(aNameOrNumber), protocol);
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1412
	if (servent != NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1413
	    RETURN ( __MKSMALLINT(ntohs(servent->s_port)) );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1414
	}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1415
	if (tryBoth) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1416
	    servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1417
	    if (servent != NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1418
		RETURN ( __MKSMALLINT(ntohs(servent->s_port)) );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1419
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1420
	}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1421
	RETURN ( nil );
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1422
    }
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1423
#endif
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1424
    RETURN ( nil );
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1425
%}
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1426
    "
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1427
     Socket portOfService:'echo' protocol:'udp'
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1428
     Socket portOfService:'echo' protocol:'tcp'
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1429
    "
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1430
!
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1431
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1432
protocolOfService:aNameOrNumber
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1433
    "returns the protocol (as string) for a given IP-service
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1434
     or nil if no such service exists."
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1435
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1436
%{  /* UNLIMITEDSTACK(noWIN32) */
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1437
#ifndef NO_SOCKET
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1438
    struct servent *servent = NULL;
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1439
    short portNo;
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1440
58
bd6753bf0401 *** empty log message ***
claus
parents: 51
diff changeset
  1441
    if (__isSmallInteger(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1442
	portNo = __intVal(aNameOrNumber);
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1443
	servent = getservbyport(htons(portNo), "tcp") ;
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1444
	if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1445
	    servent = getservbyport(htons(portNo), "udp") ;
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1446
	    if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1447
		RETURN ( nil );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1448
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1449
	}
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1450
    } else {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1451
	if (__isStringLike(aNameOrNumber)) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1452
	    servent = getservbyname((char *) __stringVal(aNameOrNumber), "tcp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1453
	    if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1454
		servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1455
		if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1456
		    RETURN ( nil );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1457
		}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1458
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1459
	}
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1460
    }
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1461
    if (servent) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1462
	RETURN ( __MKSTRING(servent->s_proto) );
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1463
    }
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1464
#endif /* !NO_SOCKET */
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1465
    RETURN ( nil );
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1466
%}
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1467
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1468
     Socket protocolOfService:'finger'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1469
     Socket protocolOfService:'nntp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1470
     Socket protocolOfService:'xxx'
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1471
     Socket protocolOfService:79
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1472
     Socket protocolOfService:'snmp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1473
    "
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1474
!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1475
1187
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1476
socketAddressClassForDomain:domain
1230
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1477
    ^ SocketAddress knownClassFromCode:domain
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1478
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1479
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1480
     self socketAddressClassForDomain:#AF_INET
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1481
     self socketAddressClassForDomain:#AF_UNIX
1230
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1482
    "
1187
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1483
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1484
532
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1485
supportedProtocolFamilies
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1486
    "return a collection of supported protocol families.
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1487
     This list specifies what the Socket class supports -
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1488
     socket creation may still fail, if your system was built
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1489
     without it."
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1490
1280
b973855bfe74 moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents: 1278
diff changeset
  1491
    ^ OperatingSystem supportedProtocolFamilies
532
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1492
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1493
    "
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1494
     Socket supportedProtocolFamilies
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1495
    "
559
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1496
!
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1497
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1498
typeOfProtocol:aProtocol
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1499
    "given a protocols name (i.e. tcp, udp etc) return the connection type.
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1500
     This method needs more ... - or is there a way to get this from the system ?"
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1501
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1502
    (aProtocol = 'tcp') ifTrue:[^ #stream].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1503
    (aProtocol = 'udp') ifTrue:[^ #datagram].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1504
    (aProtocol = 'ip')  ifTrue:[^ #raw].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1505
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1506
     unix domain
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1507
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1508
    (aProtocol = 'ud')  ifTrue:[^ #stream].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1509
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1510
     add x25 stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1511
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1512
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1513
     add appletalk stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1514
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1515
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1516
     add other stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1517
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1518
    ^ nil
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1519
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1520
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1521
     Socket typeOfProtocol:'tcp'
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1522
     Socket typeOfProtocol:'ucp'
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1523
     Socket typeOfProtocol:(Socket protocolOfService:'nntp')
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1524
     Socket typeOfProtocol:(Socket protocolOfService:'echo')
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1525
    "
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1526
! !
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1527
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1528
!Socket methodsFor:'Compatibility-Dolphin'!
1110
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1529
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1530
setReceiveTimeout: milliseconds
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1531
    self receiveTimeout:(milliseconds / 1000)
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1532
!
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1533
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1534
setSendTimeout: milliseconds
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1535
    self sendTimeout:(milliseconds / 1000)
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1536
! !
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1537
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1538
!Socket methodsFor:'Compatibility-ST80'!
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1539
887
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1540
acceptNonBlock
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1541
    ^ self accept
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1542
!
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1543
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1544
errorReporter
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1545
    "ST-80 mimicry."
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1546
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  1547
    ^ self class
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1548
!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1549
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1550
ioConnection
887
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1551
    ^ self
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1552
!
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1553
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1554
notReadySignal
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1555
    "ST-80 mimicry.
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1556
     for now - this is not yet raised"
71
claus
parents: 63
diff changeset
  1557
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1558
    ^ Signal new
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1559
!
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1560
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1561
readAppendStream
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1562
    "ST-80 mimicry.
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1563
     In ST-80, socket is not a stream, but referes to one.
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1564
     ST-80 code therefore uses 'Socket readWriteStream' to access
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1565
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1566
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1567
     this method returns the receiver, for transparency"
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1568
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1569
    ^ self
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1570
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1571
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1572
readStream
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1573
    "ST-80 mimicry.
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1574
     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
  1575
     ST-80 code therefore uses 'Socket readStream' to access
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1576
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1577
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1578
     this method returns the receiver, for transparency"
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1579
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1580
    ^ self
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1581
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1582
    "Created: 24.1.1997 / 23:52:57 / cg"
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1583
!
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1584
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1585
writeStream
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1586
    "ST-80 mimicry.
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1587
     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
  1588
     ST-80 code therefore uses 'Socket writeStream' to access
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1589
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1590
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1591
     this method returns the receiver, for transparency"
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1592
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1593
    ^ self
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1594
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1595
    "Created: 24.1.1997 / 10:34:35 / cg"
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1596
    "Modified: 24.1.1997 / 23:52:52 / cg"
71
claus
parents: 63
diff changeset
  1597
! !
claus
parents: 63
diff changeset
  1598
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1599
!Socket methodsFor:'Compatibility-Squeak'!
771
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1600
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1601
address
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1602
    ^ self getSocketAddress
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1603
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1604
2308
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1605
connectToHostNamed:hostName port:portNr
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1606
    self connectTo:hostName port:portNr
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1607
!
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1608
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1609
dataAvailable
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1610
    ^ self canReadWithoutBlocking
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1611
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1612
1886
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1613
destroy
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1614
    self close
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1615
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1616
    "Created: / 04-06-2007 / 21:29:03 / cg"
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1617
!
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1618
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1619
listenOn:aPortNr
1882
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1620
    self listenOn:aPortNr backlogSize:5
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1621
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1622
    "Modified: / 31-05-2007 / 17:59:53 / cg"
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1623
!
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1624
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1625
listenOn:aPortNr backlogSize:aNumber
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1626
    self bindTo:aPortNr address:nil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1627
    self listenFor:aNumber
1882
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1628
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1629
    "Created: / 31-05-2007 / 17:59:47 / cg"
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1630
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1631
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1632
peerName
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1633
    "return my peer (i.e. ipAddr + port);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1634
     May return nil if not yet setup completely."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1635
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1636
    ^ self getPeer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1637
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1638
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1639
primSocketLocalPort:aSocket
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1640
    ^ self port
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1641
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1642
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1643
sendData: aStringOrByteArray
3043
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1644
        "Send all of the data in the given array, even if it requires multiple calls to send it all.
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1645
         Return the number of bytes sent."
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1646
3150
302e72416eb5 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3139
diff changeset
  1647
        |remaining nWritten offset|
2953
855e7a58faa0 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2915
diff changeset
  1648
855e7a58faa0 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2915
diff changeset
  1649
"/ Transcript show:'>> '; showCR:aStringOrByteArray.
855e7a58faa0 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2915
diff changeset
  1650
3043
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1651
        remaining := aStringOrByteArray size.
3150
302e72416eb5 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3139
diff changeset
  1652
        offset := 1.
3043
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1653
        [remaining > 0] whileTrue:[
3150
302e72416eb5 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3139
diff changeset
  1654
            nWritten := self nextPutBytes:remaining from:aStringOrByteArray startingAt:offset.
3151
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1655
            nWritten == 0 ifTrue:[
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1656
                "/ seems to be blocking - give others a chance to do something useful
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1657
                "/ now (instead of spinning here until my timeslice ends)
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1658
                Processor yield
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1659
            ] ifFalse:[
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1660
                remaining := remaining - nWritten.
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1661
                offset := offset + nWritten.
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  1662
            ].
3043
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1663
        ].
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1664
        ^ aStringOrByteArray size.
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1665
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1666
        "An experimental version for use on slow lines:
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  1667
         Longer timeout and smaller writes to try to avoid spurious timeouts."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1668
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1669
"/        | bytesSent bytesToSend count |
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1670
"/        bytesToSend := aStringOrByteArray size.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1671
"/        bytesSent := 0.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1672
"/        [bytesSent < bytesToSend] whileTrue: [
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1673
"/                (self waitForSendDoneFor: 60)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1674
"/                        ifFalse: [ConnectionTimedOut signal: 'send data timeout; data not sent'].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1675
"/                count := self primSocket: socketHandle
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1676
"/                        sendData: aStringOrByteArray
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1677
"/                        startIndex: bytesSent + 1
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1678
"/                        count: (bytesToSend - bytesSent min: 5000).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1679
"/                bytesSent := bytesSent + count].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1680
"/
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1681
"/        ^ bytesSent
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1682
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1683
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1684
setOption: optionName value: optionValue
2899
a1f9a142d49b class: Socket
Claus Gittinger <cg@exept.de>
parents: 2891
diff changeset
  1685
    ^ self setSocketOption:(optionName asSymbol) argument:optionValue argument:nil
1887
f26d9b04244d changed #setOption:value:
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1686
f26d9b04244d changed #setOption:value:
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1687
    "Modified: / 04-06-2007 / 21:23:19 / cg"
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1688
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1689
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1690
socketHandle
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1691
    ^ self
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1692
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1693
2904
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1694
waitForConnectionUntil:aTimestamp
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1695
    "return true if connected"
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1696
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1697
    self readWaitWithTimeoutMs: (aTimestamp millisecondDeltaFrom:Timestamp now).
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1698
    ^ self isConnected
1886
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1699
!
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1700
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1701
waitForData
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1702
    self readWait
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1703
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1704
    "Created: / 04-06-2007 / 21:28:40 / cg"
771
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1705
! !
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1706
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1707
!Socket methodsFor:'accepting connections'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1708
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1709
accept
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1710
    "create a new TCP socket from accepting on the receiver.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1711
     This method will suspend the current process if no connection is waiting.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1712
     For ST-80 compatibility"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1713
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1714
    |newSock|
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
    self readWait.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1717
    newSock := self class new.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1718
    (newSock primAcceptOn:self blocking:false) ifFalse:[
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1719
	"should raise an error here"
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1720
	^ nil
1935
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
    ^ newSock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1723
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1724
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1725
     |sock newSock|
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
     sock := Socket provide:8004.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1728
     sock listenFor:5.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1729
     newSock := sock accept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1730
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1731
!
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
blockingAccept
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1734
    "create a new TCP socket from accepting on the receiver.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1735
     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
  1736
     For ST-80 compatibility"
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
    |newSock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1739
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1740
    newSock := self class new.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1741
    (newSock primAcceptOn:self blocking:true) ifFalse:[
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1742
	"should raise an error here"
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1743
	^ nil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1744
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1745
    ^ newSock
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
!Socket methodsFor:'binding'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1749
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1750
bindAnonymously
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1751
    "bind to any address. A free port will be allocated.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1752
     Our own socket address will be determined after conection set up.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1753
     This is the default after the socket has been created"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1754
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1755
    ^ self bindTo:nil reuseAddress:false
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1756
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1757
    "
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1758
      self newTCP bindAnonymously; listenFor:1; yourself
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1759
    "
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1760
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1761
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1762
bindAnonymouslyToAddress:aSocketAddress
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1763
    "bind to address addressString.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1764
     A free port will be allocated"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1765
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1766
    ^ self
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1767
        bindTo:0
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1768
        address:aSocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1769
        reuseAddress:false
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1770
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1771
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1772
      self newTCP bindAnonymouslyToAddress:IPSocketAddress localHost; listenFor:1; yourself
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1773
    "
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1774
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1775
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1776
bindTo:aSocketAddress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1777
    "ST80 compatible bind, expecting a socketAddress argument.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1778
     The socketAddress object (an instance of SocketAddress)
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1779
     is supposed to respond to #portOrName and #address requests."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1780
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1781
    ^ self bindTo:aSocketAddress reuseAddress:true
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1782
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1783
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1784
bindTo:portNrOrNameString address:addressString
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1785
    "Old interface: bind the socket to an address
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1786
     - returns true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1787
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1788
     The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain:
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1789
        Best use a SocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1790
        For backward compatibility:
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1791
            AF_INET domain can also use (4byte) byteArray like internet numbers,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1792
            AF_UNIX domain cab use pathname strings."
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1793
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1794
    ^ self
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1795
        bindTo:portNrOrNameString
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1796
        address:addressString
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1797
        reuseAddress:true
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1798
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1799
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1800
bindTo:portNrOrNameOrNil address:hostOrPathNameOrSocketAddrOrNil reuseAddress:reuse
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1801
    "Old interface: bind the socket to an address
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1802
     - returns true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1803
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1804
     The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain:
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1805
        Best use a SocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1806
        For backward compatibility:
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1807
            AF_INET domain can also use (4byte) byteArray like internet numbers,
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1808
            AF_UNIX domain can use pathname strings.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1809
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1810
     The reuse boolean argument controls if the SO_REUSEADDR socket option
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1811
     is to be set (to avoid the 'bind: address in use' error).
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1812
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1813
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1814
    |socketAddress|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1815
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1816
    handle isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1817
        ^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1818
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1819
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1820
    hostOrPathNameOrSocketAddrOrNil isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1821
        socketAddress := self socketAddressClass anyHost.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1822
    ] ifFalse:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1823
        (hostOrPathNameOrSocketAddrOrNil isSocketAddress) ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1824
            socketAddress := hostOrPathNameOrSocketAddrOrNil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1825
        ] ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1826
            "backward compatibility: support for byteArray and string arg"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1827
            hostOrPathNameOrSocketAddrOrNil isString ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1828
                socketAddress := self socketAddressClass hostName:hostOrPathNameOrSocketAddrOrNil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1829
            ] ifFalse:[
3138
02f1ace9b3ef class: Socket
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1830
                hostOrPathNameOrSocketAddrOrNil isByteCollection ifFalse:[
02f1ace9b3ef class: Socket
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  1831
                    ^ self error:'bindTo: bad host (socketAddress) argument'
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1832
                ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1833
                socketAddress := self socketAddressClass hostAddress:hostOrPathNameOrSocketAddrOrNil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1834
            ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1835
        ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1836
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1837
    portNrOrNameOrNil notNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1838
        socketAddress port:portNrOrNameOrNil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1839
    ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1840
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1841
    ^ self bindTo:socketAddress reuseAddress:reuse.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1842
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1843
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1844
     (Socket domain:#'AF_INET' type:#stream)
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1845
         bindTo:2144 address:nil; yourself
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1846
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1847
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1848
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1849
bindTo:aSocketAddress reuseAddress:reuse
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1850
    "Bind the socket to aSocketAddress - returns true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1851
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1852
     The reuse boolean argument controls if the SO_REUSEADDR socket option
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1853
     is to be set (to avoid the 'bind: address in use' error).
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1854
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1855
     You can bind to nil to bind to anyHost and assign a random port."
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1856
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1857
    |ok error socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1858
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1859
    handle isNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1860
        ^ self errorNotOpen
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1861
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1862
    socketAddress := aSocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1863
    socketAddress isNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1864
        "ok, get a all zero socket address, so it is for anyHost
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1865
         and the port will be assigned"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1866
        socketAddress := self socketAddressClass new.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1867
    ].
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1868
    ok := false.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1869
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1870
%{  /* STACK: 100000 */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1871
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1872
    OBJ fp = __INST(handle);
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1873
    SOCKET sock;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1874
    union sockaddr_u sa;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1875
    int sockaddr_size;
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  1876
    INT ret;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1877
    int sockAddrOffs;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1878
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1879
    if (fp == nil) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1880
        goto getOutOfHere;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1881
    }
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1882
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1883
    if (! __isBytes(socketAddress)) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1884
        error = __mkSmallInteger(-1);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1885
        goto getOutOfHere;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1886
    }
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1887
    /* get the socket-address */
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1888
    if (__isNonNilObject(socketAddress)){
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1889
        int nIndex;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1890
        OBJ cls = __qClass(socketAddress);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1891
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1892
        sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1893
        nIndex = __qSize(socketAddress) - OHDR_SIZE;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1894
        sockaddr_size = nIndex - sockAddrOffs;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1895
        if (sockaddr_size > sizeof(sa)) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1896
            error=__mkSmallInteger(-2);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1897
            goto getOutOfHere;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1898
        }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1899
        memcpy(&sa, __byteArrayVal(socketAddress) + sockAddrOffs, sockaddr_size);
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1900
    }
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1901
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1902
    sock = SOCKET_FROM_FILE_OBJECT(fp);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1903
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1904
# ifdef SO_REUSEADDR
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1905
    if (reuse == true) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1906
        int on = 1;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1907
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1908
            DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1909
        }
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1910
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1911
# endif /* SO_REUSEADDR */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1912
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1913
# ifdef BIND_BLOCKS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1914
#  ifdef DO_WRAP_CALLS
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1915
    do {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1916
        __threadErrno = 0;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1917
        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
  1918
    } while ((ret < 0) && (__threadErrno == EINTR));
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1919
    if (ret < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1920
        errno = __threadErrno;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1921
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1922
#  else
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1923
    __BEGIN_INTERRUPTABLE__
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1924
    do {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1925
        ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size);
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1926
    } while ((ret < 0) && (errno == EINTR));
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1927
    __END_INTERRUPTABLE__
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1928
#  endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1929
# else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1930
    errno = 0;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1931
    ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1932
# endif
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1933
    if (ret < 0) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1934
# ifdef WIN32
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1935
        if (errno == 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1936
            errno = WSAGetLastError();
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1937
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1938
# endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1939
        DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1940
        error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1941
        goto getOutOfHere;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1942
    } else {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1943
        ok = true;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1944
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1945
#endif /* NO_SOCKET */
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
getOutOfHere: ;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1948
%}.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1949
    ok ~~ true ifTrue:[
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1950
        |errorHolder errorString|
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1951
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1952
        error isInteger ifTrue:[
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1953
            errorHolder := OperatingSystem errorHolderForNumber:error.
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1954
            errorString := errorHolder errorString.
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1955
        ] ifFalse:[
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1956
            errorString := error.
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1957
        ].
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1958
        OpenError newException
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1959
            errorString:('cannot bind socket to address: %1 (%2)' 
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1960
                            bindWith:socketAddress 
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1961
                            with:errorString);
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1962
            errorCode:error;
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1963
            osErrorHolder:errorHolder;
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1964
            parameter:self;
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1965
            raiseRequest.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1966
        "maybe someone catches the error and binds to some other port..."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1967
        ^ true.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1968
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1969
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1970
    port := socketAddress port.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1971
    port == 0 ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1972
        "this is a bind to a random port, now we can get the real port"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1973
        port := self getFullSocketAddress port.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1974
    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1975
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1976
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1977
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1978
     (Socket domain:#'AF_INET' type:#stream)
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1979
        bindTo:(IPSocketAddress anyHost port:445) reuseAddress:false;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1980
        yourself.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1981
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1982
     (Socket domain:#'AF_INET' type:#stream)
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1983
        bindTo:139 reuseAddress:false;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1984
        yourself.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1985
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1986
     (Socket domain:#'AF_INET6' type:#stream)
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1987
        bindTo:nil reuseAddress:false;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1988
        yourself.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1989
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1990
     (Socket domain:#'AF_INET' type:#stream)
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1991
        bindTo:(IPSocketAddress localHost port:2122) reuseAddress:false;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1992
        yourself.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1993
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1994
     (Socket domain:#'AF_UNIX' type:#stream)
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1995
        bindTo:nil reuseAddress:false;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1996
        yourself.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1997
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1998
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1999
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2000
listenFor:aNumber
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2001
    "start listening; return true if ok, false on error
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2002
     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
  2003
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2004
    handle isNil ifTrue:[
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2005
        ^ self errorNotOpen
1935
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
%{
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2008
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2009
    OBJ fp = __INST(handle);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2010
    SOCKET sock;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2011
    int ret;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2012
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2013
    if (! __isSmallInteger(aNumber)) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2014
        DBGPRINTF(("SOCKET: invalid arg\n"));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2015
        RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2016
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2017
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2018
    sock = SOCKET_FROM_FILE_OBJECT(fp);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2019
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2020
#ifdef LISTEN_BLOCKS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2021
# ifdef DO_WRAP_CALLS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2022
    do {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2023
        __threadErrno = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2024
        ret = STX_WSA_NOINT_CALL2("listen", listen, sock, __intVal(aNumber));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2025
    } while ((ret < 0) && (__threadErrno == EINTR));
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2026
    if (ret < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2027
        errno = __threadErrno;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2028
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2029
# else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2030
    __BEGIN_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2031
    do {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2032
        ret = listen(sock, __intVal(aNumber));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2033
    } while ((ret < 0) && (errno == EINTR));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2034
    __END_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2035
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2036
#else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2037
    errno = 0;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2038
    ret = listen(sock, __intVal(aNumber));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2039
#endif
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 (ret < 0) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2042
# ifdef WIN32
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2043
        if (errno == 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2044
            errno = WSAGetLastError();
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2045
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2046
# endif
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2047
        DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2048
        __INST(lastErrorNumber) = __MKSMALLINT(errno);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2049
        RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2050
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2051
#else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2052
    RETURN (false);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2053
#endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2054
%}.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2055
    listening := true.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2056
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2057
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2058
1576
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2059
!Socket methodsFor:'closing'!
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2060
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2061
shutDown
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2062
    "shutDown and close the socket"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2063
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2064
    self shutdown:2.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2065
    self close
1576
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2066
!
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2067
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2068
shutDownInput
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2069
    "shutDown the input side of the socket.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2070
     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
  2071
     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
  2072
     (e.g. setting the TCP-Windowsize to 0)"
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2073
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2074
    self shutdown:0.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2075
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2076
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2077
shutDownOutput
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2078
    "shutDown the output side of the socket.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2079
     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
  2080
     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
  2081
     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
  2082
1884
ee0d640a25a7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
  2083
    self shutdown:1.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2084
! !
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2085
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2086
!Socket methodsFor:'connecting'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2087
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2088
connectTo:aSocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2089
    ^ self connectTo:aSocketAddress withTimeout:nil
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2090
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2091
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2092
connectTo:hostOrPathName port:portNrOrName
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2093
    "Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2094
     For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2095
     but it is recommended to pass SocketAddress instances.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2096
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2097
     Return true if ok, false otherwise.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2098
     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
  2099
     See also: #connectTo:port:withTimeout: for a somewhat nicer interface."
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
    ^ self connectTo:hostOrPathName port:portNrOrName withTimeout:nil
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2102
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2103
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2104
connectTo:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil withTimeout:timeout
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2105
    "Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2106
     For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2107
     but it is recommended to pass SocketAddress instances.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2108
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2109
     Return true if ok, false otherwise.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2110
     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
  2111
     or timeout millliseconds have passed."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2112
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2113
    |domainClass socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2114
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2115
    (hostOrPathNameOrSocketAddr isSocketAddress) ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2116
        socketAddress := hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2117
        portNrOrNameOrNil notNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2118
            socketAddress port:portNrOrNameOrNil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2119
        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2120
    ] ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2121
        "backward compatibility: support for byteArray and string arg"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2122
        domainClass := self class socketAddressClassForDomain:domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2123
        domainClass isNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2124
            ^ self error:'invalid (unsupported) domain'.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2125
        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2126
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2127
        hostOrPathNameOrSocketAddr isString ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2128
            socketAddress := domainClass hostName:hostOrPathNameOrSocketAddr serviceName:portNrOrNameOrNil type:#SOCK_STREAM.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2129
            peerName := hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2130
        ] ifFalse:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2131
            hostOrPathNameOrSocketAddr isByteCollection ifFalse:[
3138
02f1ace9b3ef class: Socket
Stefan Vogel <sv@exept.de>
parents: 3137
diff changeset
  2132
                ^ self error:'connectTo: bad host (socketAddress) argument'
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2133
            ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2134
            socketAddress := domainClass hostAddress:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2135
        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2136
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2137
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2138
    ^ self connectTo:socketAddress withTimeout:timeout.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2139
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2140
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2141
       |sock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2142
       sock := Socket newTCP.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2143
       sock connectTo:'localhost' port:21 withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2144
       sock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2145
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2146
       |sock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2147
       sock := Socket newTCP.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2148
       sock connectTo:'localhost' port:9876 withTimeout:2000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2149
       sock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2150
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2151
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2152
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2153
connectTo:aSocketAddress withTimeout:timeout
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2154
    "Connect to a SocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2155
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2156
     Return true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2157
     The current process will block (but not the whole Smalltalk) until the connection is established,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2158
     or timeout millliseconds have passed."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2159
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2160
    |isAsync err|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2161
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2162
    handle isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2163
        ^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2164
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2165
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2166
%{  /* STACK: 100000 */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2167
#ifndef NO_SOCKET
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2168
//#define DUMP_ADDRESS 1
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2169
    OBJ fp = __INST(handle);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2170
    union sockaddr_u sa;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2171
    SOCKET sock;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2172
    int a;
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2173
    INT ret;
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2174
    int oldFlags;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2175
    int on = 1;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2176
    int sockaddr_size;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2177
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2178
    if (!__isNonNilObject(aSocketAddress) || !__isBytes(aSocketAddress)) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2179
        DBGPRINTF(("SOCKET: invalid socketAddress\n"));
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2180
        err = @symbol(argumentError);
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2181
        goto out;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2182
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2183
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2184
    {
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2185
        int sockAddrOffs = 0;
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2186
        int nIndex =__byteArraySize(aSocketAddress);
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2187
        OBJ cls = __qClass(aSocketAddress);
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2188
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2189
        //if (cls != @global(ByteArray))
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2190
        //    sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2191
        sockaddr_size = nIndex - sockAddrOffs;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2192
        if (sockaddr_size > sizeof(sa)) {
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2193
            DBGPRINTF(("SOCKET: invalid (short) socketAddress\n"));
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2194
            err = @symbol(argumentError);
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2195
            goto out;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2196
        }
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2197
        memcpy(&sa, __byteArrayVal(aSocketAddress) + sockAddrOffs, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2198
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2199
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2200
    sock = SOCKET_FROM_FILE_OBJECT(fp);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2201
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2202
# if defined(O_NONBLOCK) && !defined(WIN32)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2203
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2204
     * set to non-blocking and wait later
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2205
     */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2206
    oldFlags = fcntl(sock, F_GETFL, 0);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2207
    /* on SUNOS4.x, would use fcntl(osfd, F_SETFL, flags | FNDELAY); */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2208
    fcntl(sock, F_SETFL, oldFlags | O_NONBLOCK);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2209
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2210
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2211
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2212
     * connect
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2213
     */
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2214
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2215
# if defined(DO_WRAP_CALLS)
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2216
    // __setWrapCallDebugging(1,1); 
2414
886ecf4b36cd changed:
ab
parents: 2340
diff changeset
  2217
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2218
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2219
    do {
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2220
        DBGFPRINTF((stderr, "SOCKET: (sock=%d) connect...\n", sock));
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2221
        ret = STX_WSA_NOINT_CALL3("connect", connect, sock, &sa, (INT)sockaddr_size);
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2222
        DBGFPRINTF((stderr, "SOCKET: connect(%d) -> %"_ld_" (%d)\n", sock, (INT)ret, __threadErrno));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2223
    } while ((ret < 0) && (__threadErrno == EINTR));
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2224
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2225
    if (ret < 0) {
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2226
        int optLen = sizeof(errno);
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2227
        errno = __threadErrno;
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2228
#if 0
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2229
        if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errno, &optLen) == SOCKET_ERROR) {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2230
            DBGFPRINTF((stderr, "SOCKET: getsockopt(SO_ERROR) failed: %d\n", WSAGetLastError()));
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2231
        }
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2232
#endif
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2233
    }
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2234
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2235
    // __setWrapCallDebugging(1,0);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2236
# else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2237
    __BEGIN_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2238
    do {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2239
        ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2240
    } while ((ret < 0)
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2241
             && ((errno == EINTR)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2242
# ifdef EAGAIN
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2243
                 || (errno == EAGAIN)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2244
# endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2245
                ));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2246
    __END_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2247
#endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2248
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2249
    if (ret < 0) {
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2250
# if defined(EINPROGRESS) || defined(EALREADY)
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2251
        if (0
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2252
#  ifdef EINPROGRESS
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2253
            || (errno == EINPROGRESS)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2254
#  endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2255
#  ifdef EALREADY
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2256
            || (errno == EALREADY)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2257
#  endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2258
        ) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2259
            /*
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2260
             * This was a nonblocking operation that will take some time.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2261
             * Do a select on read to get informed when the operation is ready.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2262
             */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2263
            DBGFPRINTF((stderr, "SOCKET: isAsync is true\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2264
            isAsync = true;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2265
        } else
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2266
# endif /* EINPROGRESS or EALREADY */
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2267
        {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2268
            DBGFPRINTF((stderr, "SOCKET: connect failed ret=%"_ld_" errno=%d __threadErrno=%d\n",
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2269
                        (INT)ret, errno, __threadErrno ));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2270
# ifdef DUMP_ADDRESS
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2271
            {
3072
fbad791801f2 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3059
diff changeset
  2272
                unsigned char *cp = (unsigned char *)(&sa);
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2273
                int i;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2274
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2275
                console_printf("address data:\n");
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2276
                for (i=0; i<sockaddr_size; i++) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2277
                    console_printf(" %02x\n", *cp++);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2278
                }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2279
            }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2280
# endif
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2281
            err = __MKSMALLINT(errno);
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2282
        }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2283
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2284
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2285
# if defined(O_NONBLOCK) && !defined(WIN32)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2286
    fcntl(sock, F_SETFL, oldFlags);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2287
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2288
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2289
# else /* NO_SOCKET */
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2290
    err = @symbol(notSupported);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2291
# endif /* NO_SOCKET */
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2292
out:;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2293
%}.
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2294
    
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2295
    err notNil ifTrue:[
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2296
        err isSymbol ifTrue:[
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2297
            self primitiveFailed:err.
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2298
        ].
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2299
        lastErrorNumber := err.
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2300
        ^ false.
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2301
        "/ Once we will raise an exception instead of returning false (and have to change some code above):
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2302
"/        (OperatingSystem errorHolderForNumber:err) reportError.
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2303
    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2304
    isAsync == true ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2305
        (self writeWaitWithTimeoutMs:timeout) ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2306
            "/ a timeout occured
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2307
            "/ should I cancel the connect?
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2308
            ^ false.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2309
        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2310
        err := self getSocketError.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2311
        err ~~ 0 ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2312
            lastErrorNumber := err.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2313
            ^ false.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2314
        ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2315
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2316
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
  2317
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2318
    peer := aSocketAddress.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2319
    port isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2320
        "socket has not been explicitly bound,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2321
         after connect it has been bound implicitly - fetch the port"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2322
        port := self getFullSocketAddress port.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2323
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2324
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2325
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2326
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2327
       |sock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2328
       sock := Socket newTCP.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2329
       sock connectTo:(IPSocketAddress localHost port:21) withTimeout:1000.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2330
       sock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2331
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2332
       |sock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2333
       sock := Socket newTCP.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2334
       sock connectTo:(IPSocketAddress localHost port:9876) withTimeout:2000.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2335
       sock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2336
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2337
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2338
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2339
!Socket methodsFor:'datagram transmission'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2340
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2341
receiveBuffer:aDataBuffer start:startIndex for:nBytes
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2342
    "receive data
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2343
     Return the number of bytes received, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2344
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2345
     instance variable.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2346
     The thread blocks until data arrives - you may want to wait before
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2347
     receiving, using #readWait or #readWaitWithTimeout:."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2348
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2349
    |nReceived error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2350
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2351
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2352
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2353
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2354
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2355
    if (fp != nil) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2356
        SOCKET sock;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2357
        INT objSize, offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2358
        INT n;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2359
        char *extPtr;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2360
        unsigned char *buffer;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2361
        unsigned char *allocatedBuffer = NULL;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2362
        INT flags = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2363
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2364
        sock = SOCKET_FROM_FILE_OBJECT(fp);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2365
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2366
        if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2367
        if (__isSmallInteger(nBytes)) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2368
            if (__intVal(nBytes) < objSize) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2369
                objSize = __intVal(nBytes);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2370
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2371
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2372
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2373
# ifdef DO_WRAP_CALLS
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2374
        if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2375
            buffer = extPtr + offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2376
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2377
            allocatedBuffer = buffer = (char *)malloc(objSize);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2378
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2379
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2380
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2381
            __threadErrno = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2382
            n = (INT)STX_WSA_NOINT_CALL4("recv", recv, sock, buffer, objSize, flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2383
        } while ((n < 0) && (__threadErrno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2384
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2385
            errno = __threadErrno;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2386
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2387
        
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2388
        if (allocatedBuffer) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2389
            if (n > 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2390
                bcopy(allocatedBuffer, (char *)__InstPtr(aDataBuffer) + offs, n);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2391
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2392
            free(allocatedBuffer);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2393
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2394
# else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2395
        __BEGIN_INTERRUPTABLE__
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2396
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2397
            if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2398
                n = recv(sock, extPtr + offs, objSize, flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2399
            } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2400
                n = recv(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2401
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2402
        } while ((n < 0) && (errno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2403
        __END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2404
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2405
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2406
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2407
            error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2408
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2409
            RETURN(__MKSMALLINT(n));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2410
        }        
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2411
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2412
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2413
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2414
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2415
    error notNil ifTrue:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2416
        ^ ReadError
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2417
            raiseRequestWith:self
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2418
            errorString:(' : ' , OperatingSystem errorTextForNumber:error)
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2419
    ].
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2420
    "
1282
7e4ffdfd19f6 remove historic leftover
Claus Gittinger <cg@exept.de>
parents: 1280
diff changeset
  2421
     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
  2422
    "
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2423
    self primitiveFailed
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2424
!
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2425
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2426
receiveFrom:anAddressBuffer buffer:aDataBuffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2427
    "receive datagramm data - put address of originating host into
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2428
     anAddressBuffer, data into aBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2429
     Both must be ByteArray-like. The addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2430
     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
  2431
     Return the number of bytes received, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2432
     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
  2433
     instance variable."
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  2434
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2435
    ^ 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
  2436
!
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
  2437
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2438
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2439
    "receive datagramm data
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2440
     - put address of originating host into anAddressBuffer, data into aBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2441
     For backward compatibility, the addressBuffer may be a non-SocketAddress;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2442
     then, it must be a byteArray with appropriate size for the addressBytes.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2443
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2444
     Return the number of bytes received, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2445
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2446
     instance variable.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2447
     The thread blocks until data arrives - you may want to wait before
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2448
     receiving, using #readWait or #readWaitWithTimeout:."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2449
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2450
    |domainClass addr addrLen nReceived error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2451
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2452
    domainClass := self class socketAddressClassForDomain:domain.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2453
    domainClass isNil ifTrue:[
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2454
        ^ self error:'invalid (unsupported) domain'.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2455
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2456
    (anAddressBuffer isKindOf:SocketAddress) ifTrue:[
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2457
        anAddressBuffer class == domainClass ifFalse:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2458
            ^ self error:'addressBuffer class mismatch (domain)'.
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2459
        ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2460
        addr := anAddressBuffer.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2461
    ] ifFalse:[
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2462
        anAddressBuffer notNil ifTrue:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2463
            addr := domainClass new.
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2464
        ].
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2465
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2466
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2467
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2468
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2469
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2470
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2471
    if (fp != nil) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2472
        SOCKET sock;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2473
        INT objSize;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2474
        union sockaddr_u sa;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2475
        unsigned INT alen = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2476
        INT n, offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2477
        INT flags = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2478
        char *extPtr;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2479
        unsigned char *allocatedBuffer = NULL;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2480
        unsigned char *buffer = NULL;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2481
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2482
        sock = SOCKET_FROM_FILE_OBJECT(fp);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2483
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2484
        if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2485
        if (__isSmallInteger(nBytes)) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2486
            if (__intVal(nBytes) < objSize) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2487
                objSize = __intVal(nBytes);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2488
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2489
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2490
# ifdef DO_WRAP_CALLS
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2491
        if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2492
            buffer = extPtr + offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2493
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2494
            allocatedBuffer = buffer = (char *)malloc(objSize);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2495
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2496
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2497
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2498
            __threadErrno = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2499
            alen = sizeof(sa);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2500
            n = (INT)STX_WSA_NOINT_CALL6("recvfrom", recvfrom, sock, buffer, objSize, flags, (struct sockaddr *)&sa, &alen);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2501
        } while ((n < 0) && (__threadErrno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2502
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2503
            errno = __threadErrno;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2504
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2505
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2506
        if (allocatedBuffer) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2507
            if (n > 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2508
                memcpy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, n);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2509
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2510
            free(allocatedBuffer);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2511
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2512
# else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2513
        __BEGIN_INTERRUPTABLE__
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2514
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2515
            alen = sizeof(sa);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2516
            if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2517
                n = recvfrom(sock, extPtr + offs, objSize, flags, (struct sockaddr *) &sa, &alen);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2518
            } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2519
                n = recvfrom(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, flags, (struct sockaddr *) &sa, &alen);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2520
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2521
        } while ((n < 0) && (errno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2522
        __END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2523
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2524
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2525
        if (n >= 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2526
            if (__isNonNilObject(addr)) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2527
                char *addrPtr;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2528
                OBJ oClass;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2529
                int nInstVars, nInstBytes, objSize;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2530
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2531
                oClass = __qClass(addr);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2532
                if (! __isBytes(addr) )
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2533
                    goto bad;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2534
                nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2535
                nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2536
                objSize = __qSize(addr) - nInstBytes;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2537
                addrPtr = (char *)__InstPtr(addr) + nInstBytes;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2538
                if (objSize < alen)
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2539
                    goto bad;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2540
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2541
                /*
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2542
                 * extract the datagrams address
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2543
                 */
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2544
                memcpy(addrPtr, (char *)&sa, alen);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2545
                addrLen = __MKSMALLINT(alen);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2546
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2547
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2548
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2549
            error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2550
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2551
        nReceived = __MKSMALLINT(n);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2552
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2553
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2554
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2555
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2556
    error notNil ifTrue:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2557
        ^ ReadError
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2558
            raiseRequestWith:self
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2559
            errorString:(' : ' , OperatingSystem errorTextForNumber:error)
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2560
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2561
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2562
    nReceived notNil ifTrue:[
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2563
        addrLen notNil ifTrue:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2564
            (addr == anAddressBuffer) ifFalse:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2565
                self obsoleteFeatureWarning:'please use a socketAddress argument'.
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2566
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2567
                "can be a ByteArray for backward compatibility"
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2568
                anAddressBuffer replaceFrom:1 to:addrLen with:(addr hostAddress).
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2569
            ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2570
        ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2571
        ^ nReceived
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2572
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2573
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2574
     arrive here if you try to receive into an invalid buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2575
     (i.e. not ByteArray-like),
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2576
     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
  2577
     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
  2578
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2579
    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
  2580
!
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2581
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2582
sendBuffer:aDataBuffer start:startIndex for:nBytes flags:flags
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2583
    "send data.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2584
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2585
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2586
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2587
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2588
     instance variable."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2589
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2590
    |error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2591
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2592
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2593
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2594
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2595
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2596
    if ((fp != nil)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2597
     && __isSmallInteger(startIndex)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2598
     && __isSmallInteger(nBytes)) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2599
        SOCKET sock;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2600
        INT objSize;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2601
        INT n;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2602
        char *extPtr;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2603
        INT _flags = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2604
        INT offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2605
        unsigned long norder;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2606
        unsigned char *buffer;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2607
        unsigned char *allocatedBuffer = NULL;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2608
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2609
        _flags = __longIntVal(flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2610
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2611
        sock = SOCKET_FROM_FILE_OBJECT(fp);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2612
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2613
        if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2614
        if (__isSmallInteger(nBytes)) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2615
            if (__intVal(nBytes) < objSize) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2616
                objSize = __intVal(nBytes);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2617
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2618
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2619
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2620
# ifdef DGRAM_DEBUG
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2621
        console_printf("sending %d bytes ...\n", nBytes);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2622
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2623
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2624
#ifdef DO_WRAP_CALLS
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2625
        if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2626
            buffer = extPtr + offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2627
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2628
            allocatedBuffer = buffer = (char *)malloc(objSize);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2629
            bcopy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, objSize);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2630
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2631
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2632
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2633
            __threadErrno = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2634
            n = (INT)STX_WSA_NOINT_CALL4("send", send, sock, buffer, objSize, _flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2635
        } while ((n < 0) && (__threadErrno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2636
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2637
            errno = __threadErrno;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2638
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2639
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2640
        if (allocatedBuffer) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2641
            free(allocatedBuffer);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2642
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2643
#else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2644
        __BEGIN_INTERRUPTABLE__
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2645
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2646
            if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2647
                n = send(sock, extPtr + offs, objSize, _flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2648
            } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2649
                n = send(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2650
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2651
        } while ((n < 0) && (errno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2652
        __END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2653
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2654
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2655
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2656
            error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2657
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2658
            RETURN (__MKSMALLINT(n));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2659
        }            
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2660
    }
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2661
#endif
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2662
bad: ;
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2663
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2664
    error notNil ifTrue:[
3139
71647b626b10 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3138
diff changeset
  2665
        self writeError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2666
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2667
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2668
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2669
     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
  2670
    "
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2671
    self primitiveFailed
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2672
!
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2673
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2674
sendTo:anAddressBuffer buffer:buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2675
    "send datagramm data - fetch address of destination host from
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2676
     anAddressBuffer, data from aDataBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2677
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2678
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2679
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2680
     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
  2681
     instance variable.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2682
     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
  2683
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2684
    ^ 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
  2685
!
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2686
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2687
sendTo:anAddressBuffer buffer:buffer start:startIndex for:count
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2688
    "send datagramm data - fetch address of destination host from
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2689
     anAddressBuffer, data from aDataBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2690
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2691
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2692
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2693
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2694
     instance variable.
817
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
  2695
     Flags is currently ignored; it is there for ST-80 compatibility."
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
  2696
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2697
    ^ self sendTo:anAddressBuffer buffer:buffer start:startIndex for:count flags:0
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2698
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2699
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2700
sendTo:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:flags
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2701
    "send datagramm data - fetch address of destination host from
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2702
     anAddressBuffer, data from aDataBuffer starting at startIndex,
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2703
     sending count bytes.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2704
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2705
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2706
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2707
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2708
     instance variable."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2709
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2710
    |domainClass addr error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2711
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2712
    (anAddressBuffer isKindOf:SocketAddress) ifTrue:[
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2713
        addr := anAddressBuffer.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2714
    ] ifFalse:[
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2715
        anAddressBuffer isByteArray ifFalse:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2716
            ^ self error:'bad socketAddress argument'
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2717
        ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2718
        domainClass := self class socketAddressClassForDomain:domain.
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2719
        domainClass isNil ifTrue:[
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2720
            ^ self error:'invalid (unsupported) domain'.
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2721
        ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2722
        addr := domainClass hostAddress:anAddressBuffer.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2723
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2724
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2725
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2726
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2727
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2728
    if ((fp != nil)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2729
     && __isSmallInteger(startIndex)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2730
     && __isSmallInteger(nBytes)) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2731
        SOCKET sock;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2732
        INT objSize;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2733
        struct sockaddr *sockaddr_ptr;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2734
        union sockaddr_u sa;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2735
        INT alen = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2736
        INT sockAddrOffs, sockaddr_size;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2737
        INT n;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2738
        char *extPtr;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2739
        INT _flags = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2740
        INT offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2741
        unsigned long norder;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2742
        unsigned char *buffer;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2743
        unsigned char *allocatedBuffer = NULL;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2744
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2745
        _flags = __longIntVal(flags);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2746
        sock = SOCKET_FROM_FILE_OBJECT(fp);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2747
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2748
        if (! __isBytes(addr)) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2749
            sockaddr_size = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2750
            sockaddr_ptr = (struct sockaddr *)0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2751
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2752
            int nIndex;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2753
            OBJ cls;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2754
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2755
            sockAddrOffs = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2756
            if ((cls = __qClass(addr)) != @global(ByteArray))
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2757
                sockAddrOffs += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2758
            nIndex = __qSize(addr) - OHDR_SIZE;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2759
            sockaddr_size = nIndex - sockAddrOffs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2760
            if (sockaddr_size > sizeof(sa)) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2761
                console_fprintf(stderr, "Socket [warning]: bad socketAddr\n");
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2762
                goto bad;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2763
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2764
            memcpy(&sa, (__byteArrayVal(addr) + sockAddrOffs), sockaddr_size);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2765
            sockaddr_ptr = (struct sockaddr *)(&sa);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2766
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2767
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2768
        if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2769
        if (__isSmallInteger(nBytes)) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2770
            if (__intVal(nBytes) < objSize) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2771
                objSize = __intVal(nBytes);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2772
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2773
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2774
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2775
#ifdef DO_WRAP_CALLS
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2776
        if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2777
            buffer = extPtr + offs;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2778
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2779
            allocatedBuffer = buffer = (char *)malloc(objSize);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2780
            bcopy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, objSize);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2781
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2782
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2783
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2784
            __threadErrno = 0;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2785
            n = (INT)STX_WSA_NOINT_CALL6("sendto", sendto, sock, buffer, objSize, _flags, sockaddr_ptr, sockaddr_size);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2786
        } while ((n < 0) && (__threadErrno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2787
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2788
            errno = __threadErrno;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2789
        }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2790
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2791
        if (allocatedBuffer) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2792
            free(allocatedBuffer);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2793
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2794
#else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2795
        __BEGIN_INTERRUPTABLE__
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2796
        do {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2797
            if (extPtr) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2798
                n = sendto(sock, extPtr + offs, objSize, _flags, sockaddr_ptr, sockaddr_size);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2799
            } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2800
                n = sendto(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, sockaddr_ptr, sockaddr_size);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2801
            }
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2802
        } while ((n < 0) && (errno == EINTR));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2803
        __END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2804
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2805
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2806
        if (n < 0) {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2807
            error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2808
        } else {
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2809
            RETURN (__MKSMALLINT(n));
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2810
        }            
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2811
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2812
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2813
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2814
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2815
    error notNil ifTrue:[
3139
71647b626b10 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3138
diff changeset
  2816
        self writeError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2817
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2818
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2819
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2820
     arrive here if you try to send from an invalid buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2821
     (i.e. not ByteArray-like),
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2822
     or if the addressBuffer is nonNil AND not a ByteArray/String
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2823
     or if the addressBuffer is nonNil AND too small.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2824
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2825
    self primitiveFailed
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  2826
! !
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  2827
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2828
!Socket methodsFor:'low level'!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2829
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2830
closeFile
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2831
    "low level close"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2832
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2833
%{  /* NOCONTEXT */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2834
#ifndef NO_SOCKET
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2835
    OBJ t;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2836
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2837
    t = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2838
    if (t != nil) {
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2839
        FILE *fp = __FILEVal(t);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2840
        int fd  = fileno(fp);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2841
        SOCKET sock = SOCKET_FROM_FD(fd);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2842
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2843
        __INST(handle) = nil;
2414
886ecf4b36cd changed:
ab
parents: 2340
diff changeset
  2844
886ecf4b36cd changed:
ab
parents: 2340
diff changeset
  2845
# ifdef DO_WRAP_CALLS
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2846
        {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2847
          INT ret;
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2848
          /*__setWrapCallDebugging(1,1); */
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2849
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2850
          if (__INST(buffered) == true){
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2851
              /* do a fflush() first, so that fclose() doesn't block
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2852
               * we suspect, that EINTR causes problems in fclose()
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2853
               */
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2854
              do {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2855
                  __threadErrno = 0;
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2856
                  ret = (INT)__STX_C_CALL1("fflush", fflush, fp);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2857
              } while((ret < 0) && (__threadErrno == EINTR));
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2858
          }
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  2859
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  2860
#  if defined(WIN32) && defined(CLOSESOCKET_BEFORE_FCLOSE)
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2861
          DBGFPRINTF((stderr, "SOCKET: closesocket (%d)\n", sock));
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2862
          closesocket(sock);
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  2863
#  endif
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2864
          do {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2865
            __threadErrno = 0;
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2866
            ret = (INT)STX_C_NOINT_CALL1("fclose", fclose, fp);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2867
          } 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
  2868
ff00563f0444 Protect against concurrent access when doing fclose and fdopen
Stefan Vogel <sv@exept.de>
parents: 2579
diff changeset
  2869
#  if defined(WIN32) && defined(CLOSESOCKET_AFTER_FCLOSE)
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2870
          do {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2871
            __threadErrno = 0;
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2872
            ret = (INT)STX_WSA_NOINT_CALL1("closesocket", closesocket, sock);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2873
          } while ((ret < 0) && (__threadErrno == EINTR));
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2874
#  endif
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2875
          /*__setWrapCallDebugging(1,0);*/
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2876
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2877
# else /* !DO_WRAP_CALLS */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2878
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2879
        if (__INST(buffered) == true){
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2880
            DBGFPRINTF((stderr, "SOCKET: fflush %"_lx_" (%d %d)\n", (INT)fp, fileno(fp), sock));
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2881
            fflush(fp);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2882
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2883
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2884
#  if defined(CLOSESOCKET_BEFORE_FCLOSE)
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2885
        DBGFPRINTF((stderr, "SOCKET: closesocket (%d)\n", sock));
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2886
        closesocket(sock);
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2887
#  endif
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2888
        if ((@global(FileOpenTrace) == true) || __debugging__) {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2889
            console_fprintf(stderr, "SOCKET: fclose %"_lx_" (%d %d)\n", (INT)fp, fileno(fp), sock);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2890
        }
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2891
        fclose(fp);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2892
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2893
#  if defined(CLOSESOCKET_AFTER_FCLOSE)
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2894
        DBGFPRINTF((stderr, "SOCKET: closesocket (%d)\n", sock));
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2895
        closesocket(sock);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2896
#  endif
2414
886ecf4b36cd changed:
ab
parents: 2340
diff changeset
  2897
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2898
# endif /* !DO_WRAP_CALLS */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2899
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2900
#endif /* NO_SOCKET */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2901
%}
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2902
!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2903
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2904
getSocketAdress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2905
    "BAD SPELLING, of #getSocketAddress, kept for compatibility with swazoo"
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
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2908
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2909
    ^ self getSocketAddress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2910
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2911
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2912
getSocketError
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2913
    "get the SO_ERROR form the socket, which indicates the
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2914
     result of an asynchronous operation"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2915
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2916
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2917
#ifndef NO_SOCKET
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2918
    OBJ fp;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2919
    int err;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2920
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2921
    fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2922
    if (fp == nil) {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2923
	err = EBADF;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2924
    } else {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2925
	unsigned int sz;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2926
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2927
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2928
	sock = SOCKET_FROM_FILE_OBJECT(fp);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2929
	sz = sizeof(err);
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2930
	if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)(&err), &sz) < 0) {
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2931
# ifdef WIN32
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  2932
	    errno = WSAGetLastError();
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2933
# endif
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  2934
	    err = errno;
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  2935
	}
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2936
    }
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2937
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2938
    RETURN(__MKSMALLINT(err));
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2939
#endif
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2940
%}
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2941
!
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2942
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2943
listenWithBacklog:aNumber
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2944
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2945
    "same as listenFor: - backward compatibility with old ST/X"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2946
    self obsoleteMethodWarning.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2947
    ^ self listenFor:aNumber
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2948
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2949
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2950
pollingWaitForNewConnectionOrDataOnAny:otherConnections timeout:timeoutSeconds
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2951
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2952
    "stupid MSDOS does not support select on sockets (sigh).
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2953
     Must poll here."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2954
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2955
    |millis newConnection|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2956
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2957
    millis := timeoutSeconds * 1000.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2958
    [millis > 0] whileTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2959
	otherConnections size > 0 ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2960
	    otherConnections do:[:aConnection |
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2961
		aConnection canReadWithoutBlocking ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2962
		    ^ aConnection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2963
		]
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2964
	    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2965
	].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2966
	newConnection := self blockingAccept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2967
	newConnection notNil ifTrue:[^ newConnection].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2968
	Delay waitForMilliseconds:20.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2969
	millis := millis - 20.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2970
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2971
    ^ nil.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2972
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2973
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2974
pollingWaitForNewConnectionWithTimeout:timeoutSeconds
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2975
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2976
    "stupid MSDOS does not support select on sockets (sigh).
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2977
     Must poll here."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2978
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2979
    |millis newConnection|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2980
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2981
    timeoutSeconds notNil ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2982
	millis := timeoutSeconds * 1000.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2983
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2984
    [millis isNil or:[millis > 0]] whileTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2985
	newConnection := self blockingAccept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2986
	newConnection notNil ifTrue:[^ newConnection].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2987
	Delay waitForMilliseconds:20.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2988
	millis notNil ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2989
	    millis := millis - 20.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2990
	]
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2991
    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2992
    ^ nil.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2993
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2994
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2995
primAcceptOn:aSocket blocking:blocking
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2996
    "accept a connection on a server port (created with:'Socket>>newTCP:')
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2997
     usage is: (Socket basicNew acceptOn:(Socket newTCP:9999)).
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2998
     Return the true if ok; false if not.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2999
     If blocking is true, the accept() syscall (and the whole smalltalk image)
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3000
     will block, until a connection is accepted.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3001
     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
  3002
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3003
    |serverSocketFd addr domainClass|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3004
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3005
    handle notNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3006
        ^ self errorAlreadyOpen
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
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3009
    domain := aSocket domain.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3010
    socketType := aSocket type.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3011
    serverSocketFd := aSocket fileDescriptor.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3012
    serverSocketFd isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3013
        ^ self error:'invalid server socket'
1935
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
    (serverSocketFd isMemberOf:SmallInteger) ifFalse:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3016
        ^ self error:'invalid server socket'
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3017
    ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3018
    "unix domain sockets do not return a valid peer name on accept"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3019
    domainClass := self class socketAddressClassForDomain:domain.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3020
    domainClass isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3021
        ^ self error:'invalid (unsupported) domain'.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3022
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3023
    addr := domainClass new.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3024
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3025
%{  /* STACK: 100000 */
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3026
#ifndef NO_SOCKET
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3027
    FILE *fp;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3028
    int flags;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3029
    SOCKET sock, newSock;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3030
    union sockaddr_u sa;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3031
    unsigned int alen;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3032
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3033
    sock = SOCKET_FROM_FD(__intVal(serverSocketFd));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3034
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3035
    if (blocking == false) {
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3036
# if defined(O_NONBLOCK) && defined(SET_NDELAY)
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3037
        flags = fcntl(sock, F_GETFL);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3038
        fcntl(sock, F_SETFL, flags | O_NONBLOCK);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3039
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3040
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3041
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3042
# ifdef DO_WRAP_CALLS
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3043
    do {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3044
        __threadErrno = 0;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3045
        alen = sizeof(sa);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3046
        newSock = (SOCKET)STX_WSA_CALL3("accept", accept, sock, &sa, &alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3047
    } while ((newSock < 0) && (__threadErrno == EINTR));
3127
333e87ba225a class: Socket
Stefan Vogel <sv@exept.de>
parents: 3126
diff changeset
  3048
    if (newSock < 0) {
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  3049
        errno = __threadErrno;
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  3050
    }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3051
# else
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3052
    __BEGIN_INTERRUPTABLE__
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3053
    do {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3054
        alen = sizeof(sa);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3055
        newSock = accept(sock, (struct sockaddr *) &sa, &alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3056
    } while ((newSock < 0) && (errno == EINTR));
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3057
    __END_INTERRUPTABLE__
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3058
# endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3059
    DBGFPRINTF((stderr, "SOCKET: accept newSock=%d\n", newSock));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3060
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3061
    if (blocking == false) {
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3062
# if defined(O_NDELAY) && defined(SET_NDELAY)
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3063
        fcntl(sock, F_SETFL, flags);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3064
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3065
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3066
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3067
    if (newSock < 0) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3068
        DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3069
        __INST(lastErrorNumber) = __MKSMALLINT(errno);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3070
        RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3071
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3072
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3073
    if (__isNonNilObject(addr)) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3074
        OBJ oClass = __qClass(addr);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3075
        int nInstVars, nInstBytes, objSize;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3076
        char *addrP;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3077
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3078
        if (! __isBytes(addr) ) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3079
            DBGPRINTF(("SOCKET: bad addr\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3080
            closesocket(newSock);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3081
            RETURN (false);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3082
        }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3083
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3084
        nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3085
        nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3086
        objSize = __qSize(addr) - nInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3087
        addrP = (char *)__InstPtr(addr) + nInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3088
        if (objSize < alen) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3089
            DBGPRINTF(("SOCKET: bad addr\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3090
            closesocket(newSock);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3091
            RETURN (false);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3092
        }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3093
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3094
        /*
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3095
         * extract the partners address
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3096
         */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3097
        memcpy(addrP, (char *)&sa, alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3098
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3099
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3100
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3101
     * make it a FILE *
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3102
     */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3103
# ifdef WIN32
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3104
    {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3105
        int _fd;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3106
        __stxWrapApiEnterCritical();
2915
896fffb0f02e mingw64
Claus Gittinger <cg@exept.de>
parents: 2909
diff changeset
  3107
#  if defined( __BORLANDC__ ) || defined( __MINGW__ )
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3108
        _fd = _open_osfhandle((long)newSock, 0);
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3109
#  else
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3110
        _fd = (int)newSock;
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3111
#  endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3112
        fp = fdopen(_fd, "r+");
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3113
        __stxWrapApiLeaveCritical();
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3114
        DBGPRINTF(("SOCKET: sock=%d fd=%d fp=%"_lx_"\n",newSock, _fd, (INT)fp));
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3115
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3116
# else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3117
    fp = fdopen(newSock, "r+");
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3118
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3119
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3120
    if (! fp) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3121
        DBGPRINTF(("SOCKET: fdopen call failed\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3122
        __INST(lastErrorNumber) = __MKSMALLINT(errno);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3123
#  ifdef DO_WRAP_CALLS
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3124
        {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3125
          INT ret;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3126
          do {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3127
              __threadErrno = 0;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3128
              ret = (INT)STX_WSA_CALL1("closesocket", closesocket, newSock);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3129
          } while ((ret < 0) && (__threadErrno == EINTR));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3130
        }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3131
#  else
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3132
        closesocket(newSock);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3133
#  endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3134
        DBGFPRINTF((stderr, "SOCKET: close (fdopen failed) (%d)\n", newSock));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3135
        RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3136
    }
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  3137
    __INST(handleType) = @symbol(socketFilePointer);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3138
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3139
    if ((@global(FileOpenTrace) == true) || __debugging__) {
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3140
# ifdef WIN32
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3141
        {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3142
            HANDLE h;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3143
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3144
            int _fd = fileno(fp);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3145
            h = (HANDLE)_get_osfhandle(_fd);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3146
            console_fprintf(stderr, "fdopen [Socket] -> %"_lx_" (fd: %d) (H: %"_lx_")\n", (INT)fp, _fd, (INT)h);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3147
        }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3148
# else
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3149
        console_fprintf(stderr, "fdopen [Socket] -> %"_lx_" (fd: %d)\n", (INT)fp, newSock);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3150
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3151
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3152
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3153
# ifdef BUGGY_STDIO_LIB
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3154
    setbuf(fp, NULL);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3155
    __INST(buffered) = false;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3156
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3157
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3158
# if 0
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3159
    // The original code was:
2417
6df5cc0fc132 Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents: 2414
diff changeset
  3160
    __INST(handle) = __MKEXTERNALADDRESS(fp); __STORESELF(handle);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3161
    // 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
  3162
    // 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
  3163
    // 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
  3164
    // object.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3165
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3166
    {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3167
        OBJ t;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3168
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3169
        t = __MKEXTERNALADDRESS(fp);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3170
        __INST(handle) = t;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3171
        __STORE(self, t);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3172
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3173
#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
  3174
%}.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3175
    mode := #readwrite.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3176
    Lobby register:self.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3177
    binary := false.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3178
    port := aSocket port.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3179
    peer := addr.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3180
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3181
    ^ true
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3182
!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3183
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3184
setSocketOption:option argument:arg1 argument:arg2
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3185
    |ok|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3186
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3187
    handle isNil ifTrue:[
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3188
	^ self errorNotOpen
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3189
    ].
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3190
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3191
%{  /* STACK: 32000 */
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
  3192
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3193
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3194
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3195
    if (fp != nil) {
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3196
	SOCKET sock;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3197
	int opt = -1;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3198
	int level = -1;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3199
	int usize = -1;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3200
	int ret;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3201
	union u {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3202
	    BOOL        u_bool;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3203
	    int         u_int;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3204
	    struct linger u_linger;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3205
# ifdef IP_ADD_MEMBERSHIP
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3206
	    struct ip_mreq      u_ip_mreq;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3207
# endif
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3208
	} u;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3209
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3210
	sock = SOCKET_FROM_FILE_OBJECT(fp);
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3211
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3212
# ifdef IP_ADD_MEMBERSHIP
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3213
	if (option == @symbol(IP_DROP_MEMBERSHIP)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3214
	    opt = IP_DROP_MEMBERSHIP;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3215
	    goto dropOrAdd;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3216
	}
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3217
	if (option == @symbol(IP_ADD_MEMBERSHIP)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3218
	    /* add membership to a multicast group */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3219
	    opt = IP_ADD_MEMBERSHIP;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3220
dropOrAdd:
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3221
	    level = IPPROTO_IP;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3222
	    usize = sizeof(u.u_ip_mreq);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3223
	    if (__isByteArrayLike(arg1) && __isByteArrayLike(arg2)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3224
		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
  3225
		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
  3226
	    }
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3227
	    else
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3228
		goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3229
	}
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3230
# endif /* IP_ADD_MEMBERSHIP */
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3231
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3232
# ifdef SO_BROADCAST
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3233
	if (option == @symbol(SO_BROADCAST)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3234
	    /* Enables transmission and receipt of broadcast messages on the socket. */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3235
	    opt = SO_BROADCAST;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3236
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3237
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3238
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3239
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3240
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3241
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3242
# endif /* SO_BROADCAST */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3243
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3244
# ifdef SO_CONDITIONAL
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3245
#  if 0
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3246
	if (option == @symbol(SO_CONDITIONAL)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3247
	    /* 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
  3248
	    opt = SO_CONDITIONAL;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3249
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3250
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3251
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3252
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3253
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3254
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3255
#  endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3256
# endif /* SO_CONDITIONAL */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3257
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3258
# ifdef SO_DEBUG
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3259
	if (option == @symbol(SO_DEBUG)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3260
	    /* Records debugging information. */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3261
	    opt = SO_DEBUG;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3262
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3263
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3264
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3265
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3266
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3267
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3268
# endif /* SO_DEBUG */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3269
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3270
# ifdef SO_DONTLINGER
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3271
	if (option == @symbol(SO_DONTLINGER)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3272
	    /* Does not block close waiting for unsent data to be sent.
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3273
	       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
  3274
	    opt = SO_DONTLINGER;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3275
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3276
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3277
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3278
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3279
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3280
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3281
# endif /* SO_DONTLINGER */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3282
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3283
# ifdef SO_DONTROUTE
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3284
	if (option == @symbol(SO_DONTROUTE)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3285
	    /* Does not route: sends directly to interface.
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3286
	       Succeeds but is ignored on AF_INET sockets;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3287
	       fails on AF_INET6 sockets with WSAENOPROTOOPT.
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3288
	       Not supported on ATM sockets (results in an error). */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3289
	    opt = SO_DONTROUTE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3290
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3291
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3292
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3293
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3294
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3295
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3296
# endif /* SO_DONTROUTE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3297
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3298
#if defined(IPPROTO_TCP) && defined(TCP_NODELAY)
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3299
	if (option == @symbol(TCP_NODELAY)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3300
	    /* enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3301
	    opt = TCP_NODELAY;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3302
	    level = IPPROTO_TCP;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3303
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3304
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3305
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3306
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3307
	}
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3308
# endif /* TCP_NODELAY */
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3309
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3310
# ifdef SO_KEEPALIVE
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3311
	if (option == @symbol(SO_KEEPALIVE)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3312
	    /* 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
  3313
	    opt = SO_KEEPALIVE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3314
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3315
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3316
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3317
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3318
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3319
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3320
# endif /* SO_KEEPALIVE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3321
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3322
# ifdef SO_LINGER
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3323
	if (option == @symbol(SO_LINGER)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3324
	    /* Lingers on close if unsent data is present. */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3325
	    opt = SO_LINGER;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3326
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3327
	    usize = sizeof(u.u_linger);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3328
	    if (arg1 == true) u.u_linger.l_onoff = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3329
	    else if (arg1 == false) u.u_linger.l_onoff = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3330
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3331
	    if (arg2 == nil) u.u_linger.l_linger = 0;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3332
	    else if (__isSmallInteger(arg2))u.u_linger.l_linger = __intVal(arg2);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3333
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3334
	    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
  3335
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3336
# endif /* SO_LINGER */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3337
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3338
# ifdef SO_OOBINLINE
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3339
	if (option == @symbol(SO_OOBINLINE)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3340
	    /* Receives OOB data in the normal data stream. */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3341
	    opt = SO_OOBINLINE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3342
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3343
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3344
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3345
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3346
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3347
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3348
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3349
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3350
# ifdef SO_RCVBUF
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3351
	if (option == @symbol(SO_RCVBUF)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3352
	    /* Specifies the total per-socket buffer space reserved for receives.
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3353
	       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
  3354
	    opt = SO_RCVBUF;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3355
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3356
	    usize = sizeof(u.u_int);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3357
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3358
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3359
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3360
# endif /* SO_RCVBUF */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3361
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3362
# ifdef SO_SNDBUF
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3363
	if (option == @symbol(SO_SNDBUF)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3364
	    /* Specifies the total per-socket buffer space reserved for sends.
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3365
	       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
  3366
	    opt = SO_SNDBUF;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3367
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3368
	    usize = sizeof(u.u_int);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3369
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3370
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3371
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3372
# endif /* SO_SNDBUF */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3373
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3374
# ifdef SO_REUSEADDR
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3375
	if (option == @symbol(SO_REUSEADDR)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3376
	    /* 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
  3377
	    opt = SO_REUSEADDR;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3378
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3379
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3380
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3381
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3382
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3383
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3384
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3385
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3386
# ifdef SO_EXCLUSIVEADDRUSE
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3387
	if (option == @symbol(SO_EXCLUSIVEADDRUSE)) {
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3388
	    /* Enables a socket to be bound for exclusive access.
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3389
	       Does not require administrative privilege.  */
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3390
	    opt = SO_EXCLUSIVEADDRUSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3391
	    level = SOL_SOCKET;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3392
	    usize = sizeof(u.u_bool);
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3393
	    if (arg1 == true) u.u_bool = TRUE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3394
	    else if (arg1 == false) u.u_bool = FALSE;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3395
	    else goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3396
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3397
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3398
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3399
	if (usize == -1) goto argError;
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3400
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  3401
	ok = ( setsockopt( sock, level, opt, (char *)(&u), usize) >= 0) ? true : false;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3402
    }
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3403
argError: ;
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
  3404
#endif /* NO_SOCKET */
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3405
%}.
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3406
    ok isNil ifTrue:[
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3407
	self primitiveFailed
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3408
    ].
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3409
    ok ifFalse:[
2579
d70306ae12ac visual c compilable
Claus Gittinger <cg@exept.de>
parents: 2551
diff changeset
  3410
	'setsocketoption failed' infoPrintCR.
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3411
    ].
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3412
!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3413
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3414
shutdown:howNum
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3415
    "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
  3416
	 0 - read side   (no further reads)
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3417
	     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
  3418
	 1 - write side  (no further writes)
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3419
	     first, all queued data will be delivered to the peer.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3420
	     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
  3421
	     that we will not send more data.
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3422
	 2 - both read side and write side."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3423
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3424
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3425
#ifndef NO_SOCKET
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3426
    OBJ __handle = __INST(handle);
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3427
    if ((__handle != nil) && __isSmallInteger(howNum)) {
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3428
	FILE *fp = __FILEVal(__handle);
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3429
	SOCKET sock = SOCKET_FROM_FILE(fp);
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  3430
	INT how = __intVal(howNum);
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  3431
	INT ret;
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3432
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3433
# ifdef DO_WRAP_CALLS
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3434
	if (how > 0 && __INST(buffered) == true) {
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3435
	    // do a fflush() first, so that fclose() doesn't block
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3436
	    do {
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3437
		__threadErrno = 0;
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  3438
		ret = (INT)__STX_C_CALL1("fflush", fflush, fp);
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3439
	    } while((ret < 0) && (__threadErrno == EINTR));
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3440
	}
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3441
	do {
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3442
	    __threadErrno = 0;
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3443
	    DBGFPRINTF((stderr, "SOCKET: shutDown...\n"));
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  3444
	    ret = (INT)STX_WSA_NOINT_CALL2("shutdown", shutdown, sock, how);
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3445
	    DBGFPRINTF((stderr, "SOCKET: shutDown -> %d (%d)\n", ret, __threadErrno));
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3446
	} 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
  3447
# else
2825
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3448
	if (how > 0 && __INST(buffered) == true) {
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3449
	    fflush(fp);
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3450
	}
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3451
	__BEGIN_INTERRUPTABLE__
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3452
	shutdown(sock, how);
e5b7d2005351 fixed for compilability
Claus Gittinger <cg@exept.de>
parents: 2824
diff changeset
  3453
	__END_INTERRUPTABLE__
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3454
# endif
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3455
    }
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3456
#endif
1339
cc973a42aa54 shutdown: added
Claus Gittinger <cg@exept.de>
parents: 1338
diff changeset
  3457
%}.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3458
! !
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3459
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3460
!Socket methodsFor:'printing & storing'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3461
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3462
printOn:aStream
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3463
    aStream nextPutAll:self className; nextPutAll:'(protocol='.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3464
    protocol printOn:aStream.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3465
    aStream nextPutAll:' port='.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3466
    self port printOn:aStream.
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  3467
    aStream nextPutAll:' peer='.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  3468
    peer printOn:aStream.
1842
88c04d1e9e6b changed #printOn:
Stefan Vogel <sv@exept.de>
parents: 1839
diff changeset
  3469
    aStream nextPut:$).
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3470
! !
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3471
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3472
!Socket methodsFor:'queries'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3473
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3474
domain
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3475
    "return the sockets addressing domain (i.e. #AF_INET, #AF_INET6, #AF_UNIX, ...)"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3476
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3477
    ^ domain
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3478
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3479
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3480
getFullPeerAddress
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3481
    "implemented for swazoo project (primitive code can't be loaded as extension)
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3482
     Answer my own address (I am bound to this address).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3483
     Note that this address may change after a connect or accept."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3484
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3485
    |error domainClass addr|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3486
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3487
    handle isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3488
        ^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3489
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3490
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3491
    domainClass := self class socketAddressClassForDomain:domain.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3492
    domainClass isNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3493
        ^ self error:'invalid (unsupported) domain'.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3494
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3495
    addr := domainClass new.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3496
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3497
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3498
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3499
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3500
    SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3501
    int ret;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3502
    union sockaddr_u sa;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3503
    unsigned int alen = sizeof(sa);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3504
    char *addrP;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3505
    int addrObjSize, nAddrInstBytes;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3506
    OBJ addrClass;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3507
    int nAddrInstVars;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3508
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3509
    if (!__isNonNilObject(addr) || !__isBytes(addr)) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3510
        DBGPRINTF(("SOCKET: bad addr\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3511
        error = @symbol(badArgument);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3512
        goto err;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3513
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3514
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3515
    addrClass = __qClass(addr);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3516
    nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3517
    nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3518
    addrObjSize = __qSize(addr) - nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3519
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3520
    sock = SOCKET_FROM_FILE_OBJECT(fp);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3521
    ret = getpeername(sock, (struct sockaddr *)&sa, &alen);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3522
    if (ret < 0) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3523
# ifdef WIN32
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3524
        errno = WSAGetLastError();
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3525
# endif
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3526
        DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3527
        error = __MKSMALLINT(errno);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3528
        goto err;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3529
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3530
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3531
    if (addrObjSize < alen) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3532
        DBGPRINTF(("SOCKET: bad addr\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3533
        error = @symbol(badArgument);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3534
        goto err;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3535
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3536
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3537
    addrP = (char *)__InstPtr(addr) + nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3538
    memcpy(addrP, (char *)&sa, alen);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3539
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3540
err:;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3541
#else /* NO_SOCKET */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3542
    error = @symbol(notImplemented);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3543
#endif /* NO_SOCKET */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3544
%}.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3545
    error notNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3546
        ^ self errorReporter reportOn:error
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3547
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3548
    ^ addr
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3549
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3550
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3551
getFullSocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3552
    "implemented for swazoo project (primitive code can't be loaded as extension)
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3553
     Answer my own address (I am bound to this address).
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3554
     Note that this address may change after a connect or accept."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3555
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3556
    |error domainClass addr|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3557
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3558
    handle isNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3559
        ^ self errorNotOpen
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3560
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3561
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3562
    domainClass := self class socketAddressClassForDomain:domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3563
    domainClass isNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3564
        ^ self error:'invalid (unsupported) domain'.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3565
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3566
    addr := domainClass new.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3567
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3568
%{
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3569
#ifndef NO_SOCKET
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3570
    OBJ fp = __INST(handle);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3571
    SOCKET sock;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3572
    int ret;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3573
    union sockaddr_u sa;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3574
    unsigned int alen = sizeof(sa);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3575
    char *addrP;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3576
    int addrObjSize, nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3577
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3578
    if (!__isNonNilObject(addr) || !__isBytes(addr)) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3579
        DBGPRINTF(("SOCKET: bad addr\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3580
        error = @symbol(badArgument);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3581
        goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3582
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3583
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3584
    {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3585
        OBJ addrClass;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3586
        int nAddrInstVars;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3587
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3588
        addrClass = __qClass(addr);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3589
        nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3590
        nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3591
        addrObjSize = __qSize(addr) - nAddrInstBytes;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3592
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3593
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3594
    sock = SOCKET_FROM_FILE_OBJECT(fp);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3595
    ret = getsockname(sock, (struct sockaddr *)&sa, &alen);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3596
    if (ret < 0) {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3597
# ifdef WIN32
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3598
        errno = WSAGetLastError();
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3599
# endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3600
        DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3601
        error = __MKSMALLINT(errno);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3602
        goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3603
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3604
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3605
    if (addrObjSize < alen) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3606
        DBGPRINTF(("SOCKET: bad addr\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3607
        error = @symbol(badArgument);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3608
        goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3609
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3610
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3611
    addrP = (char *)__InstPtr(addr) + nAddrInstBytes;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3612
    memcpy(addrP, (char *)&sa, alen);
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3613
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3614
err:;
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3615
#else /* NO_SOCKET */
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3616
    error = @symbol(notImplemented);
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3617
#endif /* NO_SOCKET */
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3618
%}.
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3619
    error notNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3620
        ^ self errorReporter reportOn:error
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3621
    ].
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3622
    ^ addr
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3623
!
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3624
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3625
getName
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3626
    "return the name; here, we return the ports name"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3627
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3628
    ^ self port printString
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3629
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3630
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3631
getPeer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3632
    "ST-80 compatibility: return an IPSocketAddress instance representing
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3633
     my hostname/port combination.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3634
     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
  3635
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
  3636
    ^ peer
102
5dc264007513 added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
  3637
!
5dc264007513 added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
  3638
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3639
getPeerName
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3640
    "return the peer name; thats the hostname (or dotted name) of the
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3641
     partners host after an accept."
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3642
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
  3643
    peerName isNil ifTrue:[
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  3644
	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
  3645
    ].
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3646
    ^ peerName
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3647
!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3648
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3649
getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3650
    "implemented for swazoo project primitive code cant load as extension
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3651
     answer my own address (I am bound to this address).
1146
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3652
     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
  3653
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3654
    ^ self getFullSocketAddress hostAddress
1146
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3655
!
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3656
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3657
isActive
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3658
    "return true, if the receiver has a connection"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3659
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3660
    ^ handle notNil
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3661
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3662
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3663
isConnected
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3664
    "return true, if the receiver has a connection"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3665
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3666
    ^ self isActive
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3667
	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
  3668
!
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3669
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3670
port
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3671
    "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
  3672
     - so this is the local port."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3673
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3674
"/    port isNil ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3675
"/        port := self getFullSocketAddress port.
1505
Stefan Vogel <sv@exept.de>
parents: 1504
diff changeset
  3676
"/    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3677
71
claus
parents: 63
diff changeset
  3678
    ^ port
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3679
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3680
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3681
socketAddressClass
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3682
    "get the matchon SocketAddress class for this socket"
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3683
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3684
    |domainClass|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3685
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3686
    domainClass := self class socketAddressClassForDomain:domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3687
    domainClass isNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3688
        ^ self error:'invalid (unsupported) domain'.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3689
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3690
    ^ domainClass.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3691
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3692
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3693
type
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3694
    "return the sockets connection type (i.e. #datagram, #stream etc)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3695
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3696
    ^ socketType
81
claus
parents: 79
diff changeset
  3697
! !
claus
parents: 79
diff changeset
  3698
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3699
!Socket methodsFor:'socket setup'!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3700
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3701
domain:domainArg type:typeArg
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3702
    "set up socket with domain and type.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3703
     This is a low level entry; no binding, listening or connect
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3704
     is done. Both arguments must be symbols from one of
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3705
      #AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3706
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  3707
    ^ self domain:domainArg type:typeArg protocol:nil
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3708
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3709
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3710
domain:domainArg type:typeArg protocol:protocolNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3711
    "set up socket with domain, type and protocol number.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3712
     This is a low level entry; no binding, listening or connect
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3713
     is done. Both arguments must be symbols from one of
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3714
     #AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3715
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3716
    |domainName domainCode typeCode error|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3717
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3718
    handle notNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3719
        ^ self errorAlreadyOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3720
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3721
    domainName := SocketAddress domainCodeFromName:domainArg.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3722
    domainCode := OperatingSystem domainCodeOf:domainName.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3723
    typeCode := OperatingSystem socketTypeCodeOf:typeArg.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3724
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3725
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3726
#ifndef NO_SOCKET
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3727
    FILE *fp;
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  3728
    int dom, typ, proto = 0;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3729
    int on = 1;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3730
    SOCKET sock;
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3731
# ifdef WIN32
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3732
#  ifndef WSA_FLAG_NO_HANDLE_INHERIT
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3733
#   define WSA_FLAG_NO_HANDLE_INHERIT 0x80
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3734
#  endif
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3735
    static int noInheritFlag = WSA_FLAG_NO_HANDLE_INHERIT;
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3736
# endif
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3737
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3738
    if (! __isSmallInteger(domainCode)) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3739
        error = @symbol(badArgument1);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3740
        goto out;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3741
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3742
    if (! __isSmallInteger(typeCode)) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3743
        error = @symbol(badArgument2);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3744
        goto out;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3745
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3746
    if (protocolNumber != nil) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3747
        if (!__isSmallInteger(protocolNumber)) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3748
            error = @symbol(badArgument3);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3749
            goto out;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3750
        }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3751
        proto = __intVal(protocolNumber);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3752
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3753
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3754
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3755
    /*
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3756
     * get address and protocol-family
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3757
     */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3758
    dom = __intVal(domainCode);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3759
    typ = __intVal(typeCode);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3760
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3761
# ifdef WIN32
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3762
    sock = WSASocket(dom, typ, proto, 0, 0, noInheritFlag);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3763
    if (sock == INVALID_SOCKET && noInheritFlag) {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3764
        // tried to open socket with WSA_FLAG_NO_HANDLE_INHERIT
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3765
        // This fails on older windows versions, e.g. Windows XP
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3766
        sock = WSASocket(dom, typ, proto, 0, 0, 0);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3767
        if (sock != INVALID_SOCKET) {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3768
            // no error without WSA_FLAG_NO_HANDLE_INHERIT,
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3769
            // never use this flag again!
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3770
            noInheritFlag = 0;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3771
        }
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3772
    }
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3773
    if (sock == INVALID_SOCKET) {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3774
        errno = WSAGetLastError();
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3775
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3776
# else  // !WIN32
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3777
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3778
    sock = socket(dom, typ, proto);
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3779
# if defined(EPROTONOSUPPORT) /* for SGI */
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3780
    if ((sock < 0) && (proto != 0) && (errno == EPROTONOSUPPORT)) {
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3781
        DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3782
        proto = 0;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3783
        sock = socket(dom, typ, 0);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3784
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3785
# endif
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3786
    if (sock < 0) {
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3787
# endif // !WIN32
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3788
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3789
        DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3790
        __INST(lastErrorNumber) = __MKSMALLINT(errno);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3791
    } else {
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3792
# if defined(SET_LINGER_WHEN_CREATING_SOCKET) && defined(SO_LINGER)
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3793
        {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3794
            struct linger l;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3795
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3796
            l.l_onoff = 1;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3797
            l.l_linger = 30;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3798
            setsockopt( sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3799
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3800
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3801
# ifdef WIN32
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3802
        /*
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3803
         * make it blocking
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3804
         */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3805
        {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3806
            unsigned long zero = 0;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3807
            ioctlsocket(sock, FIONBIO, &zero);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3808
        }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3809
        /*
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3810
         * make it a FILE *
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3811
         */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3812
        {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3813
            int _fd;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3814
            __stxWrapApiEnterCritical();
2915
896fffb0f02e mingw64
Claus Gittinger <cg@exept.de>
parents: 2909
diff changeset
  3815
#  if defined( __BORLANDC__ ) || defined( __MINGW__ )
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3816
            _fd = _open_osfhandle((long)sock, 0);
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3817
#  else
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3818
            _fd = (int)sock;
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3819
#  endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3820
            fp = fdopen(_fd, "r+");
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3821
            __stxWrapApiLeaveCritical();
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3822
            DBGPRINTF(("SOCKET: sock=%d fd=%d fp=%"_lx_"\n",sock, _fd, (INT)fp));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3823
        }
2680
ff00563f0444 Protect against concurrent access when doing fclose and fdopen
Stefan Vogel <sv@exept.de>
parents: 2579
diff changeset
  3824
# else
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3825
        fp = fdopen(sock, "r+");
2680
ff00563f0444 Protect against concurrent access when doing fclose and fdopen
Stefan Vogel <sv@exept.de>
parents: 2579
diff changeset
  3826
# endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3827
        if (! fp) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3828
            DBGPRINTF(("SOCKET: fdopen call failed\n"));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3829
            __INST(lastErrorNumber) = __MKSMALLINT(errno);
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3830
# ifdef DO_WRAP_CALLS
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3831
            {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3832
                INT ret;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3833
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3834
                do {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3835
                  __threadErrno = 0;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3836
                  ret = (INT)STX_WSA_NOINT_CALL1("closesocket", closesocket, sock);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3837
                } while ((ret < 0) && (__threadErrno == EINTR));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3838
            }
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3839
# else
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3840
            __BEGIN_INTERRUPTABLE__
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3841
            closesocket(sock);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3842
            DBGFPRINTF((stderr, "SOCKET: fdopen failed (%d)\n", sock));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3843
            __END_INTERRUPTABLE__
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3844
# endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3845
        } else {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3846
            __INST(handleType) = @symbol(socketFilePointer);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3847
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3848
            if ((@global(FileOpenTrace) == true) || __debugging__) {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3849
                console_fprintf(stderr, "fdopen [Socket] -> %"_lx_"\n", (INT)fp);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3850
            }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3851
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3852
# if 0
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3853
            // The original code was:
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3854
            __INST(handle) = __MKEXTERNALADDRESS(fp); __STORESELF(handle);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3855
            // but for that, gcc generates wrong code, which loads self (volatile) into
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3856
            // a register (bp), then calls __MKEXTERNALADDRESS, then stores indirect bp.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3857
            // That is wrong if a scavenge occurs in __MKEXTERNALADDRESS, as bp is now still pointing to the old
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3858
            // object.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3859
# endif
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3860
            {
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3861
                OBJ t;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3862
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3863
                t = __MKEXTERNALADDRESS(fp);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3864
                __INST(handle) = t;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3865
                __STORE(self, t);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3866
            }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3867
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3868
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3869
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3870
out:;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3871
%}.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3872
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3873
    "all ok?"
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3874
    handle notNil ifTrue:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3875
        domain := domainArg.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3876
        socketType := typeArg.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3877
        Lobby register:self.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3878
    ] ifFalse:[
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3879
        error notNil ifTrue:[
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3880
            ^ self primitiveFailed:error.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3881
        ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3882
        ^ self openError:lastErrorNumber.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3883
    ].
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3884
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3885
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3886
     Socket new domain:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3887
     Socket new domain:#AF_UNIX type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3888
    "
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3889
! !
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3890
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3891
!Socket methodsFor:'specials'!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3892
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3893
receiveBufferSize
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3894
    "get the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3895
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3896
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3897
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3898
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3899
	^ self errorNotOpen
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3900
    ].
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3901
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3902
#if defined(SO_RCVBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3903
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3904
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3905
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3906
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3907
	unsigned int size;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3908
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3909
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3910
	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
  3911
	    RETURN( __MKSMALLINT(opt) );
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3912
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3913
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3914
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3915
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3916
    ^ nil
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3917
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3918
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3919
receiveBufferSize:size
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3920
    "set the receive buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3921
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3922
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3923
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3924
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3925
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3926
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3927
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3928
#if defined(SO_RCVBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3929
    if (__isSmallInteger(size)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3930
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3931
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3932
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3933
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3934
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  3935
	opt = __intVal(size);
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3936
	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
  3937
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3938
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3939
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3940
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3941
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3942
    ^ false
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3943
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3944
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3945
receiveTimeout
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3946
    "get the receive timeout in millis - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3947
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3948
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3949
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3950
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3951
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3952
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3953
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3954
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3955
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3956
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3957
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3958
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3959
	int sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3960
	int len;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3961
	int __millis;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3962
	struct timeval tv = {0, 0};
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3963
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3964
	sock = fileno(__FILEVal(fp));
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3965
	len = sizeof(struct timeval);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3966
	if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, &len) == 0) {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3967
	    __millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3968
	    millis = __mkSmallInteger(__millis);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3969
# if 0
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3970
	    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
  3971
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3972
	} else {
1839
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
  3973
	    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
  3974
	}
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3975
    }
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3976
#endif
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3977
%}.
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3978
    millis notNil ifTrue:[^ millis / 1000 ].
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3979
    ^ nil
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3980
!
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3981
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3982
receiveTimeout:seconds
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3983
    "set the receive timeout - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3984
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3985
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3986
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3987
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3988
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3989
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3990
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3991
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3992
    millis := (seconds * 1000) rounded.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3993
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3994
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3995
    if (__isSmallInteger(millis)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3996
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3997
	int sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3998
	int __millis = __intVal(millis);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3999
	struct timeval tv = {0, 0};
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4000
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4001
	sock = fileno(__FILEVal(fp));
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4002
	tv.tv_sec = __millis / 1000;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4003
	tv.tv_usec = (__millis % 1000) * 1000;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4004
# if 0
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4005
	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
  4006
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4007
	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
  4008
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4009
	}
1839
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
  4010
	console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno);
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4011
    }
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4012
#endif
71
claus
parents: 63
diff changeset
  4013
%}.
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4014
    ^ false
81
claus
parents: 79
diff changeset
  4015
!
claus
parents: 79
diff changeset
  4016
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4017
sendBufferSize
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4018
    "get the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4019
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4020
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4021
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4022
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4023
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4024
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4025
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4026
#if defined(SO_SNDBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4027
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4028
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4029
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4030
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4031
	unsigned int size;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4032
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4033
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4034
	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
  4035
	    RETURN( __MKSMALLINT(opt) );
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4036
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4037
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4038
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4039
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4040
    ^ nil
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4041
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4042
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4043
sendBufferSize:size
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4044
    "set the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4045
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4046
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4047
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4048
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4049
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4050
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4051
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4052
#if defined(SO_SNDBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4053
    if (__isSmallInteger(size)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4054
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4055
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4056
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4057
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4058
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  4059
	opt = __intVal(size);
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4060
	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
  4061
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4062
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4063
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4064
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4065
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4066
    ^ false
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4067
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4068
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4069
sendTimeout
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4070
    "get the send timeout in millis - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4071
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4072
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4073
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4074
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4075
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4076
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4077
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4078
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4079
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4080
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4081
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4082
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4083
	int sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4084
	int len;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4085
	int __millis;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4086
	struct timeval tv = {0, 0};
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4087
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4088
	sock = fileno(__FILEVal(fp));
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4089
	len = sizeof(struct timeval);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4090
	if (getsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, &len) == 0) {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4091
	    __millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4092
	    millis = __mkSmallInteger(__millis);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4093
# if 0
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4094
	    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
  4095
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4096
	} else {
1839
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
  4097
	    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
  4098
	}
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4099
    }
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4100
#endif
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4101
%}.
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4102
    millis notNil ifTrue:[^ millis / 1000 ].
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4103
    ^ nil
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4104
!
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4105
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4106
sendTimeout:seconds
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4107
    "set the send timeout - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4108
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4109
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4110
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4111
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4112
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4113
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4114
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4115
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4116
    millis := (seconds * 1000) rounded.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4117
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4118
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4119
    if (__isSmallInteger(millis)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4120
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4121
	int sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4122
	int __millis = __intVal(millis);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4123
	struct timeval tv = {0, 0};
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4124
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4125
	sock = fileno(__FILEVal(fp));
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4126
	tv.tv_sec = __millis / 1000;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4127
	tv.tv_usec = (__millis % 1000) * 1000;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4128
# if 0
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4129
	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
  4130
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4131
	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
  4132
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4133
	}
1839
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
  4134
	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
  4135
    }
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4136
#endif
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4137
%}.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4138
    ^ false
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4139
1340
eb9b1f1340e5 setTCP_NODELAY
Claus Gittinger <cg@exept.de>
parents: 1339
diff changeset
  4140
!
eb9b1f1340e5 setTCP_NODELAY
Claus Gittinger <cg@exept.de>
parents: 1339
diff changeset
  4141
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4142
setTCPCork:aBoolean
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4143
    "enable/disable TCP_CORK (do-not-send-partial-frames)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4144
     For special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4145
     Not all OperatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4146
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4147
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  4148
    ^ self setSocketOption:#'TCP_CORK' argument:aBoolean argument:nil.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4149
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4150
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4151
setTCPNoDelay:aBoolean
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4152
    "enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4153
     For special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4154
     Not all OperatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4155
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4156
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  4157
    ^ self setSocketOption:#'TCP_NODELAY' argument:aBoolean argument:nil.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4158
! !
82
claus
parents: 81
diff changeset
  4159
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  4160
!Socket methodsFor:'testing'!
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  4161
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  4162
isSocket
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  4163
    ^true
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  4164
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  4165
    "Created: / 17-08-2012 / 15:32:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  4166
! !
3043
8b7ec7116ae3 class: Socket
Stefan Vogel <sv@exept.de>
parents: 2998
diff changeset
  4167
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4168
!Socket methodsFor:'waiting'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4169
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4170
waitForNewConnectionOrDataOnAny:otherConnections timeout:timeoutSeconds
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4171
    "suspend the current process, until either a new connection comes
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4172
     in at the receiver, or data arrives on any of the otherConnections.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4173
     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
  4174
     For an old connection, that socket is returned.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4175
     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
  4176
     or nil, if a timeout occured.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4177
     This method implements the inner wait-primitive of a multi-connection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4178
     server application."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4179
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4180
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4181
    |wasBlocked sema|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4182
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4183
    "first, a quick check if data is already available"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4184
    self canReadWithoutBlocking ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4185
	^ self accept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4186
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4187
    otherConnections do:[:aConnection |
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4188
	aConnection canReadWithoutBlocking ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4189
	    ^ aConnection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4190
	]
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4191
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4192
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4193
    [
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4194
	"check again - prevent incoming interrupts from disturbing our setup"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4195
	wasBlocked := OperatingSystem blockInterrupts.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4196
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4197
	self canReadWithoutBlocking ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4198
	    ^ self accept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4199
	].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4200
	otherConnections do:[:aConnection |
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4201
	    aConnection canReadWithoutBlocking ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4202
		^ aConnection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4203
	    ]
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4204
	].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4205
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4206
	"nope - must wait"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4207
	sema := Semaphore new name:'multiReadWait'.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4208
	otherConnections do:[:aConnection |
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4209
	    Processor signal:sema onInput:(aConnection fileDescriptor).
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4210
	].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4211
	Processor signal:sema onInput:(self fileDescriptor).
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4212
	timeoutSeconds notNil ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4213
	    Processor signal:sema afterSeconds:timeoutSeconds
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4214
	].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4215
	Processor activeProcess state:#ioWait.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4216
	sema wait.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4217
    ] ensure:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4218
	sema notNil ifTrue:[Processor disableSemaphore:sema].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4219
	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4220
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4221
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4222
    "see who it was ..."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4223
    self canReadWithoutBlocking ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4224
	^ self accept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4225
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4226
    otherConnections do:[:aConnection |
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4227
	aConnection canReadWithoutBlocking ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4228
	    ^ aConnection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4229
	]
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4230
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4231
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4232
    "none - a timeout"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4233
    ^ nil
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4234
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4235
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4236
waitForNewConnectionWithTimeout:timeoutSeconds
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4237
    "suspend the current process, until a new connection comes
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4238
     in at the receiver or a timeout occurs.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4239
     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
  4240
     Returns nil, if a timeout occured.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4241
     This method implements the inner wait-primitive of a single-connection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4242
     server application."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4243
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4244
    (self readWaitWithTimeout:timeoutSeconds) ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4245
	"a timeout occurred - no connection within timeout"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4246
	^ nil
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4247
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4248
    ^ self accept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4249
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4250
464
600b101a7035 alen in accept
Claus Gittinger <cg@exept.de>
parents: 463
diff changeset
  4251
!Socket class methodsFor:'documentation'!
206
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4252
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4253
version
3151
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  4254
    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.293 2014-01-28 12:48:23 cg Exp $'
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4255
!
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4256
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4257
version_CVS
3151
4f0a7da06c69 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3150
diff changeset
  4258
    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.293 2014-01-28 12:48:23 cg Exp $'
206
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4259
! !
2953
855e7a58faa0 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2915
diff changeset
  4260