public void Parse_Returns_Expected_Data() { var rooms = new List <(string Name, int UserCount)>() { ("larry", 1), ("moe", 2), ("curly", 3), ("shemp", 4), }; var builder = new MessageBuilder() .WriteCode(MessageCode.Server.RoomList) .WriteInteger(rooms.Count); rooms.ForEach(room => builder.WriteString(room.Name)); builder.WriteInteger(rooms.Count); rooms.ForEach(room => builder.WriteInteger(room.UserCount)); var response = RoomList.FromByteArray(builder.Build()).ToList(); Assert.Equal(rooms.Count, response.Count); for (int i = 0; i < rooms.Count; i++) { Assert.Equal(rooms[i].Name, response[i].Name); Assert.Equal(rooms[i].UserCount, response[i].UserCount); } }
public void Parse_Throws_MessageException_On_Code_Mismatch() { var msg = new MessageBuilder() .WriteCode(MessageCode.Peer.BrowseRequest) .Build(); var ex = Record.Exception(() => RoomList.FromByteArray(msg)); Assert.NotNull(ex); Assert.IsType <MessageException>(ex); }
public void Parse_Throws_MessageReadException_On_Missing_Data() { var msg = new MessageBuilder() .WriteCode(MessageCode.Server.RoomList) .WriteInteger(1) .Build(); var ex = Record.Exception(() => RoomList.FromByteArray(msg)); Assert.NotNull(ex); Assert.IsType <MessageReadException>(ex); }
/// <summary> /// Handles incoming messages. /// </summary> /// <param name="sender">The <see cref="IMessageConnection"/> instance from which the message originated.</param> /// <param name="message">The message.</param> public async void HandleMessage(object sender, byte[] message) { var code = new MessageReader <MessageCode.Server>(message).ReadCode(); Diagnostic.Debug($"Server message received: {code}"); try { switch (code) { case MessageCode.Server.ParentMinSpeed: case MessageCode.Server.ParentSpeedRatio: case MessageCode.Server.WishlistInterval: SoulseekClient.Waiter.Complete(new WaitKey(code), IntegerResponse.FromByteArray <MessageCode.Server>(message)); break; case MessageCode.Server.Login: SoulseekClient.Waiter.Complete(new WaitKey(code), LoginResponse.FromByteArray(message)); break; case MessageCode.Server.RoomList: SoulseekClient.Waiter.Complete(new WaitKey(code), RoomList.FromByteArray(message)); break; case MessageCode.Server.PrivilegedUsers: SoulseekClient.Waiter.Complete(new WaitKey(code), PrivilegedUserList.FromByteArray(message)); break; case MessageCode.Server.NetInfo: var netInfo = NetInfo.FromByteArray(message); try { await SoulseekClient.DistributedConnectionManager.AddParentConnectionAsync(netInfo.Parents).ConfigureAwait(false); } catch (Exception ex) { Diagnostic.Debug($"Error handling NetInfo message: {ex.Message}"); } break; case MessageCode.Server.ConnectToPeer: ConnectToPeerResponse connectToPeerResponse = default; try { connectToPeerResponse = ConnectToPeerResponse.FromByteArray(message); if (connectToPeerResponse.Type == Constants.ConnectionType.Transfer) { // ensure that we are expecting at least one file from this user before we connect. the response // doesn't contain any other identifying information about the file. if (!SoulseekClient.Downloads.IsEmpty && SoulseekClient.Downloads.Values.Any(d => d.Username == connectToPeerResponse.Username)) { var(connection, remoteToken) = await SoulseekClient.PeerConnectionManager.GetTransferConnectionAsync(connectToPeerResponse).ConfigureAwait(false); var download = SoulseekClient.Downloads.Values.FirstOrDefault(v => v.RemoteToken == remoteToken && v.Username == connectToPeerResponse.Username); if (download != default(Transfer)) { SoulseekClient.Waiter.Complete(new WaitKey(Constants.WaitKey.IndirectTransfer, download.Username, download.Filename, download.RemoteToken), connection); } } else { throw new SoulseekClientException($"Unexpected transfer request from {connectToPeerResponse.Username} ({connectToPeerResponse.IPAddress}:{connectToPeerResponse.Port}); Ignored"); } } else if (connectToPeerResponse.Type == Constants.ConnectionType.Peer) { await SoulseekClient.PeerConnectionManager.GetOrAddMessageConnectionAsync(connectToPeerResponse).ConfigureAwait(false); } else if (connectToPeerResponse.Type == Constants.ConnectionType.Distributed) { await SoulseekClient.DistributedConnectionManager.AddChildConnectionAsync(connectToPeerResponse).ConfigureAwait(false); } else { throw new MessageException($"Unknown Connect To Peer connection type '{connectToPeerResponse.Type}'"); } } catch (Exception ex) { Diagnostic.Debug($"Error handling ConnectToPeer response from {connectToPeerResponse?.Username} ({connectToPeerResponse?.IPAddress}:{connectToPeerResponse.Port}): {ex.Message}"); } break; case MessageCode.Server.AddUser: var addUserResponse = AddUserResponse.FromByteArray(message); SoulseekClient.Waiter.Complete(new WaitKey(code, addUserResponse.Username), addUserResponse); break; case MessageCode.Server.GetStatus: var statsResponse = GetStatusResponse.FromByteArray(message); SoulseekClient.Waiter.Complete(new WaitKey(code, statsResponse.Username), statsResponse); UserStatusChanged?.Invoke(this, new UserStatusChangedEventArgs(statsResponse)); break; case MessageCode.Server.PrivateMessage: var pm = PrivateMessage.FromByteArray(message); PrivateMessageReceived?.Invoke(this, pm); if (SoulseekClient.Options.AutoAcknowledgePrivateMessages) { await SoulseekClient.AcknowledgePrivateMessageAsync(pm.Id, CancellationToken.None).ConfigureAwait(false); } break; case MessageCode.Server.GetPeerAddress: var peerAddressResponse = GetPeerAddressResponse.FromByteArray(message); SoulseekClient.Waiter.Complete(new WaitKey(code, peerAddressResponse.Username), peerAddressResponse); break; default: Diagnostic.Debug($"Unhandled server message: {code}; {message.Length} bytes"); break; } } catch (Exception ex) { Diagnostic.Warning($"Error handling server message: {code}; {ex.Message}", ex); } }