user enter, leave
This commit is contained in:
@@ -13,7 +13,8 @@ namespace Core
|
||||
{
|
||||
private Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
private ConcurrentDictionary<string, Room> Rooms { get; } = new ConcurrentDictionary<string, Room>();
|
||||
public ConcurrentDictionary<string, Room> Rooms { get; } = new ConcurrentDictionary<string, Room>();
|
||||
public ConcurrentDictionary<string, Socket> Clients { get; } = new ConcurrentDictionary<string, Socket>();
|
||||
|
||||
public PServer(string ip, int port, int backlog)
|
||||
{
|
||||
@@ -74,6 +75,8 @@ namespace Core
|
||||
if (packetType == PacketType.LoginRequest)
|
||||
{
|
||||
LoginRequestPacket requestPacket = new LoginRequestPacket(dataBuffer);
|
||||
|
||||
Clients.TryAdd(requestPacket.Id, clientSocket);
|
||||
Console.WriteLine($"[{DateTime.Now}] LoginRequest - Id: {requestPacket.Id}, Nickname: {requestPacket.NickName}");
|
||||
|
||||
id = requestPacket.Id;
|
||||
@@ -119,6 +122,24 @@ namespace Core
|
||||
|
||||
EnterRoomResponsePacket responsePacket = new EnterRoomResponsePacket(StatusCode.Success);
|
||||
await clientSocket.SendAsync(responsePacket.Serialize(), SocketFlags.None);
|
||||
|
||||
await Task.Delay(100);
|
||||
foreach (var user in room.Users)
|
||||
{
|
||||
if (user.Value == nickname)
|
||||
continue;
|
||||
|
||||
// add me to other user
|
||||
if (Clients.TryGetValue(user.Key, out var otherClient))
|
||||
{
|
||||
UserEnterPacket enterPacket = new UserEnterPacket(nickname);
|
||||
await otherClient.SendAsync(enterPacket.Serialize(), SocketFlags.None);
|
||||
}
|
||||
|
||||
// add other user to me
|
||||
UserEnterPacket packet = new UserEnterPacket(user.Value);
|
||||
await clientSocket.SendAsync(packet.Serialize(), SocketFlags.None);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -129,6 +150,25 @@ namespace Core
|
||||
|
||||
}
|
||||
}
|
||||
else if (packetType == PacketType.UserLeave)
|
||||
{
|
||||
UserLeavePacket packet = new UserLeavePacket(dataBuffer);
|
||||
if (Rooms.TryGetValue(roomName, out var room))
|
||||
{
|
||||
room.Users.TryRemove(id, out _);
|
||||
|
||||
if (room.Users.IsEmpty)
|
||||
Rooms.TryRemove(roomName, out _);
|
||||
|
||||
roomName = "";
|
||||
|
||||
foreach (var user in room.Users)
|
||||
{
|
||||
if (Clients.TryGetValue(user.Key, out var otherClient))
|
||||
await otherClient.SendAsync(packet.Serialize(), SocketFlags.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
58
Chat/Core/UserEnterPacket.cs
Normal file
58
Chat/Core/UserEnterPacket.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core
|
||||
{
|
||||
public class UserEnterPacket : IPacket
|
||||
{
|
||||
public string Nickname { get; private set; }
|
||||
|
||||
public UserEnterPacket(string nickname)
|
||||
{
|
||||
Nickname = nickname;
|
||||
}
|
||||
|
||||
public UserEnterPacket(byte[] buffer)
|
||||
{
|
||||
int cursor = 2;
|
||||
|
||||
short nicknameSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor));
|
||||
cursor += sizeof(short);
|
||||
|
||||
Nickname = Encoding.UTF8.GetString(buffer, cursor, nicknameSize);
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
// 2bytes header
|
||||
// data: 2bytes packetType + 2bytes id size + id + 2bytes nickname size + nickname
|
||||
|
||||
byte[] packetType = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)PacketType.UserEnter));
|
||||
byte[] roomName = Encoding.UTF8.GetBytes(Nickname);
|
||||
byte[] roomNameSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)roomName.Length));
|
||||
|
||||
short dataSize = (short)(packetType.Length + roomName.Length + roomNameSize.Length);
|
||||
byte[] header = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataSize));
|
||||
|
||||
byte[] buffer = new byte[2 + dataSize];
|
||||
|
||||
int cursor = 0;
|
||||
Array.Copy(header, 0, buffer, cursor, header.Length);
|
||||
cursor += header.Length;
|
||||
|
||||
Array.Copy(packetType, 0, buffer, cursor, packetType.Length);
|
||||
cursor += packetType.Length;
|
||||
|
||||
Array.Copy(roomNameSize, 0, buffer, cursor, roomNameSize.Length);
|
||||
cursor += roomNameSize.Length;
|
||||
|
||||
Array.Copy(roomName, 0, buffer, cursor, roomName.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
58
Chat/Core/UserLeavePacket.cs
Normal file
58
Chat/Core/UserLeavePacket.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core
|
||||
{
|
||||
public class UserLeavePacket : IPacket
|
||||
{
|
||||
public string Nickname { get; private set; }
|
||||
|
||||
public UserLeavePacket(string nickname)
|
||||
{
|
||||
Nickname = nickname;
|
||||
}
|
||||
|
||||
public UserLeavePacket(byte[] buffer)
|
||||
{
|
||||
int cursor = 2;
|
||||
|
||||
short nicknameSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor));
|
||||
cursor += sizeof(short);
|
||||
|
||||
Nickname = Encoding.UTF8.GetString(buffer, cursor, nicknameSize);
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
// 2bytes header
|
||||
// data: 2bytes packetType + 2bytes id size + id + 2bytes nickname size + nickname
|
||||
|
||||
byte[] packetType = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)PacketType.UserLeave));
|
||||
byte[] roomName = Encoding.UTF8.GetBytes(Nickname);
|
||||
byte[] roomNameSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)roomName.Length));
|
||||
|
||||
short dataSize = (short)(packetType.Length + roomName.Length + roomNameSize.Length);
|
||||
byte[] header = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataSize));
|
||||
|
||||
byte[] buffer = new byte[2 + dataSize];
|
||||
|
||||
int cursor = 0;
|
||||
Array.Copy(header, 0, buffer, cursor, header.Length);
|
||||
cursor += header.Length;
|
||||
|
||||
Array.Copy(packetType, 0, buffer, cursor, packetType.Length);
|
||||
cursor += packetType.Length;
|
||||
|
||||
Array.Copy(roomNameSize, 0, buffer, cursor, roomNameSize.Length);
|
||||
cursor += roomNameSize.Length;
|
||||
|
||||
Array.Copy(roomName, 0, buffer, cursor, roomName.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user