mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 11:24:46 +00:00
172 lines
5.5 KiB
C#
172 lines
5.5 KiB
C#
|
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
|
|||
|
{
|
|||
|
|
|||
|
// /// <summary>
|
|||
|
// /// 服务器
|
|||
|
// /// </summary>
|
|||
|
// /// <returns></returns>
|
|||
|
// private TcpListener server;
|
|||
|
//
|
|||
|
// /// <summary>
|
|||
|
// /// 服务器线程
|
|||
|
// /// </summary>
|
|||
|
// /// <returns></returns>
|
|||
|
// private Thread thread;
|
|||
|
// /// <summary>
|
|||
|
// /// 客户端线程
|
|||
|
// /// </summary>
|
|||
|
// /// <returns></returns>
|
|||
|
// private Dictionary<string,Socket> threads = new();
|
|||
|
//
|
|||
|
// private int _clientIndex = 0;
|
|||
|
//
|
|||
|
// /// <summary>
|
|||
|
// /// 连接的客户端
|
|||
|
// /// </summary>
|
|||
|
// KeyValue<int,Socket> clients = new ();
|
|||
|
//
|
|||
|
// private int _port = 0;
|
|||
|
// public int Port => _port;
|
|||
|
//
|
|||
|
//
|
|||
|
// public override async Task OnInit()
|
|||
|
// {
|
|||
|
// await CreateServer();
|
|||
|
// }
|
|||
|
//
|
|||
|
// /// <summary>
|
|||
|
// /// 关闭服务器
|
|||
|
// /// </summary>
|
|||
|
// public override void OnClose()
|
|||
|
// {
|
|||
|
// Debug.Log($"[JNUDPServer] 关闭服务器");
|
|||
|
// base.OnClose();
|
|||
|
// server?.Stop();
|
|||
|
// thread?.Abort();
|
|||
|
// threads.ForEach(child => child.Value.Close());
|
|||
|
// }
|
|||
|
//
|
|||
|
// /// <summary>
|
|||
|
// /// 创建服务器
|
|||
|
// /// </summary>
|
|||
|
// private async UniTask CreateServer()
|
|||
|
// {
|
|||
|
//
|
|||
|
// server = new TcpListener(IPAddress.Any,_port = await GetPort());
|
|||
|
// server.Start();
|
|||
|
// thread = new Thread(CreatConnectSocket);
|
|||
|
// thread.Start();
|
|||
|
//
|
|||
|
// }
|
|||
|
//
|
|||
|
// protected virtual async UniTask<int> GetPort()
|
|||
|
// {
|
|||
|
// await UniTask.NextFrame();
|
|||
|
// return 9001;
|
|||
|
// }
|
|||
|
//
|
|||
|
//
|
|||
|
// /// <summary>
|
|||
|
// /// 接受监听后保存生成的通信客户端,并开启线程监听通信客户端消息
|
|||
|
// /// </summary>
|
|||
|
// 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();
|
|||
|
// }
|
|||
|
// }
|
|||
|
//
|
|||
|
// /// <summary>
|
|||
|
// /// 接受通信客户端消息并对消息进行处理
|
|||
|
// /// </summary>
|
|||
|
// /// <param name="socket"></param>
|
|||
|
// void ListenConnectSocket(Socket socket)
|
|||
|
// {
|
|||
|
// Debug.Log($"[JNTCPServer] 客户端连接");
|
|||
|
// //客户端连接
|
|||
|
// _event.Dispatch($"{(int)NActionEnum.ClientConnect}",new JNServerParam()
|
|||
|
// {
|
|||
|
// Client = socket,
|
|||
|
// Message = Array.Empty<byte>()
|
|||
|
// });
|
|||
|
// 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<byte>()
|
|||
|
// });
|
|||
|
// 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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|