Example #1
0
        /// <summary>
        /// Called when a media server connects. Adds the connection to the appropriate dictionaries
        /// </summary>
        /// <param name="connection">The connection that connected</param>
        /// <param name="type">The type of connection. Should be TCP</param>
        /// <returns>A Task representing the operation</returns>
        private static async Task WebMediaConnected(Connection connection, Network.Enums.ConnectionType type)
        {
            //Check that it's TCP
            if (type != Network.Enums.ConnectionType.TCP)
            {
                return;
            }

            ConLog.Log("WebMedia Link", "Media server " + connection.IPRemoteEndPoint.ToString() + " connected", LogType.Info);

            //Check that SignalR is connected - if not, disconnect them
            if (SignalRConnectionState != HubConnectionState.Connected)
            {
                ConLog.Log("WebMedia Link", "Disconnecting media server " + connection.IPRemoteEndPoint.ToString() + " because the server is not done setting up", LogType.Info);
                connection.Close(Network.Enums.CloseReason.ClientClosed);
                return;
            }

            //Create a new media server with the given name
            MediaServer newServer = new MediaServer((TcpConnection)connection);

            //Initiate
            await newServer.Init();

            //Add to dictionaries
            UuidToMediaServer.Add(newServer.UUID, newServer);
            ConnectionToUuid.Add(newServer.Connection, newServer.UUID);

            //Update SignalR
            await UpdateAllSignalRClients();

            ConLog.Log("WebMedia Link", "Added media server with name " + newServer.Name + " and address " + newServer.Connection.IPRemoteEndPoint.ToString(), LogType.Ok);
        }
Example #2
0
        /// <summary>
        /// Called when a media server disconnects. Removes the connection from the appropriate dictionaries
        /// </summary>
        /// <param name="connection">The connection that disconnected</param>
        /// <param name="type">The type of connection. Should be TCP</param>
        /// <param name="reason">The reason for disconnecting</param>
        /// <returns>A Task representing the operation</returns>
        private static async Task WebMediaDisconnected(Connection connection, Network.Enums.ConnectionType type, Network.Enums.CloseReason reason)
        {
            //Check that it's TCP
            if (type != Network.Enums.ConnectionType.TCP)
            {
                return;
            }

            //Check that it exists
            if (ConnectionToUuid.ContainsKey((TcpConnection)connection))
            {
                //Get the UUID
                MediaServer deadServer = UuidToMediaServer[ConnectionToUuid[(TcpConnection)connection]];

                ConLog.Log("WebMedia Link", "Media server with name " + deadServer.Name + " and address " + connection.IPRemoteEndPoint.ToString() + " disconnected", LogType.Info);

                //Remove the packet handlers
                connection.UnRegisterRawDataHandler("AvailableFilesUpdated");

                //Close it
                await deadServer.Close(false);

                //Remove from dictionaries
                UuidToMediaServer.Remove(deadServer.UUID);
                ConnectionToUuid.Remove((TcpConnection)connection);

                //Update SignalR
                await UpdateAllSignalRClients();

                ConLog.Log("WebMedia Link", "Removed media server with name " + deadServer.Name, LogType.Ok);
            }
            else
            {
                ConLog.Log("WebMedia Link", "Media server with address " + connection.IPRemoteEndPoint.ToString() + " disconnected", LogType.Info);
            }
        }