public void SendMessage() { const ushort roomId = 4711; const string chatMessage = "foobar1234567890"; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId0 = room.GetNextClientIndex(); var clientId1 = room.GetNextClientIndex(); var authenticationInfo0 = new ChatServerAuthenticationInfo(clientId0, roomId, "Alice", "99999"); var authenticationInfo1 = new ChatServerAuthenticationInfo(clientId1, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo0); room.RegisterClient(authenticationInfo1); var chatClient0 = new Mock <IChatClient>(); chatClient0.Setup(c => c.AuthenticationToken).Returns(authenticationInfo0.AuthenticationToken); var chatClient1 = new Mock <IChatClient>(); chatClient1.Setup(c => c.AuthenticationToken).Returns(authenticationInfo1.AuthenticationToken); room.TryJoin(chatClient0.Object); room.TryJoin(chatClient1.Object); room.SendMessage(clientId1, chatMessage); chatClient0.Verify(c => c.SendMessage(clientId1, chatMessage), Times.Once); chatClient1.Verify(c => c.SendMessage(clientId1, chatMessage), Times.Once); }
/// <summary> /// Sends the authentication information back to the ExDB-Server. /// </summary> /// <param name="authenticationInfo">The authentication information.</param> /// <param name="friendAuthenticationInfo">The friend authentication information.</param> /// <param name="clientId">The client identifier on the server where the client plays on.</param> /// <param name="serverId">The server identifier where the client plays on.</param> /// <param name="type">The type. Usually 0 for the player who requested the chat and 1 for the other player.</param> private void SendAuthentication(ChatServerAuthenticationInfo authenticationInfo, ChatServerAuthenticationInfo?friendAuthenticationInfo, ushort clientId, ushort serverId, byte type) { this.logger.LogDebug($"Registered client {authenticationInfo.ClientName} with index {authenticationInfo.Index} and token {authenticationInfo.AuthenticationToken}"); var token = uint.Parse(authenticationInfo.AuthenticationToken); uint friendToken = 0; if (friendAuthenticationInfo != null) { friendToken = uint.Parse(friendAuthenticationInfo.AuthenticationToken); } var roomId = authenticationInfo.RoomId; using var writer = this.connection !.StartSafeWrite(0xC1, 0x2C); var packet = writer.Span; packet[2] = 0xA0; packet[3] = 0x01; WriteUInt16LittleEndian(packet.Slice(4), roomId); packet.Slice(6).WriteString(authenticationInfo.ClientName, Encoding.UTF8); if (friendAuthenticationInfo != null) { packet.Slice(16).WriteString(friendAuthenticationInfo.ClientName, Encoding.UTF8); } WriteUInt16LittleEndian(packet.Slice(26), clientId); WriteUInt16LittleEndian(packet.Slice(28), serverId); WriteUInt32LittleEndian(packet.Slice(32), token); WriteUInt32LittleEndian(packet.Slice(36), friendToken); packet[40] = type; writer.Commit(); }
public void SendMessage() { const ushort roomId = 4711; const string chatMessage = "foobar1234567890"; var room = new ChatRoom(roomId); var clientId0 = room.GetNextClientIndex(); var clientId1 = room.GetNextClientIndex(); var authenticationInfo0 = new ChatServerAuthenticationInfo(clientId0, roomId, "Alice", "99999"); var authenticationInfo1 = new ChatServerAuthenticationInfo(clientId1, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo0); room.RegisterClient(authenticationInfo1); var chatClient0 = MockRepository.GenerateMock <IChatClient>(); chatClient0.Stub(c => c.AuthenticationToken).Return(authenticationInfo0.AuthenticationToken); var chatClient1 = MockRepository.GenerateMock <IChatClient>(); chatClient1.Stub(c => c.AuthenticationToken).Return(authenticationInfo1.AuthenticationToken); room.TryJoin(chatClient0); room.TryJoin(chatClient1); chatClient0.Expect(c => c.SendMessage(clientId1, chatMessage)); chatClient1.Expect(c => c.SendMessage(clientId1, chatMessage)); room.SendMessage(clientId1, chatMessage); chatClient0.VerifyAllExpectations(); chatClient1.VerifyAllExpectations(); }
/// <summary> /// Sends the authentication information back to the ExDB-Server. /// </summary> /// <param name="authenticationInfo">The authentication information.</param> /// <param name="friendAuthenticationInfo">The friend authentication information.</param> /// <param name="clientId">The client identifier on the server where the client plays on.</param> /// <param name="serverId">The server identifier where the client plays on.</param> /// <param name="type">The type. Usually 0 for the player who requested the chat and 1 for the other player.</param> private void SendAuthentication(ChatServerAuthenticationInfo authenticationInfo, ChatServerAuthenticationInfo friendAuthenticationInfo, ushort clientId, ushort serverId, byte type) { Log.Debug($"Registered client {authenticationInfo.ClientName} with index {authenticationInfo.Index} and token {authenticationInfo.AuthenticationToken}"); var token = uint.Parse(authenticationInfo.AuthenticationToken); uint friendToken = 0; if (friendAuthenticationInfo != null) { friendToken = uint.Parse(friendAuthenticationInfo.AuthenticationToken); } var roomId = authenticationInfo.RoomId; using (var writer = this.connection.StartSafeWrite(0xC1, 0x2C)) { var packet = writer.Span; packet[2] = 0xA0; packet[3] = 0x01; packet.Slice(4).SetShortBigEndian(roomId); packet.Slice(6).WriteString(authenticationInfo.ClientName, Encoding.UTF8); if (friendAuthenticationInfo != null) { packet.Slice(16).WriteString(friendAuthenticationInfo.ClientName, Encoding.UTF8); } packet.Slice(26).SetShortBigEndian(clientId); packet.Slice(28).SetShortBigEndian(serverId); packet.Slice(32).SetIntegerBigEndian(token); packet.Slice(36).SetIntegerBigEndian(friendToken); packet[40] = type; writer.Commit(); } }
public void RoomClosedEvent() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId0 = room.GetNextClientIndex(); var clientId1 = room.GetNextClientIndex(); var authenticationInfo0 = new ChatServerAuthenticationInfo(clientId0, roomId, "Alice", "99999"); var authenticationInfo1 = new ChatServerAuthenticationInfo(clientId1, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo0); room.RegisterClient(authenticationInfo1); var chatClient0 = new Mock <IChatClient>(); chatClient0.Setup(c => c.AuthenticationToken).Returns(authenticationInfo0.AuthenticationToken); var chatClient1 = new Mock <IChatClient>(); chatClient1.Setup(c => c.AuthenticationToken).Returns(authenticationInfo1.AuthenticationToken); var eventCalled = false; room.RoomClosed += (sender, e) => eventCalled = true; room.TryJoin(chatClient0.Object); room.TryJoin(chatClient1.Object); room.Leave(chatClient0.Object); room.Leave(chatClient1.Object); Assert.That(eventCalled, Is.True); }
public void SendLeftMessage() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId0 = room.GetNextClientIndex(); var clientId1 = room.GetNextClientIndex(); var authenticationInfo0 = new ChatServerAuthenticationInfo(clientId0, roomId, "Alice", "99999"); var authenticationInfo1 = new ChatServerAuthenticationInfo(clientId1, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo0); room.RegisterClient(authenticationInfo1); var chatClient0 = new Mock <IChatClient>(); chatClient0.SetupAllProperties(); chatClient0.Setup(c => c.AuthenticationToken).Returns(authenticationInfo0.AuthenticationToken); var chatClient1 = new Mock <IChatClient>(); chatClient1.SetupAllProperties(); chatClient1.Setup(c => c.AuthenticationToken).Returns(authenticationInfo1.AuthenticationToken); room.TryJoin(chatClient0.Object); room.TryJoin(chatClient1.Object); room.Leave(chatClient0.Object); chatClient1.Verify(c => c.SendChatRoomClientUpdate(clientId0, authenticationInfo0.ClientName, ChatRoomClientUpdateType.Left), Times.Once); }
public void SendLeftMessage() { const ushort roomId = 4711; var room = new ChatRoom(roomId); var clientId0 = room.GetNextClientIndex(); var clientId1 = room.GetNextClientIndex(); var authenticationInfo0 = new ChatServerAuthenticationInfo(clientId0, roomId, "Alice", "99999"); var authenticationInfo1 = new ChatServerAuthenticationInfo(clientId1, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo0); room.RegisterClient(authenticationInfo1); var chatClient0 = MockRepository.GenerateStub <IChatClient>(); chatClient0.Stub(c => c.AuthenticationToken).Return(authenticationInfo0.AuthenticationToken); var chatClient1 = MockRepository.GenerateMock <IChatClient>(); chatClient1.Stub(c => c.AuthenticationToken).Return(authenticationInfo1.AuthenticationToken); room.TryJoin(chatClient0); room.TryJoin(chatClient1); chatClient1.Expect(c => c.SendChatRoomClientUpdate(clientId0, authenticationInfo0.ClientName, ChatRoomClientUpdateType.Left)); room.Leave(chatClient0); chatClient1.VerifyAllExpectations(); }
public void RegisterClientWithDifferentRoomId() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId - 1, "Bob", "123456789"); Assert.Throws <ArgumentException>(() => room.RegisterClient(authenticationInfo)); }
public void RegisterClientWithCorrectRoomId() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); Assert.DoesNotThrow(() => room.RegisterClient(authenticationInfo)); }
public void TryJoinNullClient() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); Assert.Throws <ArgumentNullException>(() => room.TryJoin(null)); }
public void TryJoinWithUnauthenticatedClient() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); var chatClient = new Mock <IChatClient>(); Assert.That(room.TryJoin(chatClient.Object), Is.False); }
public void TryJoinWithUnauthenticatedClient() { const ushort roomId = 4711; var room = new ChatRoom(roomId); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); var chatClient = MockRepository.GenerateStub <IChatClient>(); Assert.That(room.TryJoin(chatClient), Is.False); }
public void TryJoinWithAuthenticatedClientButWrongToken() { const ushort roomId = 4711; var room = new ChatRoom(roomId); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); var chatClient = new Mock <IChatClient>(); chatClient.Setup(c => c.AuthenticationToken).Returns("987654321"); Assert.That(room.TryJoin(chatClient.Object), Is.False); }
public void ChatRoomClientListSent() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); var chatClient = new Mock <IChatClient>(); chatClient.Setup(c => c.AuthenticationToken).Returns(authenticationInfo.AuthenticationToken); room.TryJoin(chatClient.Object); chatClient.Verify(c => c.SendChatRoomClientList(room.ConnectedClients), Times.Once); }
public void ConnectedClients() { const ushort roomId = 4711; var room = new ChatRoom(roomId); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); var chatClient = MockRepository.GenerateStub <IChatClient>(); chatClient.Stub(c => c.AuthenticationToken).Return(authenticationInfo.AuthenticationToken); room.TryJoin(chatClient); Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(chatClient)); }
public void ChatRoomClientListSent() { const ushort roomId = 4711; var room = new ChatRoom(roomId); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); var chatClient = MockRepository.GenerateMock <IChatClient>(); chatClient.Stub(c => c.AuthenticationToken).Return(authenticationInfo.AuthenticationToken); chatClient.Expect(c => c.SendChatRoomClientList(room.ConnectedClients)); room.TryJoin(chatClient); chatClient.VerifyAllExpectations(); }
public void ConnectedClients() { const ushort roomId = 4711; var room = new ChatRoom(roomId, new NullLogger <ChatRoom>()); var clientId = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(clientId, roomId, "Bob", "123456789"); room.RegisterClient(authenticationInfo); var chatClient = new Mock <IChatClient>(); chatClient.Setup(c => c.AuthenticationToken).Returns(authenticationInfo.AuthenticationToken); room.TryJoin(chatClient.Object); Assert.That(room.ConnectedClients, Has.Count.EqualTo(1)); Assert.That(room.ConnectedClients, Contains.Item(chatClient.Object)); }
/// <summary> /// Registers a chat client to the chatroom. this is only called /// by the game server which will send id to the participants /// over the games connection. /// </summary> /// <param name="authenticationInfo">Authentication information of the participant.</param> public void RegisterClient(ChatServerAuthenticationInfo authenticationInfo) { if (this.isClosing) { throw new ObjectDisposedException("Chat room is already disposed."); } if (authenticationInfo.RoomId != this.RoomId) { throw new ArgumentException( $"The RoomId of the authentication info ({authenticationInfo.RoomId}) does not match with this RoomId ({this.RoomId})."); } this.AuthenticationRequiredUntil = authenticationInfo.AuthenticationRequiredUntil; this.registeredClients.Add(authenticationInfo); }
public void SetClientIndex() { var manager = new ChatRoomManager(); var roomId = manager.CreateChatRoom(); var room = manager.GetChatRoom(roomId); var connection = MockRepository.GenerateMock <IConnection>(); var client = new ChatClient(connection, 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, authentificationPacket); Assert.That(client.Index, Is.EqualTo(authInfo.Index)); }
/// <summary> /// The specified client will join the chatroom, if its registered. The Nickname is set to the clients object. /// </summary> /// <param name="chatClient">The chat client.</param> /// <returns>True, if the <paramref name="chatClient"/> provides the correct registered id with it's token.</returns> internal bool TryJoin(IChatClient chatClient) { if (chatClient == null) { throw new ArgumentNullException(nameof(chatClient)); } if (this.isClosing) { throw new ObjectDisposedException("Chat room is already disposed."); } Log.Debug($"Client {chatClient.Index} is trying to join the room {this.RoomId} with token '{chatClient.AuthenticationToken}'"); this.lockSlim.EnterWriteLock(); try { ChatServerAuthenticationInfo authenticationInformation = this.registeredClients.FirstOrDefault(info => string.Equals(info.AuthenticationToken, chatClient.AuthenticationToken)); if (authenticationInformation != null) { if (authenticationInformation.AuthenticationRequiredUntil < DateTime.Now) { Log.Info($"Client {chatClient.Index} has tried to join the room {this.RoomId} with token '{chatClient.AuthenticationToken}', but was too late. It was valid until {authenticationInformation.AuthenticationRequiredUntil}."); } else { chatClient.Nickname = authenticationInformation.ClientName; chatClient.Index = authenticationInformation.Index; this.registeredClients.Remove(authenticationInformation); this.SendChatRoomClientUpdate(chatClient, ChatRoomClientUpdateType.Joined); this.connectedClients.Add(chatClient); chatClient.SendChatRoomClientList(this.connectedClients); return(true); } } else { Log.Info($"Client {chatClient.Index} has tried to join the room {this.RoomId} with token '{chatClient.AuthenticationToken}', but was not registered."); } } finally { this.lockSlim.ExitWriteLock(); } return(false); }
/// <inheritdoc/> public ChatServerAuthenticationInfo RegisterClient(ushort roomId, string clientName) { var room = this.manager.GetChatRoom(roomId); if (room == null) { var errorMessage = $"RegisterClient: Could not find chat room with id {roomId} for '{clientName}'."; Log.Error(errorMessage); throw new ArgumentException(errorMessage, nameof(roomId)); } var index = room.GetNextClientIndex(); var authenticationInfo = new ChatServerAuthenticationInfo(index, roomId, clientName, this.GetRandomAuthenticationToken(index)); room.RegisterClient(authenticationInfo); return(authenticationInfo); }
/// <inheritdoc/> public void ChatRoomCreated(ChatServerAuthenticationInfo authenticationInfo, string friendName, bool success) { using (var writer = this.connection.StartSafeWrite(0xC3, 0x24)) { var chatServerIp = this.friendServer.GetChatserverIP(); var packet = writer.Span; packet[2] = 0xCA; packet.Slice(3, 15).WriteString(chatServerIp, Encoding.UTF8); var chatRoomId = authenticationInfo.RoomId; packet[18] = chatRoomId.GetLowByte(); packet[19] = chatRoomId.GetHighByte(); packet.Slice(20, 4).SetIntegerBigEndian(uint.Parse(authenticationInfo.AuthenticationToken)); packet[24] = 0x01; // type packet.Slice(25, 10).WriteString(friendName, Encoding.UTF8); packet[35] = success ? (byte)1 : (byte)0; writer.Commit(); } }
public async Task SetClientIndex() { 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); var authInfo = new ChatServerAuthenticationInfo(3, roomId, "Bob", "128450673"); room.RegisterClient(authInfo); 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.Index, Is.EqualTo(authInfo.Index)); }
/// <inheritdoc/> public void ChatRoomCreated(ChatServerAuthenticationInfo authenticationInfo, string friendname, bool success) { var authenticationTokenArray = uint.Parse(authenticationInfo.AuthenticationToken).ToBytesSmallEndian(); var chatRoomId = authenticationInfo.RoomId; var packet = new byte[] { 0xC3, 0x24, 0xCA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // chat server ip (without port) chatRoomId.GetLowByte(), chatRoomId.GetHighByte(), authenticationTokenArray[3], authenticationTokenArray[2], authenticationTokenArray[1], authenticationTokenArray[0], 0x01, // type 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // friendname success ? (byte)1 : (byte)0 }; // chatserver unavailable would have success = 2, type = 0xAF var chatServerIp = this.friendServer.GetChatserverIP(); Encoding.ASCII.GetBytes(chatServerIp, 0, chatServerIp.Length, packet, 3); Encoding.UTF8.GetBytes(friendname, 0, friendname.Length, packet, 25); this.connection.Send(packet); }
/// <summary> /// Sends the authentication information back to the ExDB-Server. /// </summary> /// <param name="authenticationInfo">The authentication information.</param> /// <param name="friendAuthenticationInfo">The friend authentication information.</param> /// <param name="clientId">The client identifier on the server where the client plays on.</param> /// <param name="serverId">The server identifier where the client plays on.</param> /// <param name="type">The type. Usually 0 for the player who requested the chat and 1 for the other player.</param> private void SendAuthentication(ChatServerAuthenticationInfo authenticationInfo, ChatServerAuthenticationInfo friendAuthenticationInfo, ushort clientId, ushort serverId, byte type) { Log.Debug($"Registered client {authenticationInfo.ClientName} with index {authenticationInfo.Index} and token {authenticationInfo.AuthenticationToken}"); var token = uint.Parse(authenticationInfo.AuthenticationToken); uint friendToken = 0; if (friendAuthenticationInfo != null) { friendToken = uint.Parse(friendAuthenticationInfo.AuthenticationToken); } var roomId = authenticationInfo.RoomId; var packet = new byte[] { 0xC1, 0x2C, 0xA0, 0x01, (byte)roomId, (byte)(roomId >> 8), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, clientId.GetLowByte(), clientId.GetHighByte(), // user client id serverId.GetLowByte(), serverId.GetHighByte(), // server id 0, 0, // 30+31, padding for the alignment of the following token 0, 0, 0, 0, // token 0, 0, 0, 0, // friend token type, 0, 0, 0 // padding? }; packet.SetIntegerBigEndian(token, 32); packet.SetIntegerBigEndian(friendToken, 36); Encoding.UTF8.GetBytes(authenticationInfo.ClientName, 0, authenticationInfo.ClientName.Length, packet, 6); if (friendAuthenticationInfo != null) { Encoding.UTF8.GetBytes(friendAuthenticationInfo.ClientName, 0, friendAuthenticationInfo.ClientName.Length, packet, 16); } this.connection.Send(packet); }
/// <inheritdoc/> public void ChatRoomCreated(ChatServerAuthenticationInfo authenticationInfo, string creatorName) { Player player = this.gameContext.GetPlayerByCharacterName(authenticationInfo.ClientName); player?.PlayerView.MessengerView.ChatRoomCreated(authenticationInfo, creatorName, true); }