完成Socket封装

This commit is contained in:
Mili 2022-08-27 00:36:12 +08:00
parent edf5fac5ee
commit 3cfe9ceda2
5 changed files with 63 additions and 38 deletions

View File

@ -6,6 +6,7 @@ using System;
using FunGameServer.Sockets;
using System.Net.WebSockets;
using FunGameServer.Models.Config;
using FunGameServer.Utils;
bool Running = true;
Socket? ServerSocket = null;
@ -96,7 +97,7 @@ bool Read(Socket socket)
int length = socket.Receive(buffer);
if (length > 0)
{
string msg = Encoding.GetEncoding("unicode").GetString(buffer, 0, length);
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
Console.WriteLine("收到来自客户端玩家ID -> " + msg);
return true;
}
@ -110,7 +111,7 @@ bool Send(Socket socket)
// 发送消息给客户端
string msg = ">> 已连接至服务器 -> [ " + host + " ] 连接成功";
byte[] buffer = new byte[2048];
buffer = Encoding.GetEncoding("unicode").GetBytes(msg);
buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketEnums.SendType.CheckLogin, msg));
if (socket.Send(buffer) > 0)
{
Console.WriteLine("发送给:客户端 <- " + msg);

View File

@ -16,7 +16,7 @@ namespace FunGameServer.Models.Config
public static int CONNECTING_PLAYERS = 0; // 正在连接的玩家数量
public static string SERVER_IPADRESS = "127.0.0.1"; // 默认IP地址
public static int SERVER_PORT = 22222; // 默认端口
public static string DEFAULT_ENCODING = "unicode"; // 默认传输字符集
public static Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集
public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数
/// <summary>

View File

@ -10,18 +10,26 @@ namespace FunGameServer.Models.Config
{
public enum SendType
{
Login = 1,
CheckLogin = 2,
Logout = 3,
HeartBeat = 4,
GetNotice = 1,
Login = 2,
CheckLogin = 3,
Logout = 4,
HeartBeat = 5
}
public enum ReadType
{
Login = 1,
CheckLogin = 2,
Logout = 3,
HeartBeat = 4,
GetNotice = 1,
Login = 2,
CheckLogin = 3,
Logout = 4,
HeartBeat = 5
}
public static string SENDTYPE_GetNotice = "GetNotice";
public static string SENDTYPE_Login = "Login";
public static string SENDTYPE_CheckLogin = "CheckLogin";
public static string SENDTYPE_Logout = "Logout";
public static string SENDTYPE_HeartBeat = "HeartBeat";
}
}

View File

@ -7,6 +7,7 @@ using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlTypes;
using FunGameServer.Utils;
namespace FunGameServer.Sockets
{
@ -32,14 +33,14 @@ namespace FunGameServer.Sockets
int length = socket.Receive(buffer);
if (length > 0)
{
string msg = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetString(buffer, 0, length);
int type = GetType(msg);
msg = GetMessage(msg);
Console.Write("收到来自:客户端(" + type + " -> " + msg);
buffer = new byte[2048];
length = socket.Receive(buffer);
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
int type = SocketHelper.GetType(msg);
msg = SocketHelper.GetMessage(msg);
Console.WriteLine("收到来自:客户端(" + type + " -> " + msg);
switch (type)
{
case (int)SocketEnums.ReadType.GetNotice:
break;
case (int)SocketEnums.ReadType.Login:
break;
case (int)SocketEnums.ReadType.CheckLogin:
@ -49,9 +50,7 @@ namespace FunGameServer.Sockets
case (int)SocketEnums.ReadType.HeartBeat:
break;
}
if (Send(socket, type, msg))
return true;
throw new Exception();
return Send(socket, type, msg);
}
throw new Exception();
}
@ -67,9 +66,8 @@ namespace FunGameServer.Sockets
// 发送消息给客户端
try
{
string send = MakeMessage(type, msg);
byte[] buffer = new byte[2048];
buffer = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetBytes(Convert.ToString(send));
buffer = Config.DEFAULT_ENCODING.GetBytes(Convert.ToString(SocketHelper.MakeMessage(type, msg)));
if (socket.Send(buffer) > 0)
{
Console.WriteLine("发送给:客户端(" + type + " <- " + msg);
@ -84,21 +82,6 @@ namespace FunGameServer.Sockets
}
}
private int GetType(string msg)
{
return Convert.ToInt32(msg[..(msg.IndexOf(';') - 1)]);
}
private string GetMessage(string msg)
{
return msg.Substring(msg.IndexOf(';') + 1, msg.Length - 1);
}
private string MakeMessage(int type, string msg)
{
return type + ";" + msg;
}
public void Start()
{
Task StreamReader = Task.Factory.StartNew(() =>
@ -114,17 +97,19 @@ namespace FunGameServer.Sockets
while (Running)
{
if (Socket != null)
{
if (!Read(Socket))
{
FailedTimes++;
if (FailedTimes >= Config.MAX_CONNECTFAILED)
{
Console.WriteLine("ERROR: Too Many Faileds.");
Console.WriteLine("DONE: StringStream is Closed.");
Console.WriteLine("DONE: StreamReader is Closed.");
break;
}
}
else if (FailedTimes - 1 >= 0) FailedTimes--;
}
else
{
Console.WriteLine("ERROR: Socket is Closed.");

31
Utils/SocketHelper.cs Normal file
View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FunGameServer.Utils
{
public class SocketHelper
{
public static int GetType(string msg)
{
int index = msg.IndexOf(';') - 1;
if (index > 0)
return Convert.ToInt32(msg[..index]);
else
return Convert.ToInt32(msg[..1]);
}
public static string GetMessage(string msg)
{
int index = msg.IndexOf(';') + 1;
return msg[index..];
}
public static string MakeMessage(int type, string msg)
{
return type + ";" + msg;
}
}
}