3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
23 #include <sys/socket.h>
25 #include "interfaces/json-rpc/IClient.h"
26 #include "interfaces/json-rpc/IJSONRPCAnnouncer.h"
27 #include "interfaces/json-rpc/ITransportLayer.h"
28 #include "threads/CriticalSection.h"
29 #include "threads/Thread.h"
30 #include "websocket/WebSocket.h"
34 class CTCPServer : public ITransportLayer, public JSONRPC::IJSONRPCAnnouncer, public CThread
37 static bool StartServer(int port, bool nonlocal);
38 static void StopServer(bool bWait);
39 static bool IsRunning();
41 virtual bool PrepareDownload(const char *path, CVariant &details, std::string &protocol);
42 virtual bool Download(const char *path, CVariant &result);
43 virtual int GetCapabilities();
45 virtual void Announce(ANNOUNCEMENT::AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data);
49 CTCPServer(int port, bool nonlocal);
51 bool InitializeBlue();
55 class CTCPClient : public IClient
59 //Copying a CCriticalSection is not allowed, so copy everything but that
60 //when adding a member variable, make sure to copy it in CTCPClient::Copy
61 CTCPClient(const CTCPClient& client);
62 CTCPClient& operator=(const CTCPClient& client);
63 virtual ~CTCPClient() { };
65 virtual int GetPermissionFlags();
66 virtual int GetAnnouncementFlags();
67 virtual bool SetAnnouncementFlags(int flags);
69 virtual void Send(const char *data, unsigned int size);
70 virtual void PushBuffer(CTCPServer *host, const char *buffer, int length);
71 virtual void Disconnect();
73 virtual bool IsNew() const { return m_new; }
74 virtual bool Closing() const { return false; }
77 sockaddr_storage m_cliaddr;
79 CCriticalSection m_critSection;
82 void Copy(const CTCPClient& client);
85 int m_announcementflags;
86 int m_beginBrackets, m_endBrackets;
87 char m_beginChar, m_endChar;
91 class CWebSocketClient : public CTCPClient
94 CWebSocketClient(CWebSocket *websocket);
95 CWebSocketClient(const CWebSocketClient& client);
96 CWebSocketClient(CWebSocket *websocket, const CTCPClient& client);
97 CWebSocketClient& operator=(const CWebSocketClient& client);
100 virtual void Send(const char *data, unsigned int size);
101 virtual void PushBuffer(CTCPServer *host, const char *buffer, int length);
102 virtual void Disconnect();
104 virtual bool IsNew() const { return m_websocket == NULL; }
105 virtual bool Closing() const { return m_websocket != NULL && m_websocket->GetState() == WebSocketStateClosed; }
108 CWebSocket *m_websocket;
111 std::vector<CTCPClient*> m_connections;
112 std::vector<SOCKET> m_servers;
117 static CTCPServer *ServerInstance;