1 // ***************************************************************************
2 // TcpSocket_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 November 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"
31 class TcpSocketEngine;
37 enum SocketError { UnknownSocketError = -1
38 , ConnectionRefusedError = 0
39 , RemoteHostClosedError
45 , UnsupportedSocketOperationError
48 enum SocketState { UnconnectedState = 0
57 // TcpSocket interface
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;
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);
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;
88 TcpSocket::SocketError GetError(void) const;
89 TcpSocket::SocketState GetState(void) const;
90 std::string GetErrorString(void) const;
94 bool ConnectImpl(const HostInfo& hostInfo,
95 const std::string& port,
96 IBamIODevice::OpenMode mode);
97 bool InitializeSocketEngine(HostAddress::NetworkProtocol protocol);
98 bool ReadFromSocket(void);
99 void ResetSocketEngine(void);
103 IBamIODevice::OpenMode m_mode;
105 std::string m_hostName;
106 // uint16_t m_localPort;
107 uint16_t m_remotePort;
108 // HostAddress m_localAddress;
109 HostAddress m_remoteAddress;
111 TcpSocketEngine* m_engine;
112 int m_cachedSocketDescriptor;
114 RollingBuffer m_readBuffer;
116 TcpSocket::SocketError m_error;
117 TcpSocket::SocketState m_state;
118 std::string m_errorString;
121 } // namespace Internal
122 } // namespace BamTools
124 #endif // TCPSOCKET_P_H