Merge pull request #4775 from jmarshallnz/empty_episode_playcount
[vuplus_xbmc] / xbmc / network / TCPServer.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
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)
9  *  any later version.
10  *
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.
15  *
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/>.
19  *
20  */
21
22 #include <vector>
23 #include <sys/socket.h>
24
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"
31
32 namespace JSONRPC
33 {
34   class CTCPServer : public ITransportLayer, public JSONRPC::IJSONRPCAnnouncer, public CThread
35   {
36   public:
37     static bool StartServer(int port, bool nonlocal);
38     static void StopServer(bool bWait);
39     static bool IsRunning();
40
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();
44
45     virtual void Announce(ANNOUNCEMENT::AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data);
46   protected:
47     void Process();
48   private:
49     CTCPServer(int port, bool nonlocal);
50     bool Initialize();
51     bool InitializeBlue();
52     bool InitializeTCP();
53     void Deinitialize();
54
55     class CTCPClient : public IClient
56     {
57     public:
58       CTCPClient();
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() { };
64
65       virtual int  GetPermissionFlags();
66       virtual int  GetAnnouncementFlags();
67       virtual bool SetAnnouncementFlags(int flags);
68
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();
72
73       virtual bool IsNew() const { return m_new; }
74       virtual bool Closing() const { return false; }
75
76       SOCKET           m_socket;
77       sockaddr_storage m_cliaddr;
78       socklen_t        m_addrlen;
79       CCriticalSection m_critSection;
80
81     protected:
82       void Copy(const CTCPClient& client);
83     private:
84       bool m_new;
85       int m_announcementflags;
86       int m_beginBrackets, m_endBrackets;
87       char m_beginChar, m_endChar;
88       std::string m_buffer;
89     };
90
91     class CWebSocketClient : public CTCPClient
92     {
93     public:
94       CWebSocketClient(CWebSocket *websocket);
95       CWebSocketClient(const CWebSocketClient& client);
96       CWebSocketClient(CWebSocket *websocket, const CTCPClient& client);
97       CWebSocketClient& operator=(const CWebSocketClient& client);
98       ~CWebSocketClient();
99
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();
103
104       virtual bool IsNew() const { return m_websocket == NULL; }
105       virtual bool Closing() const { return m_websocket != NULL && m_websocket->GetState() == WebSocketStateClosed; }
106
107     private:
108       CWebSocket *m_websocket;
109     };
110
111     std::vector<CTCPClient*> m_connections;
112     std::vector<SOCKET> m_servers;
113     int m_port;
114     bool m_nonlocal;
115     void* m_sdpd;
116
117     static CTCPServer *ServerInstance;
118   };
119 }