Update Socket

This commit is contained in:
Mili 2022-08-26 00:22:56 +08:00
parent b71bd4e9fa
commit edf5fac5ee
2 changed files with 75 additions and 82 deletions

View File

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

View File

@ -6,6 +6,7 @@ using System.IO;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Data.SqlTypes;
namespace FunGameServer.Sockets namespace FunGameServer.Sockets
{ {
@ -20,87 +21,110 @@ namespace FunGameServer.Sockets
Running = running; Running = running;
} }
bool Read(Socket socket) private int FailedTimes = 0; // 超过一定次数断开连接
private bool Read(Socket socket)
{ {
// 接收客户端消息 // 接收客户端消息
byte[] buffer = new byte[2048]; try
int length = socket.Receive(buffer);
if (length > 0)
{ {
string type = Encoding.GetEncoding("unicode").GetString(buffer, 0, length); byte[] buffer = new byte[2048];
Console.Write("收到来自:客户端(" + type + " -> "); int length = socket.Receive(buffer);
buffer = new byte[2048];
length = socket.Receive(buffer);
if (length > 0) if (length > 0)
{ {
string msg = Encoding.GetEncoding("unicode").GetString(buffer, 0, length); string msg = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetString(buffer, 0, length);
Console.WriteLine(msg); int type = GetType(msg);
int getType = Convert.ToInt32(type); msg = GetMessage(msg);
if (getType == (int)SocketEnums.ReadType.HeartBeat) // 检测心跳包 Console.Write("收到来自:客户端(" + type + " -> " + msg);
Send(socket, getType, msg); buffer = new byte[2048];
return true; length = socket.Receive(buffer);
switch (type)
{
case (int)SocketEnums.ReadType.Login:
break;
case (int)SocketEnums.ReadType.CheckLogin:
break;
case (int)SocketEnums.ReadType.Logout:
break;
case (int)SocketEnums.ReadType.HeartBeat:
break;
}
if (Send(socket, type, msg))
return true;
throw new Exception();
} }
else throw new Exception();
Console.WriteLine("客户端没有回应。"); }
catch (Exception e)
{
Console.WriteLine("ERROR客户端没有回应。\n" + e.StackTrace);
return false; return false;
} }
else
Console.WriteLine("客户端没有回应。");
return false;
} }
bool Send(Socket socket, int type, string msg) private bool Send(Socket socket, int type, string msg, object[]? objs = null)
{ {
// 发送消息给客户端 // 发送消息给客户端
byte[] buffer = new byte[2048]; try
buffer = Encoding.GetEncoding("unicode").GetBytes(Convert.ToString(type));
if (socket.Send(buffer) > 0)
{ {
Console.Write("发送给:客户端(" + type + " <- "); string send = MakeMessage(type, msg);
buffer = new byte[2048]; byte[] buffer = new byte[2048];
buffer = Encoding.GetEncoding("unicode").GetBytes(msg); buffer = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetBytes(Convert.ToString(send));
if (socket.Send(buffer) > 0) if (socket.Send(buffer) > 0)
{ {
Console.WriteLine("发送给:客户端(" + msg + " <- "); Console.WriteLine("发送给:客户端(" + type + " <- " + msg);
return true; return true;
} }
else throw new Exception();
Console.WriteLine("无法传输数据,与客户端的连接可能丢失。"); }
catch (Exception e)
{
Console.WriteLine("ERROR客户端没有回应。" + e.StackTrace);
return false; return false;
} }
else }
Console.WriteLine("无法传输数据,与客户端的连接可能丢失。");
return false; 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() public void Start()
{ {
Task StringStream = Task.Factory.StartNew(() => Task StreamReader = Task.Factory.StartNew(() =>
{ {
CreateStringStream(); CreateStreamReader();
});
Task IntStream = Task.Factory.StartNew(() =>
{
CreateStringStream();
});
Task DecimalStream = Task.Factory.StartNew(() =>
{
CreateDecimalStream();
});
Task ObjectStream = Task.Factory.StartNew(() =>
{
CreateObjectStream();
}); });
} }
private void CreateStringStream() private void CreateStreamReader()
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
Console.WriteLine("Creating: StringStream...OK"); Console.WriteLine("Creating: StreamReader...OK");
while (Running) while (Running)
{ {
if (Socket != null) if (Socket != null)
Read(Socket); if (!Read(Socket))
{
FailedTimes++;
if (FailedTimes >= Config.MAX_CONNECTFAILED)
{
Console.WriteLine("ERROR: Too Many Faileds.");
Console.WriteLine("DONE: StringStream is Closed.");
break;
}
}
else if (FailedTimes - 1 >= 0) FailedTimes--;
else else
{ {
Console.WriteLine("ERROR: Socket is Closed."); Console.WriteLine("ERROR: Socket is Closed.");
@ -109,38 +133,5 @@ namespace FunGameServer.Sockets
} }
} }
} }
private void CreateIntStream()
{
Thread.Sleep(1000);
Console.WriteLine("Creating: IntStream...OK");
while (Running)
{
Console.WriteLine("DONE: IntStream is Closed.");
break;
}
}
private void CreateDecimalStream()
{
Thread.Sleep(1000);
Console.WriteLine("Creating: DecimalStream...OK");
while (Running)
{
Console.WriteLine("DONE: DecimalStream is Closed.");
break;
}
}
private void CreateObjectStream()
{
Thread.Sleep(1000);
Console.WriteLine("Creating: ObjectStream...OK");
while (Running)
{
Console.WriteLine("DONE: ObjectStream is Closed.");
break;
}
}
} }
} }