login
This commit is contained in:
@@ -8,4 +8,8 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
2
Chat/Client/LoginForm.Designer.cs
generated
2
Chat/Client/LoginForm.Designer.cs
generated
@@ -49,7 +49,7 @@
|
||||
this.tbxId.Location = new System.Drawing.Point(79, 12);
|
||||
this.tbxId.Name = "tbxId";
|
||||
this.tbxId.Size = new System.Drawing.Size(181, 23);
|
||||
this.tbxId.TabIndex = 1;
|
||||
this.tbxId.TabIndex = 0;
|
||||
//
|
||||
// label2
|
||||
//
|
||||
|
@@ -1,3 +1,6 @@
|
||||
using Core;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
public partial class LoginForm : Form
|
||||
@@ -5,21 +8,55 @@ namespace Client
|
||||
public LoginForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitInstance();
|
||||
}
|
||||
|
||||
private void btnLogin_Click(object sender, EventArgs e)
|
||||
private void InitInstance()
|
||||
{
|
||||
if (string.IsNullOrEmpty(tbxId.Text) || string.IsNullOrEmpty(tbxNickname.Text))
|
||||
Singleton.Instance.LoginResponsed += LoginResponsed;
|
||||
FormClosing += LoginForm_FormClosing;
|
||||
}
|
||||
|
||||
private void LoginForm_FormClosing(object? sender, FormClosingEventArgs e)
|
||||
{
|
||||
Singleton.Instance.LoginResponsed -= LoginResponsed;
|
||||
}
|
||||
|
||||
private async void btnLogin_Click(object sender, EventArgs e)
|
||||
{
|
||||
string id = tbxId.Text;
|
||||
string nickname = tbxNickname.Text;
|
||||
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(nickname))
|
||||
{
|
||||
MessageBox.Show("Please input ID and Nickname.");
|
||||
return;
|
||||
}
|
||||
|
||||
Singleton.Instance.Id = tbxId.Text;
|
||||
Singleton.Instance.Nickname = tbxNickname.Text;
|
||||
await Singleton.Instance.ConnectAsync("127.0.0.1", 20000);
|
||||
|
||||
RoomListForm roomListForm = new RoomListForm();
|
||||
roomListForm.ShowDialog();
|
||||
Singleton.Instance.Id = id;
|
||||
Singleton.Instance.Nickname = nickname;
|
||||
|
||||
LoginRequestPacket packet = new LoginRequestPacket(id, nickname);
|
||||
await Singleton.Instance.SendAsync(packet.Serialize(), SocketFlags.None);
|
||||
}
|
||||
|
||||
private void LoginResponsed(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender == null)
|
||||
return;
|
||||
|
||||
LoginResponsePacket packet = (LoginResponsePacket)sender;
|
||||
MessageBox.Show(packet.Code.ToString());
|
||||
if (packet.Code != StatusCode.Success)
|
||||
{
|
||||
RoomListForm roomListForm = new RoomListForm();
|
||||
roomListForm.ShowDialog();
|
||||
}
|
||||
else
|
||||
{
|
||||
Singleton.Instance.Socket.Shutdown(SocketShutdown.Send);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -12,5 +15,62 @@ namespace Client
|
||||
|
||||
public string Id { get; set; } = "";
|
||||
public string Nickname { get; set; } = "";
|
||||
public Socket Socket { get; } = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
public event EventHandler? LoginResponsed;
|
||||
|
||||
public async Task ConnectAsync(string ip, int port)
|
||||
{
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
||||
await this.Socket.ConnectAsync(endPoint);
|
||||
ThreadPool.QueueUserWorkItem(ReceiveAsync, this.Socket);
|
||||
}
|
||||
|
||||
public async Task<int> SendAsync(byte[] buffer, SocketFlags socketFlags)
|
||||
{
|
||||
return await this.Socket.SendAsync(buffer, socketFlags);
|
||||
}
|
||||
|
||||
private async void ReceiveAsync(object? sender)
|
||||
{
|
||||
if (sender == null)
|
||||
return;
|
||||
|
||||
Socket socket = (Socket)sender;
|
||||
byte[] headerBuffer = new byte[2];
|
||||
while (true)
|
||||
{
|
||||
// header buffer
|
||||
int n1 = await socket.ReceiveAsync(headerBuffer, SocketFlags.None);
|
||||
if (n1 < 1)
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now}] Disconnect server: {socket.RemoteEndPoint}");
|
||||
socket.Dispose();
|
||||
return;
|
||||
}
|
||||
else if (n1 == 1)
|
||||
{
|
||||
await socket.ReceiveAsync(new ArraySegment<byte>(headerBuffer, 1, 1), SocketFlags.None);
|
||||
}
|
||||
|
||||
// data buffer
|
||||
short dataSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(headerBuffer));
|
||||
byte[] dataBuffer = new byte[dataSize];
|
||||
|
||||
int totalRecv = 0;
|
||||
while (totalRecv < dataSize)
|
||||
{
|
||||
int n2 = await socket.ReceiveAsync(new ArraySegment<byte>(dataBuffer, totalRecv, dataSize - totalRecv), SocketFlags.None);
|
||||
totalRecv += n2;
|
||||
}
|
||||
|
||||
PacketType packetType = (PacketType)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer));
|
||||
if (packetType == PacketType.LoginResponse)
|
||||
{
|
||||
LoginResponsePacket packet = new LoginResponsePacket(dataBuffer);
|
||||
LoginResponsed?.Invoke(packet, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user