OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [Utils/] [common/] [eCosSocket.h] - Blame information for rev 790

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.