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