mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-12-05 08:09:03 +00:00
Update Socket
This commit is contained in:
parent
b71bd4e9fa
commit
edf5fac5ee
@ -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
|
||||||
|
|||||||
@ -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)
|
||||||
{
|
{
|
||||||
// 接收客户端消息
|
// 接收客户端消息
|
||||||
|
try
|
||||||
|
{
|
||||||
byte[] buffer = new byte[2048];
|
byte[] buffer = new byte[2048];
|
||||||
int length = socket.Receive(buffer);
|
int length = socket.Receive(buffer);
|
||||||
if (length > 0)
|
if (length > 0)
|
||||||
{
|
{
|
||||||
string type = Encoding.GetEncoding("unicode").GetString(buffer, 0, length);
|
string msg = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetString(buffer, 0, length);
|
||||||
Console.Write("收到来自:客户端(" + type + ") -> ");
|
int type = GetType(msg);
|
||||||
|
msg = GetMessage(msg);
|
||||||
|
Console.Write("收到来自:客户端(" + type + ") -> " + msg);
|
||||||
buffer = new byte[2048];
|
buffer = new byte[2048];
|
||||||
length = socket.Receive(buffer);
|
length = socket.Receive(buffer);
|
||||||
if (length > 0)
|
switch (type)
|
||||||
{
|
{
|
||||||
string msg = Encoding.GetEncoding("unicode").GetString(buffer, 0, length);
|
case (int)SocketEnums.ReadType.Login:
|
||||||
Console.WriteLine(msg);
|
break;
|
||||||
int getType = Convert.ToInt32(type);
|
case (int)SocketEnums.ReadType.CheckLogin:
|
||||||
if (getType == (int)SocketEnums.ReadType.HeartBeat) // 检测心跳包
|
break;
|
||||||
Send(socket, getType, msg);
|
case (int)SocketEnums.ReadType.Logout:
|
||||||
|
break;
|
||||||
|
case (int)SocketEnums.ReadType.HeartBeat:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (Send(socket, type, msg))
|
||||||
return true;
|
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)
|
||||||
{
|
{
|
||||||
// 发送消息给客户端
|
// 发送消息给客户端
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string send = MakeMessage(type, msg);
|
||||||
byte[] buffer = new byte[2048];
|
byte[] buffer = new byte[2048];
|
||||||
buffer = Encoding.GetEncoding("unicode").GetBytes(Convert.ToString(type));
|
buffer = Encoding.GetEncoding(Config.DEFAULT_ENCODING).GetBytes(Convert.ToString(send));
|
||||||
if (socket.Send(buffer) > 0)
|
if (socket.Send(buffer) > 0)
|
||||||
{
|
{
|
||||||
Console.Write("发送给:客户端(" + type + ") <- ");
|
Console.WriteLine("发送给:客户端(" + type + ") <- " + msg);
|
||||||
buffer = new byte[2048];
|
|
||||||
buffer = Encoding.GetEncoding("unicode").GetBytes(msg);
|
|
||||||
if (socket.Send(buffer) > 0)
|
|
||||||
{
|
|
||||||
Console.WriteLine("发送给:客户端(" + 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user