xbmc
AirPlayServer.h
1 /*
2  * Many concepts and protocol specification in this code are taken from
3  * the Boxee project. http://www.boxee.tv
4  *
5  * Copyright (C) 2011-2018 Team Kodi
6  * This file is part of Kodi - https://kodi.tv
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  * See LICENSES/README.md for more information.
10  */
11 
12 #pragma once
13 
14 #include "interfaces/IAnnouncer.h"
15 #include "network/Network.h"
16 #include "threads/CriticalSection.h"
17 #include "threads/Thread.h"
18 #include "utils/HttpParser.h"
19 
20 #include <map>
21 #include <vector>
22 
23 #include <sys/socket.h>
24 
25 class CVariant;
26 
27 #define AIRPLAY_SERVER_VERSION_STR "101.28"
28 
30 {
31 public:
32  // IAnnouncer IF
33  void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
34  const std::string& sender,
35  const std::string& message,
36  const CVariant& data) override;
37 
38  //AirPlayServer impl.
39  static bool StartServer(int port, bool nonlocal);
40  static void StopServer(bool bWait);
41  static bool IsRunning();
42  static bool SetCredentials(bool usePassword, const std::string& password);
43  static bool IsPlaying(){ return m_isPlaying > 0;}
44  static void backupVolume();
45  static void restoreVolume();
46  static int m_isPlaying;
47 
48 protected:
49  void Process() override;
50 
51 private:
52  CAirPlayServer(int port, bool nonlocal);
53  ~CAirPlayServer() override;
54  bool SetInternalCredentials(bool usePassword, const std::string& password);
55  bool Initialize();
56  void Deinitialize();
57  void AnnounceToClients(int state);
58 
59  class CTCPClient
60  {
61  public:
62  CTCPClient();
63  ~CTCPClient();
64  //Copying a CCriticalSection is not allowed, so copy everything but that
65  //when adding a member variable, make sure to copy it in CTCPClient::Copy
66  CTCPClient(const CTCPClient& client);
67  CTCPClient& operator=(const CTCPClient& client);
68  void PushBuffer(CAirPlayServer *host, const char *buffer,
69  int length, std::string &sessionId,
70  std::map<std::string, int> &reverseSockets);
71  void ComposeReverseEvent(std::string& reverseHeader, std::string& reverseBody, int state);
72 
73  void Disconnect();
74 
75  int m_socket;
76  struct sockaddr_storage m_cliaddr;
77  socklen_t m_addrlen;
78  CCriticalSection m_critSection;
79  int m_sessionCounter;
80  std::string m_sessionId;
81 
82  private:
83  int ProcessRequest( std::string& responseHeader,
84  std::string& response);
85 
86  void ComposeAuthRequestAnswer(std::string& responseHeader, std::string& responseBody);
87  bool checkAuthorization(const std::string& authStr, const std::string& method, const std::string& uri);
88  void Copy(const CTCPClient& client);
89 
90  HttpParser* m_httpParser;
91  bool m_bAuthenticated;
92  int m_lastEvent;
93  std::string m_authNonce;
94  };
95 
96  CCriticalSection m_connectionLock;
97  std::vector<CTCPClient> m_connections;
98  std::map<std::string, int> m_reverseSockets;
99  std::vector<SOCKET> m_ServerSockets;
100  int m_port;
101  bool m_nonlocal;
102  bool m_usePassword;
103  std::string m_password;
104  int m_origVolume;
105 
106  static CCriticalSection ServerInstanceLock;
107  static CAirPlayServer *ServerInstance;
108 };
Definition: AirPlayServer.h:29
Definition: Thread.h:44
Definition: IAnnouncer.h:70
Definition: Variant.h:31
Definition: HttpParser.h:42