[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / network / TCPServer.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://www.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
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();
43
44     virtual void Announce(ANNOUNCEMENT::AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data);
45   protected:
46     void Process();
47   private:
48     CTCPServer(int port, bool nonlocal);
49     bool Initialize();
50     bool InitializeBlue();
51     bool InitializeTCP();
52     void Deinitialize();
53
54     class CTCPClient : public IClient
55     {
56     public:
57       CTCPClient();
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() { };
63
64       virtual int  GetPermissionFlags();
65       virtual int  GetAnnouncementFlags();
66       virtual bool SetAnnouncementFlags(int flags);
67
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();
71
72       virtual bool IsNew() const { return m_new; }
73       virtual bool Closing() const { return false; }
74
75       SOCKET           m_socket;
76       sockaddr_storage m_cliaddr;
77       socklen_t        m_addrlen;
78       CCriticalSection m_critSection;
79
80     protected:
81       void Copy(const CTCPClient& client);
82     private:
83       bool m_new;
84       int m_announcementflags;
85       int m_beginBrackets, m_endBrackets;
86       char m_beginChar, m_endChar;
87       std::string m_buffer;
88     };
89
90     class CWebSocketClient : public CTCPClient
91     {
92     public:
93       CWebSocketClient(CWebSocket *websocket);
94       CWebSocketClient(const CWebSocketClient& client);
95       CWebSocketClient(CWebSocket *websocket, const CTCPClient& client);
96       CWebSocketClient& operator=(const CWebSocketClient& client);
97       ~CWebSocketClient();
98
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();
102
103       virtual bool IsNew() const { return m_websocket == NULL; }
104       virtual bool Closing() const { return m_websocket != NULL && m_websocket->GetState() == WebSocketStateClosed; }
105
106     private:
107       CWebSocket *m_websocket;
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 }