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