初步完善

This commit is contained in:
sin365 2023-05-25 13:46:54 +08:00
parent d6a4c5d0e7
commit 646284b987
45 changed files with 2855 additions and 162 deletions

Binary file not shown.

View File

@ -11,12 +11,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ServerNetwork", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ClientNetwork", "NetLib\HaoYueNet.ClientNetwork\HaoYueNet.ClientNetwork.csproj", "{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleClient", "Simple\SimpleClient\SimpleClient.csproj", "{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleServer", "Simple\SimpleServer\SimpleServer.csproj", "{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HaoYueNet.ClientNetworkNet4x", "NetLib\HaoYueNet.ClientNetworkNet4x\HaoYueNet.ClientNetworkNet4x.csproj", "{A741610F-29B9-4F0E-9A3D-0B3E1318F031}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerCore", "Simple\ServerCore\ServerCore.csproj", "{3F26CB3D-ECD1-4D50-8AE6-EB6762CBACF6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Protobuf", "Simple\Protobuf\Protobuf.csproj", "{E5AE46D9-D809-4E87-8ADD-B1BD28ED60E9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Simple\Server\Server.csproj", "{3F52D1D0-4B1E-4819-9AB3-A770DD5C505D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientCore", "Simple\ClientCore\ClientCore.csproj", "{99E54CE5-4EF6-4441-9F40-8FDE8229041E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client-Cli", "Simple\Client-Cli\Client-Cli.csproj", "{45FBC25D-9EC5-4C8E-A979-005F04CE76AB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -31,18 +37,30 @@ Global
{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519}.Debug|Any CPU.Build.0 = Debug|Any CPU
{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519}.Release|Any CPU.ActiveCfg = Release|Any CPU
{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519}.Release|Any CPU.Build.0 = Release|Any CPU
{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3}.Release|Any CPU.Build.0 = Release|Any CPU
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Release|Any CPU.Build.0 = Release|Any CPU
{A741610F-29B9-4F0E-9A3D-0B3E1318F031}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A741610F-29B9-4F0E-9A3D-0B3E1318F031}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A741610F-29B9-4F0E-9A3D-0B3E1318F031}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A741610F-29B9-4F0E-9A3D-0B3E1318F031}.Release|Any CPU.Build.0 = Release|Any CPU
{3F26CB3D-ECD1-4D50-8AE6-EB6762CBACF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F26CB3D-ECD1-4D50-8AE6-EB6762CBACF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F26CB3D-ECD1-4D50-8AE6-EB6762CBACF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F26CB3D-ECD1-4D50-8AE6-EB6762CBACF6}.Release|Any CPU.Build.0 = Release|Any CPU
{E5AE46D9-D809-4E87-8ADD-B1BD28ED60E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5AE46D9-D809-4E87-8ADD-B1BD28ED60E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5AE46D9-D809-4E87-8ADD-B1BD28ED60E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5AE46D9-D809-4E87-8ADD-B1BD28ED60E9}.Release|Any CPU.Build.0 = Release|Any CPU
{3F52D1D0-4B1E-4819-9AB3-A770DD5C505D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F52D1D0-4B1E-4819-9AB3-A770DD5C505D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F52D1D0-4B1E-4819-9AB3-A770DD5C505D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F52D1D0-4B1E-4819-9AB3-A770DD5C505D}.Release|Any CPU.Build.0 = Release|Any CPU
{99E54CE5-4EF6-4441-9F40-8FDE8229041E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99E54CE5-4EF6-4441-9F40-8FDE8229041E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99E54CE5-4EF6-4441-9F40-8FDE8229041E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99E54CE5-4EF6-4441-9F40-8FDE8229041E}.Release|Any CPU.Build.0 = Release|Any CPU
{45FBC25D-9EC5-4C8E-A979-005F04CE76AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45FBC25D-9EC5-4C8E-A979-005F04CE76AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45FBC25D-9EC5-4C8E-A979-005F04CE76AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45FBC25D-9EC5-4C8E-A979-005F04CE76AB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -50,9 +68,12 @@ Global
GlobalSection(NestedProjects) = preSolution
{22ED65D3-E2FB-49E4-BDF5-EB2A70774C2E} = {D0066C06-A89A-4E05-80E0-D8232FB0FF3C}
{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519} = {D0066C06-A89A-4E05-80E0-D8232FB0FF3C}
{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
{A741610F-29B9-4F0E-9A3D-0B3E1318F031} = {D0066C06-A89A-4E05-80E0-D8232FB0FF3C}
{3F26CB3D-ECD1-4D50-8AE6-EB6762CBACF6} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
{E5AE46D9-D809-4E87-8ADD-B1BD28ED60E9} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
{3F52D1D0-4B1E-4819-9AB3-A770DD5C505D} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
{99E54CE5-4EF6-4441-9F40-8FDE8229041E} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
{45FBC25D-9EC5-4C8E-A979-005F04CE76AB} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {22107F03-013F-4871-AC8E-F082694E2679}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net7.0\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net7.0\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
</PropertyGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,75 @@
syntax = "proto3";
package AxibugProtobuf;
option optimize_for = SPEED;
enum CommandID
{
CMD_DEFAUL = 0;//使
CMD_LOGIN = 2001; // | Protobuf_Login | Protobuf_Login_RESP
CMD_CHATMSG = 4001; // | Protobuf_ChatMsg | Protobuf_ChatMsg_RESP
}
enum ErrorCode
{
ERROR_DEFAUL = 0;//使
ERROR_OK = 1; //
}
enum LoginType
{
BaseDefault = 0;//使
HaoYueAuth = 1;
BF3 = 3;
BF4 = 4;
}
enum DeviceType
{
DeviceType_Default = 0;//使
PC = 1;
Android = 2;
IOS = 3;
PSV = 4;
}
enum LoginResultStatus
{
LoginResultStatus_BaseDefault = 0;//使
OK = 1;
AccountErr = 2;
}
//
message Protobuf_Login
{
LoginType loginType = 1;// [0] [3] BF3 [4] BF4
DeviceType deviceType = 2;// [0]PC [1]AndroidPad预留 [3]IPad预留
string Account = 3;//
string Password = 4;//
}
//
message Protobuf_Login_RESP
{
string Token = 1;//
string LastLoginDate = 2;//
string RegDate = 3;//
LoginResultStatus Status = 4;// [1][0]
}
//
message Protobuf_ChatMsg
{
string ChatMsg = 1;//
}
//
message Protobuf_ChatMsg_RESP
{
string NickName = 1;//
string ChatMsg = 2;//
int64 Date = 3;//
}

Binary file not shown.

View File

@ -1,6 +0,0 @@
call proto2cpp.exe protoc.exe
cd..
set "protopath=%cd%"
cd new_server_proto
copy %cd%\out\cpp\ %protopath%\newhxserver\ProtocolSrc\
pause

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>Client_Cli</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ClientCore\ClientCore.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,41 @@
using ClientCore;
App.Init("127.0.0.1", 23846);
//注册
App.chat.OnChatMsg += OnChatMsg;
while (true)
{
string CommandStr = Console.ReadLine();
string Command = "";
Command = ((CommandStr.IndexOf(" ") <= 0) ? CommandStr : CommandStr.Substring(0, CommandStr.IndexOf(" ")));
string[] CmdArr = CommandStr.Split(' ');
switch (Command)
{
case "login":
case "l":
if (CmdArr.Length < 2)
{
Console.WriteLine("缺省用户名");
return;
}
App.login.Login(CmdArr[1]);
break;
case "say":
if (CmdArr.Length < 2)
{
Console.WriteLine("缺省参数");
return;
}
App.chat.SendChatMsg(CmdArr[1]);
break;
default:
Console.WriteLine("未知命令" + CommandStr);
break;
}
}
void OnChatMsg(string str1, string str2)
{
Console.WriteLine($"[Chat]{str1}:{str2}");
}

31
Simple/ClientCore/App.cs Normal file
View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClientCore.Manager;
using ClientCore.Network;
namespace ClientCore
{
public class App
{
public static string TokenStr;
public static long RID = -1;
public static string IP;
public static int Port;
public static NetworkHelper networkHelper;
public static AppLogin login;
public static AppChat chat;
public static void Init(string IP, int port)
{
networkHelper = new NetworkHelper();
login = new AppLogin();
chat = new AppChat();
networkHelper.Init(IP, port);
}
}
}

View File

@ -1,23 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Data\**" />
<Compile Remove="Manager\**" />
<EmbeddedResource Remove="Data\**" />
<EmbeddedResource Remove="Manager\**" />
<None Remove="Data\**" />
<None Remove="Manager\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NetLib\HaoYueNet.ClientNetwork\HaoYueNet.ClientNetwork.csproj" />
<ProjectReference Include="..\Protobuf\Protobuf.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HaoYueNet.ClientNetwork.NetworkHelperCore;
namespace ClientCore.Event
{
public class DelegateClass
{
public delegate void dg_Str(string Msg);
public delegate void dg_Str_Str(string Str1, string Str2);
}
}

View File

@ -0,0 +1,28 @@
using AxibugProtobuf;
using ClientCore.Network;
using System.Net.Sockets;
using static ClientCore.Event.DelegateClass;
using static HaoYueNet.ClientNetwork.NetworkHelperCore;
namespace ClientCore.Manager
{
public class AppChat
{
public event dg_Str_Str OnChatMsg;
public void SendChatMsg(string ChatMsg)
{
Protobuf_ChatMsg msg = new Protobuf_ChatMsg()
{
ChatMsg = ChatMsg,
};
App.networkHelper.SendToServer((int)CommandID.CmdChatmsg, NetBase.Serizlize(msg));
}
public void RecvChatMsg(byte[] reqData)
{
Protobuf_ChatMsg_RESP msg = NetBase.DeSerizlize<Protobuf_ChatMsg_RESP>(reqData);
OnChatMsg(msg.NickName, msg.ChatMsg);
}
}
}

View File

@ -0,0 +1,18 @@
using AxibugProtobuf;
using ClientCore.Network;
namespace ClientCore.Manager
{
public class AppLogin
{
public void Login(string Account)
{
Protobuf_Login msg = new Protobuf_Login()
{
LoginType = 0,
Account = Account,
};
App.networkHelper.SendToServer((int)CommandID.CmdLogin, NetBase.Serizlize(msg));
}
}
}

View File

@ -1,11 +1,6 @@
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleClient
namespace ClientCore.Network
{
public static class NetBase
{

View File

@ -1,11 +1,12 @@
using HaoYueNet.ClientNetwork;
using AxibugProtobuf;
using HaoYueNet.ClientNetwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleClient
namespace ClientCore.Network
{
/// <summary>
/// 继承网络库,以支持网络功能
@ -52,12 +53,13 @@ namespace SimpleClient
public void GetDataCallBack(int CMDID, int ERRCODE, byte[] data)
{
NetworkDeBugLog("收到消息 CMDID =>" + CMDID + " ERRCODE =>" + ERRCODE + " 数据长度=>" + data.Length);
try
{
//根据协议ID走不同逻辑
switch (CMDID)
switch ((CommandID)CMDID)
{
case CommandID.CmdLogin: break;
case CommandID.CmdChatmsg: App.chat.RecvChatMsg(data); break;
}
}
catch (Exception ex)

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="Google.Protobuf">
<HintPath>..\..\NetLib\Google.Protobuf.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff

20
Simple/Server/Program.cs Normal file
View File

@ -0,0 +1,20 @@
using ServerCore;
using ServerCore.Manager;
ServerManager.InitServer(23846);
while (true)
{
string CommandStr = Console.ReadLine();
string Command = "";
Command = ((CommandStr.IndexOf(" ") <= 0) ? CommandStr : CommandStr.Substring(0, CommandStr.IndexOf(" ")));
switch (Command)
{
case "list":
Console.WriteLine("当前在线:" + ServerManager.g_ClientMgr.GetOnlineClient());
break;
default:
Console.WriteLine("未知命令" + CommandStr);
break;
}
}

View File

@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\NetLib\HaoYueNet.ServerNetwork\HaoYueNet.ServerNetwork.csproj" />
<ProjectReference Include="..\ServerCore\ServerCore.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore.Common
{
public static class Helper
{
public static long GetNowTimeStamp()
{
return GetTimeStamp(DateTime.Now);
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static long GetTimeStamp(DateTime dt)
{
TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
}
}

View File

@ -0,0 +1,24 @@
using AxibugProtobuf;
using ServerCore.Common;
using ServerCore.NetWork;
using System.Net.Sockets;
namespace ServerCore.Manager
{
public class ChatManager
{
public void RecvPlayerChatMsg(Socket sk, byte[] reqData)
{
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
ServerManager.g_Log.Debug("收到新的登录请求");
Protobuf_ChatMsg msg = NetBase.DeSerizlize<Protobuf_ChatMsg>(reqData);
byte[] respData = NetBase.Serizlize(new Protobuf_ChatMsg_RESP()
{
ChatMsg = msg.ChatMsg,
NickName = _c.Account,
Date = Helper.GetNowTimeStamp()
});
ServerManager.g_ClientMgr.ClientSendALL((int)CommandID.CmdChatmsg, (int)ErrorCode.ErrorOk, respData);
}
}
}

View File

@ -1,12 +1,13 @@
using System.Net.Sockets;
using AxibugProtobuf;
using System.Net.Sockets;
using System.Timers;
namespace SimpleServer
namespace ServerCore.Manager
{
public class ClientInfo
{
public long UID { get; set; }
public long RID { get; set; }
public string Account { get; set; }
public Socket _socket { get; set; }
public bool IsOffline { get; set; } = false;
public DateTime LogOutDT { get; set; }
@ -14,14 +15,14 @@ namespace SimpleServer
public class ClientManager
{
public List<ClientInfo> ClientList = new List<ClientInfo>();
public Dictionary<long?, ClientInfo> _DictRIDClient = new Dictionary<long?, ClientInfo>();
public Dictionary<Socket, ClientInfo> _DictSocketClient = new Dictionary<Socket, ClientInfo>();
public Dictionary<long?, ClientInfo> _DictUIDClient = new Dictionary<long?, ClientInfo>();
private List<ClientInfo> ClientList = new List<ClientInfo>();
private Dictionary<Socket, ClientInfo> _DictSocketClient = new Dictionary<Socket, ClientInfo>();
private Dictionary<long?, ClientInfo> _DictUIDClient = new Dictionary<long?, ClientInfo>();
private long TestUIDSeed = 0;
private System.Timers.Timer _ClientCheckTimer;
private long _RemoveOfflineCacheMin;
public void Init(long ticktime,long RemoveOfflineCacheMin)
public void Init(long ticktime, long RemoveOfflineCacheMin)
{
//换算成毫秒
_RemoveOfflineCacheMin = RemoveOfflineCacheMin * 1000;
@ -31,8 +32,13 @@ namespace SimpleServer
_ClientCheckTimer.Elapsed += new ElapsedEventHandler(ClientCheckClearOffline_Elapsed);
_ClientCheckTimer.Enabled = true;
}
private void ClientCheckClearOffline_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
public long GetNextUID()
{
return ++TestUIDSeed;
}
private void ClientCheckClearOffline_Elapsed(object sender, ElapsedEventArgs e)
{
DateTime CheckDT = DateTime.Now.AddMinutes(-1 * _RemoveOfflineCacheMin);
ClientInfo[] OfflineClientlist = ClientList.Where(w => w.IsOffline == true && w.LogOutDT < CheckDT).ToArray();
@ -45,24 +51,47 @@ namespace SimpleServer
}
GC.Collect();
}
//通用处理
#region clientlist
public ClientInfo JoinNewClient(Protobuf_Login data, Socket _socket)
{
//也许这个函数需加lock
ClientInfo cinfo = GetClientForSocket(_socket);
//如果连接还在
if (cinfo != null)
{
cinfo.IsOffline = false;
}
else
{
cinfo = new ClientInfo()
{
UID = GetNextUID(),
_socket = _socket,
Account = data.Account,
IsOffline = false,
};
AddClient(cinfo);
}
return cinfo;
}
/// <summary>
/// 增加用户
/// </summary>
/// <param name="client"></param>
public void AddClient(ClientInfo clientInfo)
void AddClient(ClientInfo clientInfo)
{
try
{
Console.WriteLine("追加连接玩家 RID=>" + clientInfo.RID + " UID=>" + clientInfo.UID);
Console.WriteLine("追加连接玩家 UID=>" + clientInfo.UID + " | " + clientInfo.Account);
lock (ClientList)
{
_DictUIDClient.Add(clientInfo.UID, clientInfo);
_DictRIDClient.Add(clientInfo.RID, clientInfo);
_DictSocketClient.Add(clientInfo._socket, clientInfo);
ClientList.Add(clientInfo);
}
@ -72,7 +101,7 @@ namespace SimpleServer
ex.ToString();
}
}
/// <summary>
/// 清理连接
/// </summary>
@ -81,23 +110,16 @@ namespace SimpleServer
{
lock (ClientList)
{
if(_DictUIDClient.ContainsKey(client.UID))
if (_DictUIDClient.ContainsKey(client.UID))
_DictUIDClient.Remove(client.UID);
if (_DictRIDClient.ContainsKey(client.RID))
_DictRIDClient.Remove(client.RID);
if (_DictSocketClient.ContainsKey(client._socket))
_DictSocketClient.Remove(client._socket);
ClientList.Remove(client);
}
}
public ClientInfo GetClientForRoleID(int RoleID)
{
return _DictRIDClient.ContainsKey(RoleID) ? _DictRIDClient[RoleID] : null;
}
public ClientInfo GetClientForSocket(Socket sk)
{
@ -123,7 +145,7 @@ namespace SimpleServer
if (!_DictSocketClient.ContainsKey(sk))
return;
Console.WriteLine("标记玩家RID"+ _DictSocketClient[sk].RID+ "为离线");
Console.WriteLine("标记玩家UID" + _DictSocketClient[sk].UID + "为离线");
_DictSocketClient[sk].IsOffline = true;
_DictSocketClient[sk].LogOutDT = DateTime.Now;
}
@ -134,17 +156,14 @@ namespace SimpleServer
return;
RemoveClient(_DictSocketClient[sk]);
//ClientList.Remove(GetClientForSocket(sk));
}
#endregion
//public void AddClient_JoinGame(Socket sk)
//{
// var c = new ClientInfo();
// c = AutoRoleID;
// AddClient(c);
//}
public void ClientSendALL(int CMDID, int ERRCODE, byte[] data)
{
ClientSend(ClientList, CMDID, ERRCODE, data);
}
/// <summary>
/// 给一组用户发送数据
@ -155,11 +174,11 @@ namespace SimpleServer
/// <param name="data"></param>
public void ClientSend(List<ClientInfo> _toclientlist, int CMDID, int ERRCODE, byte[] data)
{
for (int i = 0; i < _toclientlist.Count();i++)
for (int i = 0; i < _toclientlist.Count(); i++)
{
if (_toclientlist[i] == null || _toclientlist[i].IsOffline)
continue;
ServerManager.g_SocketMgr.SendToSocket(_toclientlist[i]._socket, CMDID,ERRCODE,data);
ServerManager.g_SocketMgr.SendToSocket(_toclientlist[i]._socket, CMDID, ERRCODE, data);
}
}
@ -182,5 +201,10 @@ namespace SimpleServer
return;
ServerManager.g_SocketMgr.SendToSocket(_c._socket, CMDID, ERRCODE, data);
}
public int GetOnlineClient()
{
return ClientList.Where(w => !w.IsOffline).Count();
}
}
}

View File

@ -0,0 +1,20 @@
namespace ServerCore.Manager
{
public class LogManager
{
public void Debug(string str)
{
Console.WriteLine(str);
}
public void Warning(string str)
{
Console.WriteLine(str);
}
public void Error(string str)
{
Console.WriteLine(str);
}
}
}

View File

@ -0,0 +1,26 @@
using AxibugProtobuf;
using ServerCore.NetWork;
using System.Net.Sockets;
namespace ServerCore.Manager
{
public class LoginManager
{
public void UserLogin(Socket _socket, byte[] reqData)
{
ServerManager.g_Log.Debug("收到新的登录请求");
Protobuf_Login msg = NetBase.DeSerizlize<Protobuf_Login>(reqData);
ClientInfo cinfo = ServerManager.g_ClientMgr.JoinNewClient(msg, _socket);
byte[] respData = NetBase.Serizlize(new Protobuf_Login_RESP()
{
Status = LoginResultStatus.Ok,
RegDate = "",
LastLoginDate = "",
Token = ""
});
ServerManager.g_ClientMgr.ClientSend(cinfo, (int)CommandID.CmdLogin, (int)ErrorCode.ErrorOk, respData);
}
}
}

View File

@ -0,0 +1,27 @@
using System.Net;
using ServerCore.NetWork;
namespace ServerCore.Manager
{
public static class ServerManager
{
public static ClientManager g_ClientMgr;
public static LogManager g_Log;
public static LoginManager g_Login;
public static ChatManager g_Chat;
public static IOCPNetWork g_SocketMgr;
public static void InitServer(int port)
{
g_ClientMgr = new ClientManager();
g_Log = new LogManager();
g_Login = new LoginManager();
g_Chat = new ChatManager();
g_SocketMgr = new IOCPNetWork(1024, 1024);
g_SocketMgr.Init();
g_SocketMgr.Start(new IPEndPoint(IPAddress.Any.Address, port));
Console.WriteLine("监听:" + port);
Console.WriteLine("Succeed!");
}
}
}

View File

@ -1,7 +1,9 @@
using HaoYueNet.ServerNetwork;
using AxibugProtobuf;
using HaoYueNet.ServerNetwork;
using ServerCore.Manager;
using System.Net.Sockets;
namespace SimpleServer
namespace ServerCore.NetWork
{
public class IOCPNetWork : SocketManager
{
@ -25,7 +27,7 @@ namespace SimpleServer
private void IOCPNetWork_ClientNumberChange(int num, AsyncUserToken token)
{
Console.WriteLine("建立新的连接");
Console.WriteLine("Client数发生变化");
}
/// <summary>
@ -41,12 +43,13 @@ namespace SimpleServer
public void DataCallBackToOld(Socket sk, int CMDID, byte[] data)
{
//ServerManager.g_Log.Debug("收到消息 CMDID =>" + CMDID + " 数据长度=>" + data.Length);
ServerManager.g_Log.Debug("收到消息 CMDID =>" + CMDID + " 数据长度=>" + data.Length);
try
{
switch (CMDID)
switch ((CommandID)CMDID)
{
case CommandID.CmdLogin: ServerManager.g_Login.UserLogin(sk, data); break;
case CommandID.CmdChatmsg: ServerManager.g_Chat.RecvPlayerChatMsg(sk, data); break;
}
}
catch (Exception ex)
@ -63,16 +66,16 @@ namespace SimpleServer
{
OnCloseToOld(token.Socket);
}
/// <summary>
/// 断开连接
/// </summary>
/// <param name="sk"></param>
public void OnCloseToOld(Socket sk)
{
//ServerManager.g_Log.Debug("清理掉线");
Console.WriteLine("断开连接");
ServerManager.g_ClientMgr.SetClientOfflineForSocket(sk);
}
}
}

View File

@ -0,0 +1,22 @@
using Google.Protobuf;
namespace ServerCore.NetWork
{
public static class NetBase
{
public static byte[] Serizlize(IMessage msg)
{
return msg.ToByteArray();
}
public static T DeSerizlize<T>(byte[] bytes)
{
var msgType = typeof(T);
object msg = Activator.CreateInstance(msgType);
((IMessage)msg).MergeFrom(bytes);
return (T)msg;
}
}
}

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\NetLib\HaoYueNet.ServerNetwork\HaoYueNet.ServerNetwork.csproj" />
<ProjectReference Include="..\Protobuf\Protobuf.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Google.Protobuf">
<HintPath>..\..\NetLib\Google.Protobuf.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@ -1,22 +0,0 @@
// See https://aka.ms/new-console-template for more information
using SimpleClient;
Console.WriteLine("Hello, World!");
StaticComm.networkHelper = new NetworkHelper();
StaticComm.networkHelper.Init("127.0.0.1", 23846);
while (true)
{
string CommandStr = Console.ReadLine();
string Command = "";
Command = ((CommandStr.IndexOf(" ") <= 0) ? CommandStr : CommandStr.Substring(0, CommandStr.IndexOf(" ")));
switch (Command)
{
default:
Console.WriteLine("未知命令" + CommandStr);
break;
}
}

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleClient
{
public class StaticComm
{
public static string TokenStr;
public static long RID = -1;
public static string IP;
public static int Port;
public static NetworkHelper networkHelper;
}
}

View File

@ -1,8 +0,0 @@
namespace SimpleServer
{
public static class ServerManager
{
public static ClientManager g_ClientMgr;
public static IOCPNetWork g_SocketMgr;
}
}

View File

@ -1,27 +0,0 @@
// See https://aka.ms/new-console-template for more information
using SimpleServer;
using System.Net;
Console.WriteLine("Hello, World!");
ServerManager.g_ClientMgr = new ClientManager();
ServerManager.g_SocketMgr = new IOCPNetWork(1024, 1024);
ServerManager.g_SocketMgr.Init();
ServerManager.g_SocketMgr.Start(new IPEndPoint(IPAddress.Any.Address, 23846));
Console.WriteLine("监听:" + 23846);
Console.WriteLine("Succeed!");
while (true)
{
string CommandStr = Console.ReadLine();
string Command = "";
Command = ((CommandStr.IndexOf(" ") <= 0) ? CommandStr : CommandStr.Substring(0, CommandStr.IndexOf(" ")));
switch(Command)
{
case "list":
Console.WriteLine("当前在线:" + ServerManager.g_ClientMgr.ClientList.Count());
break;
default:
Console.WriteLine("未知命令"+CommandStr);
break;
}
}