mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-12-05 08:09:03 +00:00
实现连接指定IP和主动断开连接
This commit is contained in:
parent
2e7a4834e8
commit
35f7c18a17
@ -27,6 +27,8 @@ while (Running)
|
|||||||
switch (order)
|
switch (order)
|
||||||
{
|
{
|
||||||
case OrderDictionary.Quit:
|
case OrderDictionary.Quit:
|
||||||
|
case OrderDictionary.Exit:
|
||||||
|
case OrderDictionary.Close:
|
||||||
Running = false;
|
Running = false;
|
||||||
break;
|
break;
|
||||||
case OrderDictionary.Help:
|
case OrderDictionary.Help:
|
||||||
|
|||||||
@ -10,6 +10,8 @@ namespace FunGameServer.Models.Config
|
|||||||
{
|
{
|
||||||
public const string Help = "help";
|
public const string Help = "help";
|
||||||
public const string Quit = "quit";
|
public const string Quit = "quit";
|
||||||
|
public const string Exit = "exit";
|
||||||
|
public const string Close = "close";
|
||||||
public const string Restart = "restart";
|
public const string Restart = "restart";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,35 +55,20 @@ namespace FunGameServer.Sockets
|
|||||||
break;
|
break;
|
||||||
case (int)SocketMessageType.CheckLogin:
|
case (int)SocketMessageType.CheckLogin:
|
||||||
// 添加至玩家列表
|
// 添加至玩家列表
|
||||||
if (!Config.OnlinePlayers.ContainsKey(msg))
|
User = new User(msg);
|
||||||
{
|
|
||||||
if (Task != null)
|
|
||||||
{
|
|
||||||
User = new User(msg);
|
|
||||||
Config.OnlinePlayers.AddOrUpdate(User.Userame, Task, (key, value) => value);
|
|
||||||
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Userame + " 已添加");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// TODO
|
|
||||||
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + msg + " 重复登录!");
|
|
||||||
}
|
|
||||||
msg = " >> 欢迎回来, " + msg + " 。";
|
msg = " >> 欢迎回来, " + msg + " 。";
|
||||||
|
AddUser();
|
||||||
|
ServerHelper.WriteLine("目前在线玩家数量: " + Config.OnlinePlayers.Count);
|
||||||
break;
|
break;
|
||||||
case (int)SocketMessageType.Logout:
|
case (int)SocketMessageType.Logout:
|
||||||
msg = " >> 你已成功退出登录! ";
|
msg = " >> 你已成功退出登录! ";
|
||||||
if (Task != null && User != null)
|
RemoveUser();
|
||||||
{
|
GetUserCount();
|
||||||
if (Config.OnlinePlayers.TryRemove(User.Userame, out Task))
|
break;
|
||||||
{
|
case (int)SocketMessageType.Disconnect:
|
||||||
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Userame + " 已移除");
|
msg = " >> 你已成功断开与服务器的连接: " + Config.SERVER_NAME + "。 ";
|
||||||
Task = null;
|
RemoveUser();
|
||||||
User = null;
|
GetUserCount();
|
||||||
}
|
|
||||||
else
|
|
||||||
ServerHelper.WriteLine("OnlinePlayers: 移除玩家 " + User.Userame + " 失败");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case (int)SocketMessageType.HeartBeat:
|
case (int)SocketMessageType.HeartBeat:
|
||||||
msg = "";
|
msg = "";
|
||||||
@ -131,9 +116,59 @@ namespace FunGameServer.Sockets
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void KickUser()
|
||||||
|
{
|
||||||
|
if (User != null)
|
||||||
|
{
|
||||||
|
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Userame + " 重复登录!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool AddUser()
|
||||||
|
{
|
||||||
|
if (User != null)
|
||||||
|
{
|
||||||
|
if (!Config.OnlinePlayers.ContainsKey(User.Userame))
|
||||||
|
{
|
||||||
|
if (Task != null)
|
||||||
|
{
|
||||||
|
Config.OnlinePlayers.AddOrUpdate(User.Userame, Task, (key, value) => value);
|
||||||
|
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Userame + " 已添加");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
KickUser();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool RemoveUser()
|
||||||
|
{
|
||||||
|
if (Task != null && User != null)
|
||||||
|
{
|
||||||
|
if (Config.OnlinePlayers.TryRemove(User.Userame, out Task))
|
||||||
|
{
|
||||||
|
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Userame + " 已移除");
|
||||||
|
Task = null;
|
||||||
|
User = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else ServerHelper.WriteLine("OnlinePlayers: 移除玩家 " + User.Userame + " 失败");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GetUserCount()
|
||||||
|
{
|
||||||
|
ServerHelper.WriteLine("目前在线玩家数量: " + Config.OnlinePlayers.Count);
|
||||||
|
}
|
||||||
|
|
||||||
private void CreateStreamReader()
|
private void CreateStreamReader()
|
||||||
{
|
{
|
||||||
Thread.Sleep(1000);
|
Thread.Sleep(100);
|
||||||
ServerHelper.WriteLine("Creating: StreamReader...OK");
|
ServerHelper.WriteLine("Creating: StreamReader...OK");
|
||||||
while (Running)
|
while (Running)
|
||||||
{
|
{
|
||||||
@ -144,6 +179,8 @@ namespace FunGameServer.Sockets
|
|||||||
FailedTimes++;
|
FailedTimes++;
|
||||||
if (FailedTimes >= Config.MAX_CONNECTFAILED)
|
if (FailedTimes >= Config.MAX_CONNECTFAILED)
|
||||||
{
|
{
|
||||||
|
RemoveUser();
|
||||||
|
GetUserCount();
|
||||||
ServerHelper.WriteLine("ERROR -> Too Many Faileds.");
|
ServerHelper.WriteLine("ERROR -> Too Many Faileds.");
|
||||||
ServerHelper.WriteLine("CLOSE -> StreamReader is Closed.");
|
ServerHelper.WriteLine("CLOSE -> StreamReader is Closed.");
|
||||||
break;
|
break;
|
||||||
@ -153,6 +190,8 @@ namespace FunGameServer.Sockets
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
RemoveUser();
|
||||||
|
GetUserCount();
|
||||||
ServerHelper.WriteLine("ERROR -> Socket is Closed.");
|
ServerHelper.WriteLine("ERROR -> Socket is Closed.");
|
||||||
ServerHelper.WriteLine("CLOSE -> StringStream is Closed.");
|
ServerHelper.WriteLine("CLOSE -> StringStream is Closed.");
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -70,20 +70,10 @@ namespace FunGameServer.Utils
|
|||||||
{
|
{
|
||||||
Config.OrderList.Clear();
|
Config.OrderList.Clear();
|
||||||
Config.OrderList.Add(OrderDictionary.Help, "Milimoe -> 帮助");
|
Config.OrderList.Add(OrderDictionary.Help, "Milimoe -> 帮助");
|
||||||
Config.OrderList.Add(OrderDictionary.Quit, "Milimoe -> 帮助");
|
Config.OrderList.Add(OrderDictionary.Quit, "关闭服务器");
|
||||||
Config.OrderList.Add(OrderDictionary.Restart, "Milimoe -> 帮助");
|
Config.OrderList.Add(OrderDictionary.Exit, "关闭服务器");
|
||||||
}
|
Config.OrderList.Add(OrderDictionary.Close, "关闭服务器");
|
||||||
|
Config.OrderList.Add(OrderDictionary.Restart, "重启服务器");
|
||||||
public static bool IsIP(string ip)
|
|
||||||
{
|
|
||||||
//判断是否为IP
|
|
||||||
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsEmail(string ip)
|
|
||||||
{
|
|
||||||
//判断是否为Email
|
|
||||||
return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user