Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 dhylands 1
/****************************************************************************
2
*
3
*     Copyright (c) 2003 Dave Hylands
4
*           All Rights Reserved
5
*
6
*   Permission is granted to any individual or institution to use, copy, or
7
*   redistribute this software so long as it is not sold for profit, and that
8
*   this copyright notice is retained.
9
*
10
****************************************************************************/
11
/**
12
*
13
*   @file   TcpComm.h
14
*
15
*   @brief  Implements the abstract communications device using a TCP/IP port.
16
*
17
****************************************************************************/
18
/**
19
*  @defgroup    TcpComm  TCP/IP Communications Device
20
*  @ingroup     CommDev
21
*
22
*   @brief  Implements the abstract communications device using a TCP/IP port.
23
*/
24
 
25
#if !defined( TCPCOMM_H )
26
#define TCPCOMM_H                   ///< Include Guard
27
 
28
// ---- Include Files -------------------------------------------------------
29
 
30
#include "CommDev.h"
31
 
32
#if defined( WIN32_LEAN_AND_MEAN )
33
#   error Sockets requires WIN32_LEAN_AND_MEAN to be undefined
34
#endif
35
#if !defined( _WINDOWS_ )
36
#   include <windows.h>
37
#endif
38
 
39
/**
40
 * @addtogroup TcpComm
41
 * @{
42
 */
43
 
44
//---------------------------------------------------------------------------
45
/**
46
*  The TcpComm class implements the serial communications device.
47
*/
48
 
49
class TcpComm : public CommDev
50
{
51
public:
52
 
53
    /**
54
     *  Describes the type of socket that we're going to create.
55
     */
56
 
57
    enum Mode
58
    {
59
        Client, ///< This is a client endpoint (default)
60
        Server  ///< This is a server endpoint
61
    };
62
 
63
    /**
64
     *  Default port to use if the default constructor is called.
65
     */
66
 
67
    enum { DefaultPort = 4444 };
68
 
69
    //------------------------------------------------------------------------
70
    // Default constructor
71
 
72
    TcpComm();
73
 
74
    //------------------------------------------------------------------------
75
    // Default constructor
76
 
77
    TcpComm( Mode mode, unsigned short portNum = DefaultPort );
78
 
79
    //------------------------------------------------------------------------
80
    // Destructor
81
 
82
    virtual ~TcpComm();
83
 
84
    //------------------------------------------------------------------------
85
    // Aborts a read, if one is pending.
86
 
87
    virtual void AbortRead();
88
 
89
    //------------------------------------------------------------------------
90
    // Close a previsouly opened device.
91
 
92
    virtual void Close();
93
 
94
    //------------------------------------------------------------------------
95
    // Returns connection information (for display purposes)
96
 
97
    virtual const char *ConnectionInfo() const;
98
 
99
    //------------------------------------------------------------------------
100
    // Open a communication device. This provides a generic interface,
101
    // but a derived class may choose to provide a more specialized version.
102
 
103
    virtual bool Open( const char *openStr );
104
 
105
    //------------------------------------------------------------------------
106
    // Read data from the communication device.
107
 
108
    virtual bool Read( void *buf, size_t bufSize, size_t *bytesRead );
109
 
110
    //------------------------------------------------------------------------
111
    // Write data to the communication device.
112
 
113
    virtual bool Write( const void *buf, size_t bufSize, size_t *bytesWritten );
114
 
115
private:
116
 
117
    //------------------------------------------------------------------------
118
    // The copy constructor and assignment operator are not need for this
119
    // class so we declare them private and don't provide an implementation.
120
 
121
    TcpComm( const TcpComm & copy );
122
    TcpComm &operator =( const TcpComm &rhs );
123
 
124
    //------------------------------------------------------------------------
125
 
126
    /**
127
     *  Value which can be storeed in m_socket to indicate that a socket
128
     *  handle is invalid.
129
     */
130
 
131
    enum { InvalidSocket = -1 };
132
 
133
    Mode                m_mode;         ///< Type of socket (Client or Server)
134
    unsigned short      m_defaultPort;  ///< Default Port number (if none specified)
135
    struct sockaddr_in  m_sockAddr;     ///< Socket address for the remote end
136
    struct sockaddr_in  m_remAddr;      ///< Address of remote end
137
    std::string         m_hostName;     ///< Host name/IP address that was passed in
138
    std::string         m_portStr;      ///< Port name/number
139
    int                 m_socket;       ///< Socket handle
140
        HANDLE                          m_abortEvent;   ///< Event that's signalled to abort a read
141
        HANDLE                          m_readEvent;    ///< Event that indicates when new data has arrived
142
 
143
    char                m_connectionInfo[ 100 ]; ///< Describes the hostname etc.
144
 
145
    static  bool        m_initialized;  ///< Has WSAStartup been called yet?
146
};
147
 
148
// ---- Inline Functions ----------------------------------------------------
149
 
150
/** @} */
151
 
152
#endif // TCPCOMM_H
153