public async Task UpdateUserState(int mediaServerUuid, int currentMediaUuid, bool isPlaying, double lastSeekPosition, UserBufferState bufferState) { //Check that the media server exists if (!Server.UuidToMediaServer.ContainsKey(mediaServerUuid)) { return; } MediaServer mediaServer = Server.UuidToMediaServer[mediaServerUuid]; //Check that the user exists if (!mediaServer.SignalRConnectionToUser.ContainsKey(Context.ConnectionId)) { return; } int userUuid = mediaServer.SignalRConnectionToUser[Context.ConnectionId]; User user = mediaServer.Users[userUuid]; //If nothing changed, drop it if (user.CurrentMediaUuid == currentMediaUuid && user.IsPlaying == isPlaying && user.LastSeekPosition == lastSeekPosition && user.BufferState == bufferState) { return; } await Clients.Client(mediaServer.ServerSignalRId).SendAsync("ServerUpdateUserState", userUuid, currentMediaUuid, isPlaying, lastSeekPosition, bufferState); }
public override async Task OnDisconnectedAsync(Exception exception) { MediaServer server = null; //Find the media server that this user was in foreach (MediaServer thisMediaServer in Server.UuidToMediaServer.Values) { //Check all users foreach (User thisUser in thisMediaServer.Users.Values) { if (thisUser.ConnectionId == Context.ConnectionId) { server = thisMediaServer; } } } //If we didn't find one, that means the user wasn't logged in, so forget about them if (server != null) { //Send the client disconnected IClientProxy client = Clients.Client(server.ServerSignalRId); if (client != null) { //Client exists, send them the message await client.SendAsync("UserDisconnect", server.SignalRConnectionToUser[Context.ConnectionId]); } } await base.OnDisconnectedAsync(exception); return; }
/// <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); }
public async Task DownloadMedia(int mediaServerUuid, string urlToDownload) { //Check that the media server exists if (!Server.UuidToMediaServer.ContainsKey(mediaServerUuid)) { return; } MediaServer mediaServer = Server.UuidToMediaServer[mediaServerUuid]; //Check that the user exists if (!mediaServer.SignalRConnectionToUser.ContainsKey(Context.ConnectionId)) { return; } int userUuid = mediaServer.SignalRConnectionToUser[Context.ConnectionId]; await Clients.Client(mediaServer.ServerSignalRId).SendAsync("ServerDownloadMedia", urlToDownload); }
public async Task ModifyQueue(int mediaServerUuid, int[] newQueue, bool shouldResetPlayback) { //Check that the media server exists if (!Server.UuidToMediaServer.ContainsKey(mediaServerUuid)) { return; } MediaServer mediaServer = Server.UuidToMediaServer[mediaServerUuid]; //Check that the user exists if (!mediaServer.SignalRConnectionToUser.ContainsKey(Context.ConnectionId)) { return; } int userUuid = mediaServer.SignalRConnectionToUser[Context.ConnectionId]; await Clients.Client(mediaServer.ServerSignalRId).SendAsync("ServerModifyQueue", newQueue, shouldResetPlayback); }
public async Task UpdatePlaybackState(int mediaServerUuid, PlaybackStateUpdateType opcode, string operand) { //Check that the media server exists if (!Server.UuidToMediaServer.ContainsKey(mediaServerUuid)) { return; } MediaServer mediaServer = Server.UuidToMediaServer[mediaServerUuid]; //Check that the user exists if (!mediaServer.SignalRConnectionToUser.ContainsKey(Context.ConnectionId)) { return; } int userUuid = mediaServer.SignalRConnectionToUser[Context.ConnectionId]; await Clients.Client(mediaServer.ServerSignalRId).SendAsync("ServerUpdatePlaybackState", opcode, operand); }
/// <summary> /// Gets the information about all servers as a JSON string /// </summary> /// <returns>The JSON string representing the information about the servers</returns> private static string GetServerlistJson() { SignalR.Serverlist.ServerlistInfo info = new SignalR.Serverlist.ServerlistInfo(); info.Servers = new SignalR.Serverlist.ServerInfo[UuidToMediaServer.Count]; Dictionary <int, MediaServer> .ValueCollection.Enumerator enumerator = UuidToMediaServer.Values.GetEnumerator(); for (int i = 0; i < UuidToMediaServer.Count; i++) { enumerator.MoveNext(); MediaServer thisServer = enumerator.Current; info.Servers[i] = new SignalR.Serverlist.ServerInfo() { Name = thisServer.Name, HasPassword = thisServer.Password != "", ImageUrl = thisServer.ImageUrl, UserCount = thisServer.UserCount, ServerUuid = thisServer.UUID }; } return(JsonConvert.SerializeObject(info)); }
/// <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); } }
public async Task Login(int serverUuid, string username, string password) { string errorMessage = ""; //Check that this connection ID isn't already logged in foreach (MediaServer thisMediaServer in Server.UuidToMediaServer.Values) { //Check the server's signalR client if (thisMediaServer.ServerSignalRId == Context.ConnectionId) { errorMessage = "Error: Already logged in. Try reloading"; } //Check all users foreach (User thisUser in thisMediaServer.Users.Values) { if (thisUser.ConnectionId == Context.ConnectionId) { errorMessage = "Error: Already logged in. Try reloading"; } } } //Check that the media server exists if (!Server.UuidToMediaServer.ContainsKey(serverUuid)) { errorMessage = "Error: Media server not found. Maybe the media server went offline?"; } //Check that the username is not whitespace if (string.IsNullOrWhiteSpace(username)) { errorMessage = "Error: Username cannot be blank"; } //Check that the username is longer than 32 characters if (username.Length > 32) { errorMessage = "Error: Username cannot be longer than 32 characters"; } //Send error message if (errorMessage != "") { await Clients.Caller.SendAsync("LoginError", errorMessage); return; } //Get media server MediaServer mediaServer = Server.UuidToMediaServer[serverUuid]; //Check that the passwords match if (mediaServer.Password != password) { errorMessage = "Error: Incorrect password"; } //Check that the username isn't already taken foreach (User thisUser in mediaServer.Users.Values) { if (thisUser.Username == username) { errorMessage = "Error: Username taken"; } } //Send error message if (errorMessage != "") { await Clients.Caller.SendAsync("LoginError", errorMessage); return; } //Right, we're good to go await Clients.Client(mediaServer.ServerSignalRId).SendAsync("LoginRequest", Context.ConnectionId, username); }