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);
40 virtual bool PrepareDownload(const char *path, CVariant &details, std::string &protocol);
41 virtual bool Download(const char *path, CVariant &result);
42 virtual int GetCapabilities();
44 virtual void Announce(ANNOUNCEMENT::AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data);
48 CTCPServer(int port, bool nonlocal);
50 bool InitializeBlue();
54 class CTCPClient : public IClient
58 //Copying a CCriticalSection is not allowed, so copy everything but that
59 //when adding a member variable, make sure to copy it in CTCPClient::Copy
60 CTCPClient(const CTCPClient& client);
61 CTCPClient& operator=(const CTCPClient& client);
62 virtual ~CTCPClient() { };
64 virtual int GetPermissionFlags();
65 virtual int GetAnnouncementFlags();
66 virtual bool SetAnnouncementFlags(int flags);
68 virtual void Send(const char *data, unsigned int size);
69 virtual void PushBuffer(CTCPServer *host, const char *buffer, int length);
70 virtual void Disconnect();
72 virtual bool IsNew() const { return m_new; }
73 virtual bool Closing() const { return false; }
76 sockaddr_storage m_cliaddr;
78 CCriticalSection m_critSection;
81 void Copy(const CTCPClient& client);
84 int m_announcementflags;
85 int m_beginBrackets, m_endBrackets;
86 char m_beginChar, m_endChar;
90 class CWebSocketClient : public CTCPClient
93 CWebSocketClient(CWebSocket *websocket);
94 CWebSocketClient(const CWebSocketClient& client);
95 CWebSocketClient(CWebSocket *websocket, const CTCPClient& client);
96 CWebSocketClient& operator=(const CWebSocketClient& client);
99 virtual void Send(const char *data, unsigned int size);
100 virtual void PushBuffer(CTCPServer *host, const char *buffer, int length);
101 virtual void Disconnect();
103 virtual bool IsNew() const { return m_websocket == NULL; }
104 virtual bool Closing() const { return m_websocket != NULL && m_websocket->GetState() == WebSocketStateClosed; }
107 CWebSocket *m_websocket;
110 std::vector<CTCPClient*> m_connections;
111 std::vector<SOCKET> m_servers;
116 static CTCPServer *ServerInstance;