| 1 |
/* |
| 2 |
* Copyright (C) 2005-2021 Team Kodi (https://kodi.tv) |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* See LICENSE.md for more information. |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include "kissnet/kissnet.hpp" |
| 11 |
|
| 12 |
#include <memory> |
| 13 |
#include <mutex> |
| 14 |
|
| 15 |
namespace vdrvnsi |
| 16 |
{ |
| 17 |
|
| 18 |
enum class SocketError |
| 19 |
{ |
| 20 |
None, |
| 21 |
Errored, |
| 22 |
ReadFailure, |
| 23 |
TimeOut |
| 24 |
}; |
| 25 |
|
| 26 |
class TCPSocket |
| 27 |
{ |
| 28 |
public: |
| 29 |
TCPSocket() = delete; |
| 30 |
TCPSocket(const std::string& host, uint16_t port); |
| 31 |
|
| 32 |
virtual ~TCPSocket(); |
| 33 |
|
| 34 |
bool Open(uint64_t iTimeoutMs = 0); |
| 35 |
|
| 36 |
bool IsOpen() const { return m_socket != nullptr; } |
| 37 |
|
| 38 |
SocketError LastError() const { return m_lastSocketError; } |
| 39 |
|
| 40 |
void Shutdown(); |
| 41 |
|
| 42 |
void Close(); |
| 43 |
|
| 44 |
int64_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0); |
| 45 |
|
| 46 |
int64_t Write(void* data, size_t len); |
| 47 |
|
| 48 |
private: |
| 49 |
SocketError m_lastSocketError = SocketError::None; |
| 50 |
std::shared_ptr<kissnet::tcp_socket> GetSocket(bool bCreate = false); |
| 51 |
void ResetSocket(); |
| 52 |
|
| 53 |
const kissnet::endpoint m_endpoint; |
| 54 |
std::shared_ptr<kissnet::tcp_socket> m_socket; |
| 55 |
std::recursive_mutex m_mutex; |
| 56 |
}; |
| 57 |
|
| 58 |
} // namespace vdrvnsi |