mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-12-05 08:09:02 +00:00
Remove private set;
This commit is contained in:
parent
c97f08ff0e
commit
a87414fdf0
@ -19,7 +19,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
public string ServerName { get; } = "";
|
public string ServerName { get; } = "";
|
||||||
public string ServerNotice { get; } = "";
|
public string ServerNotice { get; } = "";
|
||||||
public string ClientIP { get; } = "";
|
public string ClientIP { get; } = "";
|
||||||
public string ClientName { get; private set; } = "";
|
public string ClientName
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _ClientName;
|
||||||
|
}
|
||||||
|
}
|
||||||
public bool Connected
|
public bool Connected
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -27,16 +33,25 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
return Instance != null && Instance.Connected;
|
return Instance != null && Instance.Connected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public bool Receiving { get; private set; } = false;
|
public bool Receiving
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _Receiving;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Task? ReceivingTask;
|
private Task? ReceivingTask;
|
||||||
|
|
||||||
|
private bool _Receiving;
|
||||||
|
private string _ClientName;
|
||||||
|
|
||||||
public ClientSocket(System.Net.Sockets.Socket Instance, int ServerPort, string ClientIP, string ClientName)
|
public ClientSocket(System.Net.Sockets.Socket Instance, int ServerPort, string ClientIP, string ClientName)
|
||||||
{
|
{
|
||||||
this.Instance= Instance;
|
this.Instance= Instance;
|
||||||
this.ServerPort = ServerPort;
|
this.ServerPort = ServerPort;
|
||||||
this.ClientIP = ClientIP;
|
this.ClientIP = ClientIP;
|
||||||
this.ClientName = ClientName;
|
this._ClientName = ClientName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
@ -67,13 +82,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
|
|
||||||
public void StartReceiving(Task t)
|
public void StartReceiving(Task t)
|
||||||
{
|
{
|
||||||
Receiving = true;
|
_Receiving = true;
|
||||||
ReceivingTask = t;
|
ReceivingTask = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StopReceiving()
|
public void StopReceiving()
|
||||||
{
|
{
|
||||||
Receiving = false;
|
_Receiving = false;
|
||||||
ReceivingTask?.Wait(1);
|
ReceivingTask?.Wait(1);
|
||||||
ReceivingTask = null;
|
ReceivingTask = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,9 +26,16 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
return Instance != null && Instance.Connected;
|
return Instance != null && Instance.Connected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public bool Receiving { get; private set; } = false;
|
public bool Receiving
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _Receiving;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private readonly ThreadManager PlayerThreads;
|
private readonly ThreadManager PlayerThreads;
|
||||||
|
private bool _Receiving = false;
|
||||||
|
|
||||||
private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0)
|
private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -22,7 +22,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
public int ServerPort { get; } = 0;
|
public int ServerPort { get; } = 0;
|
||||||
public string ServerName { get; } = "";
|
public string ServerName { get; } = "";
|
||||||
public string ServerNotice { get; } = "";
|
public string ServerNotice { get; } = "";
|
||||||
public int HeartBeatFaileds { get; private set; } = 0;
|
public int HeartBeatFaileds
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _HeartBeatFaileds;
|
||||||
|
}
|
||||||
|
}
|
||||||
public bool Connected
|
public bool Connected
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -30,13 +36,29 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
return Instance != null && Instance.Connected;
|
return Instance != null && Instance.Connected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public bool Receiving { get; private set; } = false;
|
public bool Receiving
|
||||||
public bool SendingHeartBeat { get; private set; } = false;
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _Receiving;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool SendingHeartBeat
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _SendingHeartBeat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Task? SendingHeartBeatTask;
|
private Task? SendingHeartBeatTask;
|
||||||
private Task? ReceivingTask;
|
private Task? ReceivingTask;
|
||||||
private Task? WaitHeartBeatReply;
|
private Task? WaitHeartBeatReply;
|
||||||
|
|
||||||
|
private bool _Receiving = false;
|
||||||
|
private bool _SendingHeartBeat = false;
|
||||||
|
private int _HeartBeatFaileds = 0;
|
||||||
|
|
||||||
private Socket(System.Net.Sockets.Socket Instance, string ServerIP, int ServerPort)
|
private Socket(System.Net.Sockets.Socket Instance, string ServerIP, int ServerPort)
|
||||||
{
|
{
|
||||||
this.Instance = Instance;
|
this.Instance = Instance;
|
||||||
@ -72,7 +94,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
if ((SocketMessageType)result[0] == SocketMessageType.HeartBeat)
|
if ((SocketMessageType)result[0] == SocketMessageType.HeartBeat)
|
||||||
{
|
{
|
||||||
if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1);
|
if (WaitHeartBeatReply != null && !WaitHeartBeatReply.IsCompleted) WaitHeartBeatReply.Wait(1);
|
||||||
HeartBeatFaileds = 0;
|
_HeartBeatFaileds = 0;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -91,31 +113,31 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
|
|
||||||
public void ResetHeartBeatFaileds()
|
public void ResetHeartBeatFaileds()
|
||||||
{
|
{
|
||||||
HeartBeatFaileds = 0;
|
_HeartBeatFaileds = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartReceiving(Task t)
|
public void StartReceiving(Task t)
|
||||||
{
|
{
|
||||||
Receiving = true;
|
_Receiving = true;
|
||||||
ReceivingTask = t;
|
ReceivingTask = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartSendingHeartBeat()
|
private void StartSendingHeartBeat()
|
||||||
{
|
{
|
||||||
SendingHeartBeat = true;
|
_SendingHeartBeat = true;
|
||||||
SendingHeartBeatTask = Task.Factory.StartNew(SendHeartBeat);
|
SendingHeartBeatTask = Task.Factory.StartNew(SendHeartBeat);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StopReceiving()
|
private void StopReceiving()
|
||||||
{
|
{
|
||||||
Receiving = false;
|
_Receiving = false;
|
||||||
ReceivingTask?.Wait(1);
|
ReceivingTask?.Wait(1);
|
||||||
ReceivingTask = null;
|
ReceivingTask = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StopSendingHeartBeat()
|
private void StopSendingHeartBeat()
|
||||||
{
|
{
|
||||||
SendingHeartBeat = false;
|
_SendingHeartBeat = false;
|
||||||
SendingHeartBeatTask?.Wait(1);
|
SendingHeartBeatTask?.Wait(1);
|
||||||
SendingHeartBeatTask = null;
|
SendingHeartBeatTask = null;
|
||||||
}
|
}
|
||||||
@ -125,7 +147,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
Thread.Sleep(100);
|
Thread.Sleep(100);
|
||||||
while (Connected)
|
while (Connected)
|
||||||
{
|
{
|
||||||
if (!SendingHeartBeat) SendingHeartBeat= true;
|
if (!SendingHeartBeat) _SendingHeartBeat= true;
|
||||||
// 发送心跳包
|
// 发送心跳包
|
||||||
if (Send(SocketMessageType.HeartBeat) == SocketResult.Success)
|
if (Send(SocketMessageType.HeartBeat) == SocketResult.Success)
|
||||||
{
|
{
|
||||||
@ -138,13 +160,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
else AddHeartBeatFaileds();
|
else AddHeartBeatFaileds();
|
||||||
Thread.Sleep(20000);
|
Thread.Sleep(20000);
|
||||||
}
|
}
|
||||||
SendingHeartBeat = false;
|
_SendingHeartBeat = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddHeartBeatFaileds()
|
private void AddHeartBeatFaileds()
|
||||||
{
|
{
|
||||||
// 超过三次没回应心跳,服务器连接失败。
|
// 超过三次没回应心跳,服务器连接失败。
|
||||||
if (HeartBeatFaileds++ >= 3)
|
if (_HeartBeatFaileds++ >= 3)
|
||||||
throw new System.Exception("ERROR:与服务器连接中断。");
|
throw new System.Exception("ERROR:与服务器连接中断。");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,12 +17,27 @@ namespace Milimoe.FunGame.Core.Service
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 客户端专用Socket
|
/// 客户端专用Socket
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static Socket? Socket { get; private set; } = null;
|
internal static Socket? Socket
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _Socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 服务器端专用Socket
|
/// 服务器端专用Socket
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static Socket? ServerSocket { get; private set; } = null;
|
internal static Socket? ServerSocket
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _ServerSocket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Socket? _Socket = null;
|
||||||
|
private static Socket? _ServerSocket = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建服务器监听Socket
|
/// 创建服务器监听Socket
|
||||||
@ -35,11 +50,11 @@ namespace Milimoe.FunGame.Core.Service
|
|||||||
if (MaxConnection <= 0) MaxConnection = SocketSet.MaxConnection_General;
|
if (MaxConnection <= 0) MaxConnection = SocketSet.MaxConnection_General;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ServerSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
_ServerSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
IPEndPoint ServerEndPoint = new(IPAddress.Any, Port);
|
IPEndPoint ServerEndPoint = new(IPAddress.Any, Port);
|
||||||
ServerSocket.Bind(ServerEndPoint);
|
_ServerSocket.Bind(ServerEndPoint);
|
||||||
ServerSocket.Listen(MaxConnection);
|
_ServerSocket.Listen(MaxConnection);
|
||||||
return ServerSocket;
|
return _ServerSocket;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@ -94,8 +109,8 @@ namespace Milimoe.FunGame.Core.Service
|
|||||||
ClientSocket.Connect(ServerEndPoint);
|
ClientSocket.Connect(ServerEndPoint);
|
||||||
if (ClientSocket.Connected)
|
if (ClientSocket.Connected)
|
||||||
{
|
{
|
||||||
Socket = ClientSocket;
|
_Socket = ClientSocket;
|
||||||
return Socket;
|
return _Socket;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,7 +118,7 @@ namespace Milimoe.FunGame.Core.Service
|
|||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
Socket?.Close();
|
_Socket?.Close();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,10 +17,17 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
{
|
{
|
||||||
public class MainModel
|
public class MainModel
|
||||||
{
|
{
|
||||||
public Core.Library.Common.Network.Socket? Socket { get; private set; }
|
public Core.Library.Common.Network.Socket? Socket
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _Socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
public Main Main { get; }
|
public Main Main { get; }
|
||||||
|
|
||||||
private Task? ReceivingTask;
|
private Task? ReceivingTask;
|
||||||
|
private Core.Library.Common.Network.Socket? _Socket;
|
||||||
|
|
||||||
public MainModel(Main main)
|
public MainModel(Main main)
|
||||||
{
|
{
|
||||||
@ -125,7 +132,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
// 与服务器建立连接
|
// 与服务器建立连接
|
||||||
Socket?.Close();
|
Socket?.Close();
|
||||||
Others.Config.FunGame_isRetrying = true;
|
Others.Config.FunGame_isRetrying = true;
|
||||||
Socket = Core.Library.Common.Network.Socket.Connect(Others.Constant.SERVER_IPADRESS, Others.Constant.SERVER_PORT);
|
_Socket = Core.Library.Common.Network.Socket.Connect(Others.Constant.SERVER_IPADRESS, Others.Constant.SERVER_PORT);
|
||||||
if (Socket != null && Socket.Connected)
|
if (Socket != null && Socket.Connected)
|
||||||
{
|
{
|
||||||
// 发送连接请求
|
// 发送连接请求
|
||||||
@ -178,7 +185,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
if (Socket != null)
|
if (Socket != null)
|
||||||
{
|
{
|
||||||
Socket.Close();
|
Socket.Close();
|
||||||
Socket = null;
|
_Socket = null;
|
||||||
}
|
}
|
||||||
if (ReceivingTask != null && !ReceivingTask.IsCompleted)
|
if (ReceivingTask != null && !ReceivingTask.IsCompleted)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user