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