2 * Many concepts and protocol specification in this code are taken from
3 * the Boxee project. http://www.boxee.tv
5 * Copyright (C) 2011-2018 Team Kodi
6 * This file is part of Kodi - https://kodi.tv
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 * See LICENSES/README.md for more information.
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"
23 #include <sys/socket.h>
27 #define AIRPLAY_SERVER_VERSION_STR "101.28"
29 class CAirPlayServer
: public CThread
, public ANNOUNCEMENT::IAnnouncer
33 void Announce(ANNOUNCEMENT::AnnouncementFlag flag
,
34 const std::string
& sender
,
35 const std::string
& message
,
36 const CVariant
& data
) override
;
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
;
49 void Process() override
;
52 CAirPlayServer(int port
, bool nonlocal
);
53 ~CAirPlayServer() override
;
54 bool SetInternalCredentials(bool usePassword
, const std::string
& password
);
57 void AnnounceToClients(int state
);
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
);
76 struct sockaddr_storage m_cliaddr
;
78 CCriticalSection m_critSection
;
80 std::string m_sessionId
;
83 int ProcessRequest( std::string
& responseHeader
,
84 std::string
& response
);
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
);
90 HttpParser
* m_httpParser
;
91 bool m_bAuthenticated
;
93 std::string m_authNonce
;
96 CCriticalSection m_connectionLock
;
97 std::vector
<CTCPClient
> m_connections
;
98 std::map
<std::string
, int> m_reverseSockets
;
99 std::vector
<SOCKET
> m_ServerSockets
;
103 std::string m_password
;
106 static CCriticalSection ServerInstanceLock
;
107 static CAirPlayServer
*ServerInstance
;