]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/TcpSocketEngine_p.h
1a1a944a70b61f0ea5d1a1e774634c42a3a17363
[bamtools.git] / src / api / internal / io / TcpSocketEngine_p.h
1 // ***************************************************************************
2 // TcpSocketEngine_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 low-level implementation of TCP I/O
8 // ***************************************************************************
9
10 #ifndef TCPSOCKETENGINE_P_H
11 #define TCPSOCKETENGINE_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/internal/io/HostAddress_p.h"
24 #include "api/internal/io/TcpSocket_p.h"
25
26 namespace BamTools {
27 namespace Internal {
28
29 struct TcpSocketEngine {
30
31     // ctors & dtor
32     public:
33         TcpSocketEngine(void);
34         TcpSocketEngine(const TcpSocketEngine& other);
35         ~TcpSocketEngine(void);
36
37     // TcpSocketEngine interface
38     public:
39
40         // connection-related methods
41         void Close(void);
42         bool Connect(const HostAddress& address, const uint16_t port);
43         bool Initialize(HostAddress::NetworkProtocol protocol);
44         bool IsValid(void) const;
45
46         // IO-related methods
47         int64_t NumBytesAvailable(void) const;
48         int64_t Read(char* dest, size_t max);
49         int64_t Write(const char* data, size_t length);
50
51         bool WaitForRead(int msec, bool* timedOut);
52         bool WaitForWrite(int msec, bool* timedOut);
53
54         // query connection state
55 //        HostAddress GetLocalAddress(void) const;
56 //        uint16_t GetLocalPort(void) const;
57         HostAddress GetRemoteAddress(void) const;
58         uint16_t    GetRemotePort(void) const;
59
60         int GetSocketDescriptor(void) const;
61         TcpSocket::SocketError GetSocketError(void);
62         TcpSocket::SocketState GetSocketState(void);
63
64         std::string GetErrorString(void) const;
65
66     // platform-dependent internal methods
67     // provided in the corresponding TcpSocketEngine_<OS>_p.cpp
68     private:
69         void    nativeClose(void);
70         bool    nativeConnect(const HostAddress& address, const uint16_t port);
71         bool    nativeCreateSocket(HostAddress::NetworkProtocol protocol);
72         void    nativeDisconnect(void);
73         int64_t nativeNumBytesAvailable(void) const;
74         int64_t nativeRead(char* dest, size_t max);
75         int     nativeSelect(int msecs, bool isRead) const;
76         int64_t nativeWrite(const char* data, size_t length);
77
78     // data members
79     private:
80         int m_socketDescriptor;
81
82 //        HostAddress m_localAddress;
83         HostAddress m_remoteAddress;
84 //        uint16_t m_localPort;
85         uint16_t m_remotePort;
86
87         TcpSocket::SocketError m_socketError;
88         TcpSocket::SocketState m_socketState;
89         std::string m_errorString;
90 };
91
92 } // namespace Internal
93 } // namespace BamTools
94
95 #endif // TCPSOCKETENGINE_P_H