feature:3.0.0

This commit is contained in:
lishuo committed 2023-08-01 11:02:04 +08:00
1 parent 65c19a5071
commit 9e1e0707dc
4 files changed
+180 -309

No files matched your search

+163 -58
View File
@@ -1,93 +1,198 @@
using OnlineUserPool.Model;
using NetCoreServer;
using Newtonsoft.Json;
using OnlineUserPool.Model;
using OnlineUserPool.Unility;
using Serilog;
using SimpleTcp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace OnlineUserPool.Services
{
public class TcpService : IService
{
private bool _stop = false;
SimpleTcpServer socket;
Action<IPEndPoint, MsgModel, IService> _action;
private PfTcpServer _server;
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
public void Close()
{
//throw new NotImplementedException();
_action = action;
//var ip = IPAddress.Parse("192.168.0.97");
var b = IPAddress.Any;
socket = new SimpleTcpServer($"{ IPAddress.Any }:11001");
socket.Events.ClientConnected += Events_ClientConnected;
socket.Events.ClientDisconnected += Events_ClientDisconnected;
socket.Events.DataReceived += Events_DataReceived;
Task.Run(() =>
{
socket.Start();
});
}
private void Events_DataReceived(object sender, SimpleTcp.DataReceivedEventArgs e)
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> action, Action<EndPoint> disconnected = null)
{
var returnData = Encoding.UTF8.GetString(e.Data);
Debug.WriteLine(returnData);
_server = new PfTcpServer(IPAddress.Any, ConfigHelp.TcpPort, (ip, model) => { action(ip, model, this); },
disconnected);
foreach (var item in returnData.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries))
{
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(item +"}");
var ipEndPoint = IPEndPoint.Parse(e.IpPort);
_action(ipEndPoint, msg, this);
}
}
private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
{
}
private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
{
Debug.WriteLine("有新客户端连接" + e.IpPort);
_server.Start();
}
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint)
{
if(dgram == null || !dgram.Any())
if (dgram == null || !dgram.Any())
{
return;
}
foreach (var item in socket.GetClients())
var session = _server.FindSession(endPoint);
if (session is { IsConnected: true })
{
//try
//{
if(socket.IsConnected(item) == false)
{
continue;
}
socket.Send(item, dgram);
//}
//catch(Exception e)
//{
// Log.Error($"{ item.ToString() }:{ e.Message }\r\n{ e.StackTrace }");
//}
session.SendAsync(dgram);
}
}
public void Close()
private class PfTcpServer : TcpServer
{
_stop = true;
socket.Dispose();
private Action<IPEndPoint, ReceiveModel> _action;
private Action<EndPoint> _disconnected;
public PfTcpServer(IPAddress address, int port, Action<IPEndPoint, ReceiveModel> action,
Action<EndPoint> disconnected = null) : base(address, port)
{
_action = action;
_disconnected = disconnected;
}
protected override TcpSession CreateSession()
{
return new PfTcpSession(this, _action);
}
protected override void OnError(SocketError error)
{
//base.OnError(error);
Debug.WriteLine(error);
}
public TcpSession FindSession(IPEndPoint ip)
{
try
{
var item = this.Sessions.Values.FirstOrDefault(t =>
t.Socket.Connected && t.Socket.RemoteEndPoint.ToString() == ip.ToString());
return item;
}
catch (Exception e)
{
Log.Error( $"Fail to FindSession:{e}");
return null;
}
}
protected override void OnDisconnected(TcpSession session)
{
var currentSession = (session as PfTcpSession);
if (currentSession?.IPEndPoint != null)
{
_disconnected?.Invoke(currentSession.IPEndPoint);
}
base.OnDisconnected(session);
}
}
private class PfTcpSession : TcpSession
{
Action<IPEndPoint, ReceiveModel> _action;
public IPEndPoint IPEndPoint { get; private set; }
public PfTcpSession(TcpServer server, Action<IPEndPoint, ReceiveModel> action) : base(server)
{
_action = action;
}
protected override void OnConnected()
{
base.OnConnected();
Debug.WriteLine("有新的Tcp连接");
IPEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
}
protected override void OnDisconnected()
{
base.OnDisconnected();
Debug.WriteLine("Tcp断开连接");
}
private readonly StringBuilder _dataBuffer = new StringBuilder();
protected override void OnReceived(byte[] buffer, long offset, long size)
{
var data = string.Empty;
try
{
data = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
foreach (var item in data)
{
this._dataBuffer.Append(item);
if (item != '}') continue;
Debug.WriteLine("收到消息:" + this._dataBuffer);
var msg = JsonConvert.DeserializeObject<ReceiveModel>(this._dataBuffer.ToString());
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
switch (msg.CommandType)
{
case 1:
_action(ipEndPoint, JsonConvert.DeserializeObject<MsgModel>(this._dataBuffer.ToString()));
break;
case 2:
_action(ipEndPoint, JsonConvert.DeserializeObject<SetClientCommand>(this._dataBuffer.ToString()));
break;
case 3:
switch (msg.SubType)
{
case 0:
_action(ipEndPoint,
JsonConvert.DeserializeObject<CreateGameRoomCommand>(this._dataBuffer.ToString()));
break;
case 1:
_action(ipEndPoint,
JsonConvert.DeserializeObject<JoinGameRoomCommand>(this._dataBuffer.ToString()));
break;
case 2:
_action(ipEndPoint,
JsonConvert.DeserializeObject<GameRoomReadyCommand>(this._dataBuffer.ToString()));
break;
case 3:
_action(ipEndPoint,
JsonConvert.DeserializeObject<GameRoomStartCommand>(this._dataBuffer.ToString()));
break;
case 4:
_action(ipEndPoint,
JsonConvert.DeserializeObject<GameRoomKickCommand>(this._dataBuffer.ToString()));
break;
case 5:
_action(ipEndPoint,
JsonConvert.DeserializeObject<GameRoomProcessCommand>(this._dataBuffer.ToString()));
break;
case 6:
_action(ipEndPoint,
JsonConvert.DeserializeObject<QueryGameRoomListCommand>(this._dataBuffer.ToString()));
break;
}
break;
default:
_action(ipEndPoint, msg);
break;
}
this._dataBuffer.Clear();
}
}
catch (Exception ex)
{
Log.Error($"接受到的数据处理时出错:{data}\r\n{ex.Message}\r\n{ex.StackTrace}");
}
}
protected override void OnError(SocketError error)
{
//base.OnError(error);
Debug.WriteLine(error);
}
}
}
}
-213
View File
@@ -1,213 +0,0 @@
using NetCoreServer;
using Newtonsoft.Json;
using OnlineUserPool.Model;
using OnlineUserPool.Unility;
using Serilog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace OnlineUserPool.Services
{
public class TcpService1 : IService
{
private PfTcpServer _server;
public void Close()
{
//throw new NotImplementedException();
}
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> action, Action<EndPoint> disconnected = null)
{
//throw new NotImplementedException();
//_action = action;
_server = new PfTcpServer(IPAddress.Any, ConfigHelp.TcpPort, (ip, model)=> {
action(ip, model, this);
}, disconnected);
//_server.OptionNoDelay = true;
_server.Start();
}
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint)
{
if (dgram == null || !dgram.Any())
{
return;
}
//_server.Multicast(dgram);
var session = _server.FindSession(endPoint);
if(session != null && session.IsConnected)
{
session.SendAsync(dgram);
}
}
class PfTcpServer : TcpServer
{
private Action<IPEndPoint, ReceiveModel> _action;
private Action<EndPoint> _disconnected;
//private System.Collections.Generic.Dictionary<Guid, PfTcpSession> clients = new Dictionary<Guid, PfTcpSession>();
public PfTcpServer(IPAddress address, int port, Action<IPEndPoint, ReceiveModel> action, Action<EndPoint> disconnected = null) :base(address, port)
{
_action = action;
_disconnected = disconnected;
}
protected override TcpSession CreateSession()
{
//return base.CreateSession()
return new PfTcpSession(this, _action);
}
protected override void OnError(SocketError error)
{
//base.OnError(error);
Debug.WriteLine(error);
}
public TcpSession FindSession(IPEndPoint ip)
{
var item = this.Sessions.Values.FirstOrDefault(t => t.Socket.RemoteEndPoint.ToString() == ip.ToString());
return item;
}
protected override void OnDisconnected(TcpSession session)
{
//var bbb = this.Sessions.GetValueOrDefault(session.Id);
var bbb = (session as PfTcpSession);
if(bbb.IPEndPoint != null)
{
_disconnected?.Invoke(bbb.IPEndPoint);
}
base.OnDisconnected(session);
}
}
class PfTcpSession : TcpSession
{
Action<IPEndPoint, ReceiveModel> _action;
public IPEndPoint IPEndPoint { get; private set; }
public PfTcpSession(TcpServer server, Action<IPEndPoint, ReceiveModel> action) : base(server) {
_action = action;
}
protected override void OnConnected()
{
base.OnConnected();
Debug.WriteLine("有新的Tcp连接");
IPEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
}
protected override void OnDisconnected()
{
base.OnDisconnected();
Debug.WriteLine("Tcp断开连接");
}
private string temp = "";
protected override void OnReceived(byte[] buffer, long offset, long size)
{
//base.OnReceived(buffer, offset, size);
//Debug.WriteLine(returnData);
//Log.Information(returnData + "\r\n");
string returnData = "";
try
{
returnData = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
//var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(returnData);
//var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
//_action(ipEndPoint, msg);
foreach (var item in returnData)
{
if(item == '}')
{
temp += item;
Debug.WriteLine("收到消息:"+temp);
var msg = JsonConvert.DeserializeObject<ReceiveModel>(temp);
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
if (msg.CommandType == 1)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<MsgModel>(temp));
}
else if (msg.CommandType == 2)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<SetClientCommand>(temp));
}
//else if(msg.CommandType == 3)
//{
// _action(ipEndPoint, JsonConvert.DeserializeObject<SetWatchCommand>(temp));
//}
else if (msg.CommandType == 3)
{
if (msg.SubType == 0)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<CreateGameRoomCommand>(temp));
}
if (msg.SubType == 1)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<JoinGameRoomCommand>(temp));
}
if (msg.SubType == 2)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<GameRoomReadyCommand>(temp));
}
if (msg.SubType == 3)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<GameRoomStartCommand>(temp));
}
if (msg.SubType == 4)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<GameRoomKickCommand>(temp));
}
if (msg.SubType == 5)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<GameRoomProcessCommand>(temp));
}
if (msg.SubType == 6)
{
_action(ipEndPoint, JsonConvert.DeserializeObject<QueryGameRoomListCommand>(temp));
}
}
else
{
_action(ipEndPoint, msg);
}
temp = "";
}
else
{
temp += item;
}
}
//foreach (var item in returnData.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries))
//{
// //if (string.IsNullOrWhiteSpace(item)) continue;
// var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(item + "}");
// var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
// _action(ipEndPoint, msg);
//}
}
catch (Exception ex)
{
Log.Error($"接受到的数据处理时出错:{ returnData }\r\n{ ex.Message }\r\n{ ex.StackTrace }");
}
}
protected override void OnError(SocketError error)
{
//base.OnError(error);
Debug.WriteLine(error);
}
}
}
}