summaryrefslogtreecommitdiffstats
path: root/xbmc/network/AirPlayServer.h
blob: 986616b22176176228313cc6b7f203597a229b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Many concepts and protocol specification in this code are taken from
 * the Boxee project. http://www.boxee.tv
 *
 *  Copyright (C) 2011-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "interfaces/IAnnouncer.h"
#include "network/Network.h"
#include "threads/CriticalSection.h"
#include "threads/Thread.h"
#include "utils/HttpParser.h"

#include <map>
#include <vector>

#include <sys/socket.h>

class CVariant;

#define AIRPLAY_SERVER_VERSION_STR "101.28"

class CAirPlayServer : public CThread, public ANNOUNCEMENT::IAnnouncer
{
public:
  // IAnnouncer IF
  void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
                const std::string& sender,
                const std::string& message,
                const CVariant& data) override;

  //AirPlayServer impl.
  static bool StartServer(int port, bool nonlocal);
  static void StopServer(bool bWait);
  static bool IsRunning();
  static bool SetCredentials(bool usePassword, const std::string& password);
  static bool IsPlaying(){ return m_isPlaying > 0;}
  static void backupVolume();
  static void restoreVolume();
  static int m_isPlaying;

protected:
  void Process() override;

private:
  CAirPlayServer(int port, bool nonlocal);
  ~CAirPlayServer() override;
  bool SetInternalCredentials(bool usePassword, const std::string& password);
  bool Initialize();
  void Deinitialize();
  void AnnounceToClients(int state);

  class CTCPClient
  {
  public:
    CTCPClient();
    ~CTCPClient();
    //Copying a CCriticalSection is not allowed, so copy everything but that
    //when adding a member variable, make sure to copy it in CTCPClient::Copy
    CTCPClient(const CTCPClient& client);
    CTCPClient& operator=(const CTCPClient& client);
    void PushBuffer(CAirPlayServer *host, const char *buffer,
                    int length, std::string &sessionId,
                    std::map<std::string, int> &reverseSockets);
    void ComposeReverseEvent(std::string& reverseHeader, std::string& reverseBody, int state);

    void Disconnect();

    int m_socket;
    struct sockaddr_storage m_cliaddr;
    socklen_t m_addrlen;
    CCriticalSection m_critSection;
    int  m_sessionCounter;
    std::string m_sessionId;

  private:
    int ProcessRequest( std::string& responseHeader,
                        std::string& response);

    void ComposeAuthRequestAnswer(std::string& responseHeader, std::string& responseBody);
    bool checkAuthorization(const std::string& authStr, const std::string& method, const std::string& uri);
    void Copy(const CTCPClient& client);

    HttpParser* m_httpParser;
    bool m_bAuthenticated;
    int  m_lastEvent;
    std::string m_authNonce;
  };

  CCriticalSection m_connectionLock;
  std::vector<CTCPClient> m_connections;
  std::map<std::string, int> m_reverseSockets;
  std::vector<SOCKET> m_ServerSockets;
  int m_port;
  bool m_nonlocal;
  bool m_usePassword;
  std::string m_password;
  int m_origVolume;

  static CCriticalSection ServerInstanceLock;
  static CAirPlayServer *ServerInstance;
};