]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/TcpSocket_p.h
Stablized HTTP access on all platforms. (issue #54, issue #11)
[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 BamHttp;
32 class TcpSocketEngine;
33
34 class TcpSocket {
35
36     // enums
37     public:
38         enum SocketError { NoError                = -2
39                          , UnknownSocketError     = -1
40                          , ConnectionRefusedError = 0
41                          , RemoteHostClosedError
42                          , HostNotFoundError
43                          , SocketAccessError
44                          , SocketResourceError
45                          , SocketTimeoutError
46                          , NetworkError
47                          , UnsupportedSocketOperationError
48                          };
49
50         enum SocketState { UnconnectedState = 0
51                          , ConnectedState
52                          };
53
54     // ctor & dtor
55     public:
56         TcpSocket(void);
57         ~TcpSocket(void);
58
59     // TcpSocket interface
60     public:
61
62         // connection methods
63         bool ConnectToHost(const std::string& hostName,
64                            const uint16_t port,        // Connect("host", 80)
65                            IBamIODevice::OpenMode mode = IBamIODevice::ReadOnly);
66         bool ConnectToHost(const std::string& hostName,
67                            const std::string& port,    // Connect("host", "80")
68                            IBamIODevice::OpenMode mode = IBamIODevice::ReadOnly);
69         void DisconnectFromHost(void);
70         bool IsConnected(void) const;
71
72         // I/O methods
73         size_t BufferBytesAvailable(void) const;
74         bool CanReadLine(void) const;
75         void ClearBuffer(void); // force buffer to clear (not a 'flush', just a 'discard')
76         int64_t Read(char* data, const unsigned int numBytes);
77         std::string ReadLine(int64_t max = 0);
78         int64_t ReadLine(char* dest, size_t max);
79         bool WaitForReadLine(void);
80         int64_t Write(const char* data, const unsigned int numBytes);
81
82         // connection values
83         std::string GetHostName(void) const;
84 //        HostAddress GetLocalAddress(void) const;
85 //        uint16_t    GetLocalPort(void) const;
86         HostAddress GetRemoteAddress(void) const;
87         uint16_t    GetRemotePort(void) const;
88
89         // connection status
90         TcpSocket::SocketError GetError(void) const;
91         TcpSocket::SocketState GetState(void) const;
92         std::string GetErrorString(void) const;
93
94     // internal methods
95     private:
96         bool ConnectImpl(const HostInfo& hostInfo,
97                          const std::string& port,
98                          IBamIODevice::OpenMode mode);
99         bool InitializeSocketEngine(HostAddress::NetworkProtocol protocol);
100         int64_t ReadFromSocket(void);
101         void ResetSocketEngine(void);
102
103     // data members
104     private:
105         IBamIODevice::OpenMode m_mode;
106
107         std::string m_hostName;
108 //        uint16_t    m_localPort;
109         uint16_t    m_remotePort;
110 //        HostAddress m_localAddress;
111         HostAddress m_remoteAddress;
112
113         TcpSocketEngine* m_engine;
114         int m_cachedSocketDescriptor;
115
116         RollingBuffer m_readBuffer;
117
118         TcpSocket::SocketError m_error;
119         TcpSocket::SocketState m_state;
120         std::string m_errorString;
121
122         friend class BamHttp;
123 };
124
125 } // namespace Internal
126 } // namespace BamTools
127
128 #endif // TCPSOCKET_P_H