private void OnChatMemberInfo(SteamFriends.ChatMemberInfoCallback callback) { var stateChangeInfo = callback.StateChangeInfo; var chatRoomID = callback.ChatRoomID; var chatterID = stateChangeInfo.ChatterActedOn; if (!ChatRoomManager.ChatRoomExists(chatRoomID) || !UserManager.UserExists(chatterID)) { return; } var user = UserManager.GetUser(chatterID); var chatRoom = ChatRoomManager.GetChatRoom(chatRoomID); switch (stateChangeInfo.StateChange) { case EChatMemberStateChange.Banned: case EChatMemberStateChange.Disconnected: case EChatMemberStateChange.Kicked: case EChatMemberStateChange.Left: Console.WriteLine("Removing {0} from {1}", user.Name, chatRoom.Name); // Trigger LeftChat for each module. ModuleManager.LeftChat(callback); // Remove this user from the chat room. chatRoom.RemoveUser(user); break; } }
public async Task AuthenticationFailedForSecondConnection() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authenticationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; var duplexPipe1 = new DuplexPipe(); var connection1 = new Connection(duplexPipe1, null, null); var client1 = new ChatClient(connection1, manager); await duplexPipe1.ReceivePipe.Writer.WriteAsync(authenticationPacket); await duplexPipe1.ReceivePipe.Writer.FlushAsync(); var duplexPipe2 = new DuplexPipe(); var connection2 = new Connection(duplexPipe2, null, null); var client2 = new ChatClient(connection2, manager); bool disconnectedRaised = false; client2.Disconnected += (sender, e) => disconnectedRaised = true; await duplexPipe2.ReceivePipe.Writer.WriteAsync(authenticationPacket); await duplexPipe2.ReceivePipe.Writer.FlushAsync(); Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(client1)); Assert.That(connection2.Connected, Is.False); Assert.That(disconnectedRaised, Is.True); }
public void GetChatRoomNull() { var manager = new ChatRoomManager(); var room = manager.GetChatRoom(9999); Assert.That(room, Is.Null); }
public void AuthentificationFailedForSecondConnection() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; var connection1 = new Mock <IConnection>(); var client1 = new ChatClient(connection1.Object, manager); connection1.Raise(c => c.PacketReceived += null, connection1.Object, authentificationPacket); var connection2 = new Mock <IConnection>(); var disconnectedRaised = false; var client2 = new ChatClient(connection2.Object, manager); client2.Disconnected += (sender, e) => disconnectedRaised = true; connection2.Setup(c => c.Disconnect()).Verifiable(); connection2.Raise(c => c.PacketReceived += null, connection2.Object, authentificationPacket); Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(client1)); connection2.VerifyAll(); Assert.That(disconnectedRaised, Is.True); }
public void ClientLoggedOff() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection1 = new Mock <IConnection>(); var client1 = new ChatClient(connection1.Object, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Alice", "94371960")); var authentificationPacket1 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection1.Raise(c => c.PacketReceived += null, connection1.Object, authentificationPacket1); var connection2 = new Mock <IConnection>(); var client2 = new ChatClient(connection2.Object, manager); var authentificationPacket2 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xC5, 0xFB, 0x98, 0xCB, 0xFE, 0x92, 0xCA, 0xFF, 0xAB, 0xFC }; connection2.Raise(c => c.PacketReceived += null, connection2.Object, authentificationPacket2); client1.LogOff(); var expectedPacket = new byte[] { 0xC1, 0x0F, 0x01, 0x01, 0x00, 0x42, 0x6F, 0x62, 0, 0, 0, 0, 0, 0, 0, }; connection2.Verify(c => c.Send(It.Is <byte[]>(p => p.SequenceEqual(expectedPacket))), Times.Once); Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(client2)); }
public void ClientLoggedOff() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection1 = MockRepository.GenerateMock <IConnection>(); var client1 = new ChatClient(connection1, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Alice", "94371960")); var authentificationPacket1 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection1.Raise(c => c.PacketReceived += null, connection1, authentificationPacket1); var connection2 = MockRepository.GenerateMock <IConnection>(); var client2 = new ChatClient(connection2, manager); var authentificationPacket2 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xC5, 0xFB, 0x98, 0xCB, 0xFE, 0x92, 0xCA, 0xFF, 0xAB, 0xFC }; connection2.Raise(c => c.PacketReceived += null, connection2, authentificationPacket2); client1.LogOff(); var arguments = connection2.GetArgumentsForCallsMadeOn(c => c.Send(null)); var sentPacket = arguments.FirstOrDefault(p => (p.Last() as byte[])[0] == 0xC1)[0] as byte[]; var expectedPacket = new byte[] { 0xC1, 0x0F, 0x01, 0x01, 0x00, 0x42, 0x6F, 0x62, 0, 0, 0, 0, 0, 0, 0, }; Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(client2)); Assert.That(sentPacket, Is.Not.Null); Assert.That(sentPacket, Is.EquivalentTo(expectedPacket)); }
public void GetChatRoomNull() { var manager = new ChatRoomManager(new NullLoggerFactory()); var room = manager.GetChatRoom(9999); Assert.That(room, Is.Null); }
public void RoomCreation() { var manager = new ChatRoomManager(new NullLoggerFactory()); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); Assert.That(room, Is.Not.Null); }
public void SetNickname() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection = MockRepository.GenerateMock <IConnection>(); var client = new ChatClient(connection, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection.Raise(c => c.PacketReceived += null, connection, authentificationPacket); Assert.That(client.Nickname, Is.EqualTo("Bob")); }
public void AuthentificationSuccess() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection = new Mock <IConnection>(); var client = new ChatClient(connection.Object, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection.Raise(c => c.PacketReceived += null, connection.Object, authentificationPacket); Assert.That(room.ConnectedClients, Contains.Item(client)); }
public void SetClientIndex() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection = new Mock <IConnection>(); var client = new ChatClient(connection.Object, manager); var authInfo = new ChatServerAuthenticationInfo(3, roomId, "Bob", "128450673"); room.RegisterClient(authInfo); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection.Raise(c => c.PacketReceived += null, connection.Object, authentificationPacket); Assert.That(client.Index, Is.EqualTo(authInfo.Index)); }
public void AuthentificationFailedByWrongToken() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection = MockRepository.GenerateMock <IConnection>(); var client = new ChatClient(connection, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450674")); connection.Expect(c => c.Disconnect()); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection.Raise(c => c.PacketReceived += null, connection, authentificationPacket); Assert.That(room.ConnectedClients.Contains(client), Is.False); connection.VerifyAllExpectations(); }
public async Task SetNickname() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var duplexPipe = new DuplexPipe(); var connection = new Connection(duplexPipe, null, null); var client = new ChatClient(connection, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authenticationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; await duplexPipe.ReceivePipe.Writer.WriteAsync(authenticationPacket); await duplexPipe.ReceivePipe.Writer.FlushAsync(); Assert.That(client.Nickname, Is.EqualTo("Bob")); }
public async Task AuthenticationFailedByWrongToken() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var duplexPipe = new DuplexPipe(); var connection = new Connection(duplexPipe, null, null); var client = new ChatClient(connection, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450674")); var authenticationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; await duplexPipe.ReceivePipe.Writer.WriteAsync(authenticationPacket); await duplexPipe.ReceivePipe.Writer.FlushAsync(); Assert.That(room.ConnectedClients.Contains(client), Is.False); Assert.That(connection.Connected, Is.False); }
public void RoomClientListSentAfterJoin() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection = new Mock <IConnection>(); var client = new ChatClient(connection.Object, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection.Raise(c => c.PacketReceived += null, connection.Object, authentificationPacket); var expectedPacket = new byte[] { 0xC2, 0x00, 0x13, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x42, 0x6F, 0x62, 0, 0, 0, 0, 0, 0, 0 }; connection.Verify(c => c.Send(It.Is <byte[]>(arg => arg.SequenceEqual(expectedPacket))), Times.Once); Assert.That(client.Nickname, Is.EqualTo("Bob")); }
public async Task AuthenticationSuccess() { var manager = new ChatRoomManager(new NullLoggerFactory()); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var duplexPipe = new DuplexPipe(); var connection = new Connection(duplexPipe, null, null, new NullLogger <Connection>()); var client = new ChatClient(connection, manager, new NullLogger <ChatClient>()); room !.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authenticationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; await duplexPipe.ReceivePipe.Writer.WriteAsync(authenticationPacket); await duplexPipe.ReceivePipe.Writer.FlushAsync(); Assert.That(room.ConnectedClients, Contains.Item(client)); }
public void RoomClientListSentAfterJoin() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection = MockRepository.GenerateMock <IConnection>(); var client = new ChatClient(connection, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; connection.Raise(c => c.PacketReceived += null, connection, authentificationPacket); var arguments = connection.GetArgumentsForCallsMadeOn(c => c.Send(null)); var sentPacket = arguments.FirstOrDefault(p => (p.First() as byte[])[0] == 0xC2)[0] as byte[]; var expectedPacket = new byte[] { 0xC2, 0x00, 0x13, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x42, 0x6F, 0x62, 0, 0, 0, 0, 0, 0, 0 }; Assert.That(sentPacket, Is.Not.Null); Assert.That(sentPacket, Is.EquivalentTo(expectedPacket)); }
public async Task RoomClientListSentForSecondClient() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var duplexPipe1 = new DuplexPipe(); var connection1 = new Connection(duplexPipe1, null, null); var client1 = new ChatClient(connection1, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Alice", "94371960")); var authenticationPacket1 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; duplexPipe1.ReceivePipe.Writer.Write(authenticationPacket1); await duplexPipe1.ReceivePipe.Writer.FlushAsync(); var duplexPipe2 = new DuplexPipe(); var connection2 = new Connection(duplexPipe2, null, null); var client2 = new ChatClient(connection2, manager); var authenticationPacket2 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xC5, 0xFB, 0x98, 0xCB, 0xFE, 0x92, 0xCA, 0xFF, 0xAB, 0xFC }; duplexPipe2.ReceivePipe.Writer.Write(authenticationPacket2); await duplexPipe2.ReceivePipe.Writer.FlushAsync(); var expectedPacket = new byte[] { 0xC2, 0x00, 0x1E, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x42, 0x6F, 0x62, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x41, 0x6C, 0x69, 0x63, 0x65, 0, 0, 0, 0, 0, }; var readResult = await duplexPipe2.SendPipe.Reader.ReadAsync(); var result = readResult.Buffer.ToArray(); Assert.That(result, Is.EquivalentTo(expectedPacket)); Assert.That(client1.Nickname, Is.EqualTo("Bob")); Assert.That(client2.Nickname, Is.EqualTo("Alice")); }
public async Task ClientLoggedOff() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var duplexPipe1 = new DuplexPipe(); var connection1 = new Connection(duplexPipe1, null, null); var client1 = new ChatClient(connection1, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Alice", "94371960")); var authenticationPacket1 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; await duplexPipe1.ReceivePipe.Writer.WriteAsync(authenticationPacket1); await duplexPipe1.ReceivePipe.Writer.FlushAsync(); var duplexPipe2 = new DuplexPipe(); var connection2 = new Connection(duplexPipe2, null, null); var client2 = new ChatClient(connection2, manager); var authenticationPacket2 = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xC5, 0xFB, 0x98, 0xCB, 0xFE, 0x92, 0xCA, 0xFF, 0xAB, 0xFC }; await duplexPipe2.ReceivePipe.Writer.WriteAsync(authenticationPacket2); await duplexPipe2.ReceivePipe.Writer.FlushAsync(); client1.LogOff(); var expectedPacket = new byte[] { 0xC1, 0x0F, 0x01, 0x01, 0x00, 0x42, 0x6F, 0x62, 0, 0, 0, 0, 0, 0, 0, }; var readResult = await duplexPipe2.SendPipe.Reader.ReadAsync(); var packet = readResult.Buffer.ToArray().TakeLast(expectedPacket.Length).ToArray(); Assert.That(packet, Is.EquivalentTo(expectedPacket)); Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(client2)); }
public async Task RoomClientListSentAfterJoin() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var duplexPipe = new DuplexPipe(); var connection = new Connection(duplexPipe, null, null); var client = new ChatClient(connection, manager); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authenticationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; duplexPipe.ReceivePipe.Writer.Write(authenticationPacket); await duplexPipe.ReceivePipe.Writer.FlushAsync(); var expectedPacket = new byte[] { 0xC2, 0x00, 0x13, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x42, 0x6F, 0x62, 0, 0, 0, 0, 0, 0, 0 }; var readResult = await duplexPipe.SendPipe.Reader.ReadAsync(); var result = readResult.Buffer.ToArray(); Assert.That(result, Is.EquivalentTo(expectedPacket)); Assert.That(client.Nickname, Is.EqualTo("Bob")); }
public void AuthentificationFailedForSecondConnection() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); room.RegisterClient(new ChatServerAuthenticationInfo(room.GetNextClientIndex(), roomId, "Bob", "128450673")); var authentificationPacket = new byte[] { 0xC1, 0x10, 0x00, 0x00, (byte)roomId, (byte)(roomId >> 8), 0xCD, 0xFD, 0x93, 0xC8, 0xFA, 0x9B, 0xCA, 0xF8, 0x98, 0xFC }; var connection1 = MockRepository.GenerateMock <IConnection>(); var client1 = new ChatClient(connection1, manager); connection1.Raise(c => c.PacketReceived += null, connection1, authentificationPacket); var connection2 = MockRepository.GenerateMock <IConnection>(); var client2 = new ChatClient(connection2, manager); connection2.Expect(c => c.Disconnect()).Repeat.AtLeastOnce(); connection2.Raise(c => c.PacketReceived += null, connection2, authentificationPacket); Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(client1)); connection2.VerifyAllExpectations(); }