comparison host/tools/ecostest/common/eCosTestSocket.h @ 76:435cced73e2f ecos-v1_3_1-release

eCos v1.3.1 merged from eCos master repository on 2000-03-27-23:22:51-BST
author jlarmour
date Tue, 28 Mar 2000 14:10:45 +0000
parents
children
comparison
equal deleted inserted replaced
75:41bf073c0c32 76:435cced73e2f
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 // eCosTestSocket.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 #include "eCosStd.h"
43 #include "Collections.h"
44
45 #ifndef _SOCKETUTILS_H
46 #define _SOCKETUTILS_H
47
48 class CeCosTestSerial;
49
50 class CeCosTestSocket {
51 public:
52 static bool SameHost (LPCTSTR host1,LPCTSTR host2);
53
54 typedef bool (CALLBACK FilterFunc)(void *&,unsigned int &,CeCosTestSerial&,CeCosTestSocket &,void *);
55 static int CeCosTestSocket::SSRead (CeCosTestSerial &serial,CeCosTestSocket &socket,void *pBuf,unsigned int nSize,unsigned int &nRead,bool *pbStop);
56
57 static bool ConnectSocketToSerial (int nListenSock,LPCTSTR pszPort, int nBaud,FilterFunc *pSerialToSocketFilterFunc=0,void *pSerialParam=0,FilterFunc *pSocketToSerialFilterFunc=0,void *pSocketParam=0,bool *pbStop=0);
58 static bool ConnectSocketToSerial (CeCosTestSocket &socket,CeCosTestSerial &serial,FilterFunc *pSerialToSocketFilterFunc=0,void *pSerialParam=0, FilterFunc *pSocketToSerialFilterFunc=0,void *pSocketParam=0,bool *pbStop=0);
59 bool ConnectSocketToSocket (CeCosTestSocket &o,FilterFunc *pSocketToSocketFilterFunc1,FilterFunc *pSocketToSocketFilterFunc2,void *pParam,bool *pbStop);
60
61 typedef bool (CALLBACK StopFunc)(void *);
62
63 static bool IsSocketError(int n);
64
65 enum {NOTIMEOUT=-1,DEFAULTTIMEOUT=-2}; // No explicit timeout specified
66
67 // Listen and this form of constructor used to act as server
68 static int Listen(int nTcpPort);
69 CeCosTestSocket (); // Caller promises to call Accept() or Connect() later
70
71 // Accept-like ctor (act as server)
72 CeCosTestSocket (int sock /*result of previous call of Listen*/, bool *pbStop=0);
73 // Connect-like ctor (act as client)
74 CeCosTestSocket (String strHost,int port,Duration dTimeout);
75
76 bool Accept(int sock /*result of previous call of Listen*/, bool *pbStop=0);
77 // This form of constructor used to act as client
78 bool Connect(String strHost,int port,Duration dTimeout);
79 ~CeCosTestSocket();
80
81 int Client() const { return m_nClient; }
82 static String ClientName(int nClient);
83
84 int Sock() const { return m_nSock; }
85
86 // Set the default timeout for all operations
87 void SetTimeout (Duration dTimeout) { m_nDefaultTimeout=dTimeout; }
88
89 bool send(const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0){
90 return sendrecv(true,pData,nLength,pszMsg,dTimeout,pFunc,pParam);
91 }
92 bool recv(const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0){
93 return sendrecv(false,pData,nLength,pszMsg,dTimeout,pFunc,pParam);
94 }
95 bool Ok() { return -1!=m_nSock; }
96 // Close the given socket
97 bool Close () { return CloseSocket(m_nSock); }
98
99 int SocketError() { return m_nErr; }
100 // Return last socket error, translated to a string
101 String SocketErrString();
102 static String SocketErrString(int nErr);
103
104 bool recvInteger (int &n,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
105 bool sendInteger (int n,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
106
107 bool recvString (String &str,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
108 bool sendString (const String &str,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
109
110 static bool CloseSocket (int &sock);
111 bool Peek (unsigned int &nAvail);
112 static String HostPort(LPCTSTR pszHost,int nPort);
113 static bool ParseHostPort (LPCTSTR pszHostPort, String &pszHost, int &nPort);
114 static bool IsLegalHostPort (LPCTSTR pszHostPort);
115
116 protected:
117 Duration m_nDefaultTimeout;
118 Duration TimeoutDuration (Duration dTimeout);
119 // Set appropriate socket options (most importantly, non-blocking mode)
120 bool SetSocketOptions ();
121 int m_nSock;
122 int m_nClient;
123 int m_nErr;
124 void SaveError() {
125 #ifdef _WIN32
126 m_nErr=WSAGetLastError();
127 #else // UNIX
128 m_nErr=errno;
129 #endif
130 }
131 bool sendrecv(bool bSend,const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0);
132
133 };
134 #endif