184 lines
6.2 KiB
C#
Raw Normal View History

using Assets.Scenes.Ride.Scripts.Model;
using Assets.Scripts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Assets.Scenes.Ride.Scripts.Network
{
public class TcpService1 : IService
{
private bool isExit = false;
private PfTcpClient _tcpClient;
private IPEndPoint iPEndPoint;
//private Action<List<ReceiveMsgModel>> _action;
public void Start(Action<List<ReceiveMsgModel>> action)
{
if (_tcpClient == null)
{
//if (!IPAddress.TryParse(ConfigHelper.ServerIpAdress, out var ipString))
//{
// throw new ArgumentException("IP地址不正确");
//}
//服务端端口
iPEndPoint = App.TcpAddress;
_tcpClient = new PfTcpClient(iPEndPoint.Address.ToString(), iPEndPoint.Port, action);
_tcpClient.OptionNoDelay = true;
//_action = action;
_tcpClient.ConnectAsync();
}
}
public void Send(byte[] dgram, int bytes)
{
if (_tcpClient == null) return;
if (_tcpClient.IsConnected == false) return;
//_tcpClient.WriteLine(System.Text.Encoding.ASCII.GetString(dgram));
var txt = System.Text.Encoding.ASCII.GetString(dgram);
//Debug.WriteLine("发送:"+txt);
_tcpClient.Send(txt);
}
public void Close()
{
isExit = true;
_tcpClient.DisconnectAndStop();
_tcpClient = null;
}
class PfTcpClient : NetCoreServer.TcpClient
{
private bool _stop;
Action<List<ReceiveMsgModel>> _action;
public PfTcpClient(string address, int port, Action<List<ReceiveMsgModel>> action) : base(address, port)
{
_action = action;
}
public void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected)
{
Thread.Yield();
}
}
protected override void OnConnected()
{
base.OnConnected();
MapUDPService.SendGizpModel();
}
protected override void OnDisconnected()
{
//base.OnDisconnected();
Debug.WriteLine("tcp断线3秒后重连");
Thread.Sleep(3000);
if (!_stop)
{
ConnectAsync();
}
}
public string Decompress(byte[] data)
{
// Read the last 4 bytes to get the length
byte[] lengthBuffer = new byte[4];
Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);
var buffer = new byte[uncompressedSize];
using (var ms = new MemoryStream(data))
{
using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
{
gzip.Read(buffer, 0, uncompressedSize);
}
}
return Encoding.UTF8.GetString(buffer);
}
private string temp = "";
protected override void OnReceived(byte[] buffer, long offset, long size)
{
//base.OnReceived(buffer, offset, size);
var returnData = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
//*sdfsd#*sdfsd#*sdfsd#*sdfsd#
foreach (var item in returnData)
{
if (item != '*' && item != '#')
{
temp += item;
}
if (item == '#')
{
//解压 temp
var uncompressStr = Decompress(Convert.FromBase64String(temp));
#if UNITY_EDITOR
Console.WriteLine(uncompressStr);
#endif
var list = new List<ReceiveMsgModel>();
//数据解析 l{},w{1|2|3|4} l:列表 w:观察者模式
var arr = uncompressStr.Split(';');
foreach (var t in arr)
{
//列表
if (t.IndexOf('l') == 0)
{
var l = t.Replace("l{", "").Replace("}", "");
var itemList = l.Split('|');
temp = "";
foreach (var obj in itemList)
{
var info = ReceiveMsgModel.Parse(obj);
if (info != null)
{
list.Add(info);
}
}
}
//观察者
if (t.IndexOf('w') == 0)
{
var w = t.Replace("w{", "").Replace("}", "");
foreach (var o in list)
{
o.WatchIdList = w;
}
}
}
_action(list);
}
}
//Debug.WriteLine("收到:" + returnData+"\r\n");
//System.IO.File.AppendAllText(System.Environment.CurrentDirectory + "接收到的数据.txt", returnData.Trim().Replace("\0", "")+"\r\n", Encoding.UTF8);
}
protected override void OnError(SocketError error)
{
//base.OnError(error);
Debug.WriteLine(error);
}
}
}
}