]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/TcpSocket_p.h
4cd1f1a0614b0623656c2537068b88d2063c5a68
[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: 25 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides TCP socket I/O
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(void);
76         int64_t Write(const char* data, const unsigned int numBytes);
77
78         // connection values
79         std::string GetHostName(void) const;
80 //        HostAddress GetLocalAddress(void) const;
81 //        uint16_t    GetLocalPort(void) const;
82         HostAddress GetRemoteAddress(void) const;
83         uint16_t    GetRemotePort(void) const;
84
85         // connection status
86         TcpSocket::SocketError GetError(void) const;
87         TcpSocket::SocketState GetState(void) const;
88         std::string GetErrorString(void) const;
89
90     // internal methods
91     private:
92         bool ConnectImpl(const HostInfo& hostInfo,
93                          const std::string& port,
94                          IBamIODevice::OpenMode mode);
95         bool InitializeSocketEngine(HostAddress::NetworkProtocol protocol);
96         bool ReadFromSocket(void);
97         void ResetSocketEngine(void);
98
99     // data members
100     private:
101         IBamIODevice::OpenMode m_mode;
102
103         std::string m_hostName;
104 //        uint16_t    m_localPort;
105         uint16_t    m_remotePort;
106 //        HostAddress m_localAddress;
107         HostAddress m_remoteAddress;
108
109         TcpSocketEngine* m_engine;
110         int m_cachedSocketDescriptor;
111
112         RollingBuffer m_readBuffer;
113
114         TcpSocket::SocketError m_error;
115         TcpSocket::SocketState m_state;
116         std::string m_errorString;
117 };
118
119 } // namespace Internal
120 } // namespace BamTools
121
122 #endif // TCPSOCKET_P_H