]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/TcpSocket_p.h
a25a11e0ff845f9ab847120c50784ef721a01e48
[bamtools.git] / src / api / internal / io / TcpSocket_p.h
1 // ***************************************************************************
2 // TcpSocket_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 7 December 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides basic TCP I/O interface
8 // ***************************************************************************
9
10 #ifndef TCPSOCKET_P_H
11 #define TCPSOCKET_P_H
12
13 //  -------------
14 //  W A R N I N G
15 //  -------------
16 //
17 // This file is not part of the BamTools API.  It exists purely as an
18 // implementation detail. This header file may change from version to version
19 // without notice, or even be removed.
20 //
21 // We mean it.
22
23 #include "api/IBamIODevice.h"
24 #include "api/internal/io/HostInfo_p.h"
25 #include "api/internal/io/RollingBuffer_p.h"
26 #include <string>
27
28 namespace BamTools {
29 namespace Internal {
30
31 class TcpSocketEngine;
32
33 class TcpSocket {
34
35     // enums
36     public:
37         enum SocketError { UnknownSocketError     = -1
38                          , ConnectionRefusedError = 0
39                          , RemoteHostClosedError
40                          , HostNotFoundError
41                          , SocketAccessError
42                          , SocketResourceError
43                          , SocketTimeoutError
44                          , NetworkError
45                          , UnsupportedSocketOperationError
46                          };
47
48         enum SocketState { UnconnectedState = 0
49                          , ConnectedState
50                          };
51
52     // ctor & dtor
53     public:
54         TcpSocket(void);
55         ~TcpSocket(void);
56
57     // TcpSocket interface
58     public:
59
60         // connection methods
61         bool ConnectToHost(const std::string& hostName,
62                            const uint16_t port,        // Connect("host", 80)
63                            IBamIODevice::OpenMode mode = IBamIODevice::ReadOnly);
64         bool ConnectToHost(const std::string& hostName,
65                            const std::string& port,    // Connect("host", "80")
66                            IBamIODevice::OpenMode mode = IBamIODevice::ReadOnly);
67         void DisconnectFromHost(void);
68         bool IsConnected(void) const;
69
70         // I/O methods
71         size_t BufferBytesAvailable(void) const;
72         bool CanReadLine(void) const;
73         void ClearBuffer(void); // force buffer to clear (not a 'flush', just a 'discard')
74         int64_t Read(char* data, const unsigned int numBytes);
75         std::string ReadLine(int64_t max = 0);
76         int64_t ReadLine(char* dest, size_t max);
77         bool WaitForReadLine(void);
78         int64_t Write(const char* data, const unsigned int numBytes);
79
80         // connection values
81         std::string GetHostName(void) const;
82 //        HostAddress GetLocalAddress(void) const;
83 //        uint16_t    GetLocalPort(void) const;
84         HostAddress GetRemoteAddress(void) const;
85         uint16_t    GetRemotePort(void) const;
86
87         // connection status
88         TcpSocket::SocketError GetError(void) const;
89         TcpSocket::SocketState GetState(void) const;
90         std::string GetErrorString(void) const;
91
92     // internal methods
93     private:
94         bool ConnectImpl(const HostInfo& hostInfo,
95                          const std::string& port,
96                          IBamIODevice::OpenMode mode);
97         bool InitializeSocketEngine(HostAddress::NetworkProtocol protocol);
98         int64_t ReadFromSocket(void);
99         void ResetSocketEngine(void);
100
101     // data members
102     private:
103         IBamIODevice::OpenMode m_mode;
104
105         std::string m_hostName;
106 //        uint16_t    m_localPort;
107         uint16_t    m_remotePort;
108 //        HostAddress m_localAddress;
109         HostAddress m_remoteAddress;
110
111         TcpSocketEngine* m_engine;
112         int m_cachedSocketDescriptor;
113
114         RollingBuffer m_readBuffer;
115
116         TcpSocket::SocketError m_error;
117         TcpSocket::SocketState m_state;
118         std::string m_errorString;
119 };
120
121 } // namespace Internal
122 } // namespace BamTools
123
124 #endif // TCPSOCKET_P_H