Brian Silverman | f7bd1c2 | 2015-12-24 16:07:11 -0800 | [diff] [blame^] | 1 | /* |
| 2 | TCPStream.h |
| 3 | |
| 4 | TCPStream class interface. TCPStream provides methods to trasnfer |
| 5 | data between peers over a TCP/IP connection. |
| 6 | |
| 7 | ------------------------------------------ |
| 8 | |
| 9 | Copyright © 2013 [Vic Hargrave - http://vichargrave.com] |
| 10 | |
| 11 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | you may not use this file except in compliance with the License. |
| 13 | You may obtain a copy of the License at |
| 14 | |
| 15 | http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | |
| 17 | Unless required by applicable law or agreed to in writing, software |
| 18 | distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | See the License for the specific language governing permissions and |
| 21 | limitations under the License. |
| 22 | */ |
| 23 | |
| 24 | #ifndef TCPSOCKETS_TCPSTREAM_H_ |
| 25 | #define TCPSOCKETS_TCPSTREAM_H_ |
| 26 | |
| 27 | #include <cstddef> |
| 28 | #include <string> |
| 29 | |
| 30 | #ifdef _WIN32 |
| 31 | #include <winsock2.h> |
| 32 | #else |
| 33 | #include <sys/socket.h> |
| 34 | #endif |
| 35 | |
| 36 | #include "NetworkStream.h" |
| 37 | |
| 38 | class TCPStream : public NetworkStream { |
| 39 | int m_sd; |
| 40 | std::string m_peerIP; |
| 41 | int m_peerPort; |
| 42 | |
| 43 | public: |
| 44 | friend class TCPAcceptor; |
| 45 | friend class TCPConnector; |
| 46 | |
| 47 | ~TCPStream(); |
| 48 | |
| 49 | std::size_t send(const char* buffer, std::size_t len, Error* err) override; |
| 50 | std::size_t receive(char* buffer, std::size_t len, Error* err, |
| 51 | int timeout = 0) override; |
| 52 | void close() override; |
| 53 | |
| 54 | llvm::StringRef getPeerIP() const override; |
| 55 | int getPeerPort() const override; |
| 56 | void setNoDelay() override; |
| 57 | |
| 58 | TCPStream(const TCPStream& stream) = delete; |
| 59 | TCPStream& operator=(const TCPStream&) = delete; |
| 60 | private: |
| 61 | bool WaitForReadEvent(int timeout); |
| 62 | |
| 63 | TCPStream(int sd, struct sockaddr_in* address); |
| 64 | TCPStream() = delete; |
| 65 | }; |
| 66 | |
| 67 | #endif |