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 // ***************************************************************************
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(void);
76 int64_t Write(const char* data, const unsigned int numBytes);
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;
86 TcpSocket::SocketError GetError(void) const;
87 TcpSocket::SocketState GetState(void) const;
88 std::string GetErrorString(void) const;
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);
101 IBamIODevice::OpenMode m_mode;
103 std::string m_hostName;
104 // uint16_t m_localPort;
105 uint16_t m_remotePort;
106 // HostAddress m_localAddress;
107 HostAddress m_remoteAddress;
109 TcpSocketEngine* m_engine;
110 int m_cachedSocketDescriptor;
112 RollingBuffer m_readBuffer;
114 TcpSocket::SocketError m_error;
115 TcpSocket::SocketState m_state;
116 std::string m_errorString;
119 } // namespace Internal
120 } // namespace BamTools
122 #endif // TCPSOCKET_P_H