4c486fbff806fa22884e9fe9463488d32442cdc3
[vuplus_xbmc] / xbmc / network / AirPlayServer.h
1 #pragma once
2 /*
3  * Many concepts and protocol specification in this code are taken from
4  * the Boxee project. http://www.boxee.tv
5  *
6  *      Copyright (C) 2011-2013 Team XBMC
7  *      http://xbmc.org
8  *
9  *  This Program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2.1, or (at your option)
12  *  any later version.
13  *
14  *  This Program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with XBMC; see the file COPYING.  If not, see
21  *  <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "system.h"
26 #ifdef HAS_AIRPLAY
27
28 #include <map>
29 #include <sys/socket.h>
30 #include "threads/Thread.h"
31 #include "threads/CriticalSection.h"
32 #include "utils/HttpParser.h"
33 #include "utils/StdString.h"
34 #include "interfaces/IAnnouncer.h"
35
36 class DllLibPlist;
37
38 #define AIRPLAY_SERVER_VERSION_STR "101.28"
39
40 class CAirPlayServer : public CThread, public ANNOUNCEMENT::IAnnouncer
41 {
42 public:
43   // IAnnouncer IF
44   virtual void Announce(ANNOUNCEMENT::AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data);
45
46   //AirPlayServer impl.
47   static bool StartServer(int port, bool nonlocal);
48   static void StopServer(bool bWait);
49   static bool IsRunning();
50   static bool SetCredentials(bool usePassword, const CStdString& password);
51   static bool IsPlaying(){ return m_isPlaying > 0;}
52   static void backupVolume();
53   static void restoreVolume();
54   static int m_isPlaying;
55
56 protected:
57   void Process();
58
59 private:
60   CAirPlayServer(int port, bool nonlocal);
61   ~CAirPlayServer();
62   bool SetInternalCredentials(bool usePassword, const CStdString& password);
63   bool Initialize();
64   void Deinitialize();
65   void AnnounceToClients(int state);
66
67   class CTCPClient
68   {
69   public:
70     CTCPClient();
71     ~CTCPClient();
72     //Copying a CCriticalSection is not allowed, so copy everything but that
73     //when adding a member variable, make sure to copy it in CTCPClient::Copy
74     CTCPClient(const CTCPClient& client);
75     CTCPClient& operator=(const CTCPClient& client);
76     void PushBuffer(CAirPlayServer *host, const char *buffer,
77                     int length, CStdString &sessionId,
78                     std::map<CStdString, int> &reverseSockets);
79     void ComposeReverseEvent(CStdString& reverseHeader, CStdString& reverseBody, int state);
80
81     void Disconnect();
82
83     int m_socket;
84     struct sockaddr_storage m_cliaddr;
85     socklen_t m_addrlen;
86     CCriticalSection m_critSection;
87     int  m_sessionCounter;
88     CStdString m_sessionId;
89
90   private:
91     int ProcessRequest( CStdString& responseHeader,
92                         CStdString& response);
93
94     void ComposeAuthRequestAnswer(CStdString& responseHeader, CStdString& responseBody);
95     bool checkAuthorization(const CStdString& authStr, const CStdString& method, const CStdString& uri);
96     void Copy(const CTCPClient& client);
97
98     HttpParser* m_httpParser;
99     DllLibPlist *m_pLibPlist;//the lib
100     bool m_bAuthenticated;
101     int  m_lastEvent;
102     CStdString m_authNonce;
103   };
104
105   CCriticalSection m_connectionLock;
106   std::vector<CTCPClient> m_connections;
107   std::map<CStdString, int> m_reverseSockets;
108   int m_ServerSocket;
109   int m_port;
110   bool m_nonlocal;
111   bool m_usePassword;
112   CStdString m_password;
113   int m_origVolume;
114
115   static CAirPlayServer *ServerInstance;
116 };
117
118 #endif