1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include "interfaces/json-rpc/IClient.h"
#include "interfaces/json-rpc/IJSONRPCAnnouncer.h"
#include "interfaces/json-rpc/ITransportLayer.h"
#include "threads/CriticalSection.h"
#include "threads/Thread.h"
#include "websocket/WebSocket.h"
#include <vector>
#include <sys/socket.h>
#include "PlatformDefs.h"
class CVariant;
namespace JSONRPC
{
class CTCPServer : public ITransportLayer, public JSONRPC::IJSONRPCAnnouncer, public CThread
{
public:
static bool StartServer(int port, bool nonlocal);
static void StopServer(bool bWait);
static bool IsRunning();
bool PrepareDownload(const char *path, CVariant &details, std::string &protocol) override;
bool Download(const char *path, CVariant &result) override;
int GetCapabilities() override;
void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
const std::string& sender,
const std::string& message,
const CVariant& data) override;
protected:
void Process() override;
private:
CTCPServer(int port, bool nonlocal);
bool Initialize();
bool InitializeBlue();
bool InitializeTCP();
void Deinitialize();
class CTCPClient : public IClient
{
public:
CTCPClient();
//Copying a CCriticalSection is not allowed, so copy everything but that
//when adding a member variable, make sure to copy it in CTCPClient::Copy
CTCPClient(const CTCPClient& client);
CTCPClient& operator=(const CTCPClient& client);
~CTCPClient() override = default;
int GetPermissionFlags() override;
int GetAnnouncementFlags() override;
bool SetAnnouncementFlags(int flags) override;
virtual void Send(const char *data, unsigned int size);
virtual void PushBuffer(CTCPServer *host, const char *buffer, int length);
virtual void Disconnect();
virtual bool IsNew() const { return m_new; }
virtual bool Closing() const { return false; }
SOCKET m_socket;
sockaddr_storage m_cliaddr;
socklen_t m_addrlen;
CCriticalSection m_critSection;
protected:
void Copy(const CTCPClient& client);
private:
bool m_new;
int m_announcementflags;
int m_beginBrackets, m_endBrackets;
char m_beginChar, m_endChar;
std::string m_buffer;
};
class CWebSocketClient : public CTCPClient
{
public:
explicit CWebSocketClient(CWebSocket *websocket);
CWebSocketClient(const CWebSocketClient& client);
CWebSocketClient(CWebSocket *websocket, const CTCPClient& client);
CWebSocketClient& operator=(const CWebSocketClient& client);
~CWebSocketClient() override;
void Send(const char *data, unsigned int size) override;
void PushBuffer(CTCPServer *host, const char *buffer, int length) override;
void Disconnect() override;
bool IsNew() const override { return m_websocket == NULL; }
bool Closing() const override { return m_websocket != NULL && m_websocket->GetState() == WebSocketStateClosed; }
private:
CWebSocket *m_websocket;
std::string m_buffer;
};
std::vector<CTCPClient*> m_connections;
std::vector<SOCKET> m_servers;
int m_port;
bool m_nonlocal;
void* m_sdpd;
static CTCPServer *ServerInstance;
};
}
|