X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Fapi%2Finternal%2Fio%2FTcpSocket_p.h;fp=src%2Fapi%2Finternal%2Fio%2FTcpSocket_p.h;h=4cd1f1a0614b0623656c2537068b88d2063c5a68;hb=aace28299671f066bf2135ef28652f24fa1d8d26;hp=0000000000000000000000000000000000000000;hpb=90432a3736392f98803a5e8787f3db0dc5bb44ad;p=bamtools.git diff --git a/src/api/internal/io/TcpSocket_p.h b/src/api/internal/io/TcpSocket_p.h new file mode 100644 index 0000000..4cd1f1a --- /dev/null +++ b/src/api/internal/io/TcpSocket_p.h @@ -0,0 +1,122 @@ +// *************************************************************************** +// TcpSocket_p.h (c) 2011 Derek Barnett +// Marth Lab, Department of Biology, Boston College +// --------------------------------------------------------------------------- +// Last modified: 25 October 2011 (DB) +// --------------------------------------------------------------------------- +// Provides TCP socket I/O +// *************************************************************************** + +#ifndef TCPSOCKET_P_H +#define TCPSOCKET_P_H + +// ------------- +// W A R N I N G +// ------------- +// +// This file is not part of the BamTools API. It exists purely as an +// implementation detail. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. + +#include "api/IBamIODevice.h" +#include "api/internal/io/HostInfo_p.h" +#include "api/internal/io/RollingBuffer_p.h" +#include + +namespace BamTools { +namespace Internal { + +class TcpSocketEngine; + +class TcpSocket { + + // enums + public: + enum SocketError { UnknownSocketError = -1 + , ConnectionRefusedError = 0 + , RemoteHostClosedError + , HostNotFoundError + , SocketAccessError + , SocketResourceError + , SocketTimeoutError + , NetworkError + , UnsupportedSocketOperationError + }; + + enum SocketState { UnconnectedState = 0 + , ConnectedState + }; + + // ctor & dtor + public: + TcpSocket(void); + ~TcpSocket(void); + + // TcpSocket interface + public: + + // connection methods + bool ConnectToHost(const std::string& hostName, + const uint16_t port, // Connect("host", 80) + IBamIODevice::OpenMode mode = IBamIODevice::ReadOnly); + bool ConnectToHost(const std::string& hostName, + const std::string& port, // Connect("host", "80") + IBamIODevice::OpenMode mode = IBamIODevice::ReadOnly); + void DisconnectFromHost(void); + bool IsConnected(void) const; + + // I/O methods + size_t BufferBytesAvailable(void) const; + bool CanReadLine(void) const; + void ClearBuffer(void); // force buffer to clear (not a 'flush', just a 'discard') + int64_t Read(char* data, const unsigned int numBytes); + std::string ReadLine(void); + int64_t Write(const char* data, const unsigned int numBytes); + + // connection values + std::string GetHostName(void) const; +// HostAddress GetLocalAddress(void) const; +// uint16_t GetLocalPort(void) const; + HostAddress GetRemoteAddress(void) const; + uint16_t GetRemotePort(void) const; + + // connection status + TcpSocket::SocketError GetError(void) const; + TcpSocket::SocketState GetState(void) const; + std::string GetErrorString(void) const; + + // internal methods + private: + bool ConnectImpl(const HostInfo& hostInfo, + const std::string& port, + IBamIODevice::OpenMode mode); + bool InitializeSocketEngine(HostAddress::NetworkProtocol protocol); + bool ReadFromSocket(void); + void ResetSocketEngine(void); + + // data members + private: + IBamIODevice::OpenMode m_mode; + + std::string m_hostName; +// uint16_t m_localPort; + uint16_t m_remotePort; +// HostAddress m_localAddress; + HostAddress m_remoteAddress; + + TcpSocketEngine* m_engine; + int m_cachedSocketDescriptor; + + RollingBuffer m_readBuffer; + + TcpSocket::SocketError m_error; + TcpSocket::SocketState m_state; + std::string m_errorString; +}; + +} // namespace Internal +} // namespace BamTools + +#endif // TCPSOCKET_P_H