]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/Utils/common/eCosSocket.h
Initial revision
[karo-tx-redboot.git] / tools / src / tools / Utils / common / eCosSocket.h
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 //=================================================================
26 //
27 //        eCosSocket.h
28 //
29 //        Socket test class
30 //
31 //=================================================================
32 //=================================================================
33 //#####DESCRIPTIONBEGIN####
34 //
35 // Author(s):     sdf
36 // Contributors:  sdf
37 // Date:          1999-04-01
38 // Description:   This class abstracts tcp/ip sockets for use in the testing infrastructure
39 // Usage:
40 //
41 //####DESCRIPTIONEND####
42 //=================================================================
43 // This class is a host-independent interface to a TCP/IP socket
44 // There are two flavours of socket - server and client.
45 // Server sockets listen (accept) on a socket number.  Client sockets connect to a host:port.
46 // The class can be used thus:
47 //   Server:
48 //      CeCosSocket sock; // no-argument ctor
49 //      if(-1!=sock.Listen(6000)){
50 //        ...      
51 //      }
52 //   Client:
53 //      CeCosSocket sock;
54 //      if(sock.Connect(_T("ginga"),5000)){
55 //        ...      
56 //      }
57 // In each of the above cases the socket is closed automatically by the dtor.
58 //
59 // Alternatively, the ctor can be used directly:
60 //   Server:
61 //      CeCosSocket sock(6000));
62 //      ...      
63 //   Client:
64 //      CeCosSocket sock(_T("ginga"),5000));
65 //      ...      
66 //
67 //=================================================================
68 #include "eCosStd.h"
69 #include "Collections.h"
70
71 #ifndef _SOCKETUTILS_H
72 #define _SOCKETUTILS_H
73
74 class CeCosSerial;
75
76 class CeCosSocket {
77 public: 
78         static const String GetHostByName(LPCTSTR pszHost);
79
80   // These functions must be called before any other operation is carried out:
81         static bool Init();
82         static void Term();
83
84   typedef bool (CALLBACK FilterFunc)(void *&,unsigned int &,CeCosSerial&,CeCosSocket &,void *);
85
86   // A function that causes an operation to stop - i.e. it when it returns true the operation is aborted.
87   typedef bool (CALLBACK StopFunc)(void *);
88   
89   enum {NOTIMEOUT=0x7fffffff-1,DEFAULTTIMEOUT=-2}; // No explicit timeout specified
90   
91   // Listen and this form of constructor used to act as server
92   static int Listen(int nTcpPort);
93   CeCosSocket (); // Caller promises to call Accept() or Connect() later
94   
95   // Accept-like ctor (act as server)
96   CeCosSocket (int sock /*result of previous call of Listen*/, bool *pbStop=0);
97   // Connect-like ctor (act as client)
98   CeCosSocket (LPCTSTR pszHostPort,Duration dTimeout=NOTIMEOUT);
99   
100   bool Accept(int sock /*result of previous call of Listen*/, bool *pbStop=0);
101   // This form of constructor used to act as client
102   bool Connect(LPCTSTR pszHostPort,Duration dTimeout=NOTIMEOUT);
103   ~CeCosSocket();
104   
105   int Client() const { return m_nClient; }
106   static String ClientName(int nClient);
107   
108   int Sock() const { return m_nSock; }
109   
110   // Set the default timeout for all operations
111   void SetTimeout (Duration dTimeout) { m_nDefaultTimeout=dTimeout; }
112   
113   // Use to test success after opening with the ctor:
114   bool Ok() { return -1!=m_nSock; }
115
116   // Close the given socket
117   bool Close () { return CloseSocket(m_nSock); }
118   
119   // Return last error on this socket
120   int SocketError() { return m_nErr; }
121
122   // Return last socket error, translated to a string
123   String SocketErrString();
124   static String SocketErrString(int nErr);
125
126   // Read and write functions
127
128   // Untyped: these versions allow the operation to be aborted either by timeout or by the "stop func" returning true.
129   bool send(const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0){
130     return sendrecv(true,pData,nLength,pszMsg,dTimeout,pFunc,pParam);
131   }
132   bool recv(const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0){
133     return sendrecv(false,pData,nLength,pszMsg,dTimeout,pFunc,pParam);
134   }
135
136   // Read/write an integer (this can be used between machines of different endianness)
137   bool recvInteger (int &n,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
138   bool sendInteger (int n,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
139
140   // Read/write a string
141   bool recvString  (String &str,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
142   bool sendString  (const String &str,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
143   
144   static bool CloseSocket (int &sock);
145   bool Peek (unsigned int &nAvail);
146
147   // Miscellaneous helper functions:
148
149   // Combine string and integer to the form host:port:
150   static String HostPort(LPCTSTR pszHost,int nPort);
151   // Decompose (opposite of the above):
152   static bool ParseHostPort (LPCTSTR pszHostPort, String &pszHost, int &nPort);
153   // Just check for legality:
154   static bool IsLegalHostPort (LPCTSTR pszHostPort);
155   // Are these two hosts really the same?
156   static bool SameHost (LPCTSTR host1,LPCTSTR host2);
157   // Set up a connection between a serial port and a socket.  Traffic is simply passed between them.
158   static bool ConnectSocketToSerial (int nListenSock,LPCTSTR pszPort, int nBaud,FilterFunc *pSerialToSocketFilterFunc=0,void *pSerialParam=0,FilterFunc *pSocketToSerialFilterFunc=0,void *pSocketParam=0,bool *pbStop=0);
159   static bool ConnectSocketToSerial (CeCosSocket &socket,CeCosSerial &serial,FilterFunc *pSerialToSocketFilterFunc=0,void *pSerialParam=0, FilterFunc *pSocketToSerialFilterFunc=0,void *pSocketParam=0,bool *pbStop=0);
160
161   static LPCTSTR MyHostName();
162   static LPCTSTR MySimpleHostName();
163
164   // Set up a connection between two sockets.  Traffic is simply passed between them.
165   bool ConnectSocketToSocket (CeCosSocket &o,FilterFunc *pSocketToSocketFilterFunc1,FilterFunc *pSocketToSocketFilterFunc2,void *pParam,bool *pbStop);
166   
167   enum   SSReadResult {SS_SOCKET_ERROR=-1,SS_SOCKET_READ=1,SS_SERIAL_ERROR=-2,SS_SERIAL_READ=2,SS_STOPPED=0};
168
169 protected:
170
171   // Blocking read on one or other of the data sources:
172   // Result:  -1 - socket error occurred
173   //           1 - data read from socket
174   //          -2 - serial error occurred
175   //           2 - data read from serial
176
177   static SSReadResult SSRead (CeCosSerial &serial,CeCosSocket &socket,void *pBuf,unsigned int nSize,unsigned int &nRead,bool *pbStop);
178   
179   Duration m_nDefaultTimeout;
180   Duration TimeoutDuration (Duration dTimeout);
181   // Set appropriate socket options (most importantly, non-blocking mode)
182   bool SetSocketOptions ();
183   int m_nSock;
184   int m_nClient;
185   int m_nErr;
186   void SaveError() { 
187 #ifdef _WIN32
188   m_nErr=WSAGetLastError();
189 #else // UNIX
190   m_nErr=errno;
191 #endif
192   }
193   bool sendrecv(bool bSend,const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0);
194   
195 };
196 #endif