using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using DotRecast.Core.Collections; using Google.Protobuf; using JNGame.Util.Types; using Plugins.JNGame.Network.Action; using Plugins.JNGame.Network.Entity; using Plugins.JNGame.Network.Util; using Plugins.JNGame.System; using UnityEngine; namespace Plugins.JNGame.Network { public abstract class JNTCPServer1 : JNServerBase { // /// // /// 服务器 // /// // /// // private TcpListener server; // // /// // /// 服务器线程 // /// // /// // private Thread thread; // /// // /// 客户端线程 // /// // /// // private Dictionary threads = new(); // // private int _clientIndex = 0; // // /// // /// 连接的客户端 // /// // KeyValue clients = new (); // // private int _port = 0; // public int Port => _port; // // // public override async Task OnInit() // { // await CreateServer(); // } // // /// // /// 关闭服务器 // /// // public override void OnClose() // { // Debug.Log($"[JNUDPServer] 关闭服务器"); // base.OnClose(); // server?.Stop(); // thread?.Abort(); // threads.ForEach(child => child.Value.Close()); // } // // /// // /// 创建服务器 // /// // private async UniTask CreateServer() // { // // server = new TcpListener(IPAddress.Any,_port = await GetPort()); // server.Start(); // thread = new Thread(CreatConnectSocket); // thread.Start(); // // } // // protected virtual async UniTask GetPort() // { // await UniTask.NextFrame(); // return 9001; // } // // // /// // /// 接受监听后保存生成的通信客户端,并开启线程监听通信客户端消息 // /// // void CreatConnectSocket() // { // Debug.Log($"[JNTCPServer] 创建服务器成功"); // while (true) // { // _clientIndex += 1; // Socket socket = server.AcceptSocket(); // clients.Add(_clientIndex,socket); // Thread thread1 = new Thread(() => { ListenConnectSocket(socket); }); // thread1.Start(); // } // } // // /// // /// 接受通信客户端消息并对消息进行处理 // /// // /// // void ListenConnectSocket(Socket socket) // { // Debug.Log($"[JNTCPServer] 客户端连接"); // //客户端连接 // _event.Dispatch($"{(int)NActionEnum.ClientConnect}",new JNServerParam() // { // Client = socket, // Message = Array.Empty() // }); // byte[] bytes = new byte[102400]; // try // { // while (true) // { // var max = socket.Receive(bytes); // var message = new byte[max]; // if (max >= 102400) // { // throw new Exception($"[JNTCPServer] 超出最大接收{max}"); // } // Array.Copy(bytes,message,max); // var param = NDataUtil.Parse(message); // _byteSize[param.HId] = param.Bytes.Length; // _event.Dispatch($"{param.HId}",new JNServerParam() // { // Client = socket, // Message = param.Bytes // }); // } // } // catch (Exception e) // { // Debug.LogWarning(e.Message); // Debug.Log($"[JNTCPServer] 断开客户端连接"); // //客户端断开 // _event.Dispatch($"{(int)NActionEnum.ClientDisconnect}",new JNServerParam() // { // Client = socket, // Message = Array.Empty() // }); // socket.Close(); // clients.RemoveByValue(socket); // } // } // // public void Send(Socket client,int hId,IMessage data = null) // { // var bytes = NDataUtil.Encrypt(JNetParam.Build(this._id++, hId).SetData(data)); // _byteSize[hId] = bytes.Length; // client.SendAsync(bytes,SocketFlags.None); // } // // public void AllSend(int hId,IMessage data = null) // { // clients.Values.ForEach(child => // { // Send(child.Value, hId, data); // }); // } // public override Task OnInit() { throw new NotImplementedException(); } } }