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 action, Action 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 _action; private Action _disconnected; //private System.Collections.Generic.Dictionary clients = new Dictionary(); public PfTcpServer(IPAddress address, int port, Action action, Action 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 _action; public IPEndPoint IPEndPoint { get; private set; } public PfTcpSession(TcpServer server, Action 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(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(temp); var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); if (msg.CommandType == 1) { _action(ipEndPoint, JsonConvert.DeserializeObject(temp)); } else if (msg.CommandType == 2) { _action(ipEndPoint, JsonConvert.DeserializeObject(temp)); } //else if(msg.CommandType == 3) //{ // _action(ipEndPoint, JsonConvert.DeserializeObject(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(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); } } } }