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 // ***************************************************************************
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.
23 #include "api/IBamIODevice.h"
24 #include "api/internal/io/HostInfo_p.h"
25 #include "api/internal/io/RollingBuffer_p.h"
32 class TcpSocketEngine;
38 enum SocketError { NoError = -2
39 , UnknownSocketError = -1
40 , ConnectionRefusedError = 0
41 , RemoteHostClosedError
47 , UnsupportedSocketOperationError
50 enum SocketState { UnconnectedState = 0
59 // TcpSocket interface
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;
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);
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;
90 TcpSocket::SocketError GetError(void) const;
91 TcpSocket::SocketState GetState(void) const;
92 std::string GetErrorString(void) const;
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);
105 IBamIODevice::OpenMode m_mode;
107 std::string m_hostName;
108 // uint16_t m_localPort;
109 uint16_t m_remotePort;
110 // HostAddress m_localAddress;
111 HostAddress m_remoteAddress;
113 TcpSocketEngine* m_engine;
114 int m_cachedSocketDescriptor;
116 RollingBuffer m_readBuffer;
118 TcpSocket::SocketError m_error;
119 TcpSocket::SocketState m_state;
120 std::string m_errorString;
122 friend class BamHttp;
125 } // namespace Internal
126 } // namespace BamTools
128 #endif // TCPSOCKET_P_H