2021-03-30 17:15:16 +08:00
|
|
|
|
using Assets.Scenes.Ride.Scripts.Model;
|
|
|
|
|
|
using Assets.Scripts;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
2021-07-01 18:41:35 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.IO.Compression;
|
2021-03-30 17:15:16 +08:00
|
|
|
|
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地址不正确");
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
//服务端端口
|
2021-04-30 18:03:34 +08:00
|
|
|
|
iPEndPoint = App.TcpAddress;
|
|
|
|
|
|
_tcpClient = new PfTcpClient(iPEndPoint.Address.ToString(), iPEndPoint.Port, action);
|
2021-03-30 17:15:16 +08:00
|
|
|
|
_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();
|
2021-07-01 18:41:35 +08:00
|
|
|
|
MapUDPService.SendGizpModel();
|
2021-03-30 17:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnDisconnected()
|
|
|
|
|
|
{
|
|
|
|
|
|
//base.OnDisconnected();
|
|
|
|
|
|
Debug.WriteLine("tcp断线,3秒后重连");
|
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
|
|
|
|
|
|
|
if (!_stop)
|
|
|
|
|
|
{
|
|
|
|
|
|
ConnectAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-01 18:41:35 +08:00
|
|
|
|
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 = "";
|
2021-03-30 17:15:16 +08:00
|
|
|
|
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);
|
2021-07-01 18:41:35 +08:00
|
|
|
|
//*sdfsd#*sdfsd#*sdfsd#*sdfsd#
|
|
|
|
|
|
foreach (var item in returnData)
|
2021-03-30 17:15:16 +08:00
|
|
|
|
{
|
2021-07-01 18:41:35 +08:00
|
|
|
|
if (item != '*' && item != '#')
|
|
|
|
|
|
{
|
|
|
|
|
|
temp += item;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (item == '#')
|
2021-03-30 17:15:16 +08:00
|
|
|
|
{
|
2021-07-01 18:41:35 +08:00
|
|
|
|
//解压 temp
|
|
|
|
|
|
var uncompressStr = Decompress(Convert.FromBase64String(temp));
|
2021-07-06 18:24:15 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
Console.WriteLine(uncompressStr);
|
|
|
|
|
|
#endif
|
2021-07-01 18:41:35 +08:00
|
|
|
|
var list = new List<ReceiveMsgModel>();
|
2021-07-06 18:24:15 +08:00
|
|
|
|
//数据解析 l{},w{1|2|3|4} l:列表 w:观察者模式
|
|
|
|
|
|
var arr = uncompressStr.Split(';');
|
|
|
|
|
|
foreach (var t in arr)
|
2021-07-01 18:41:35 +08:00
|
|
|
|
{
|
2021-07-06 18:24:15 +08:00
|
|
|
|
//列表
|
|
|
|
|
|
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)
|
2021-07-01 18:41:35 +08:00
|
|
|
|
{
|
2021-07-06 18:24:15 +08:00
|
|
|
|
var w = t.Replace("w{", "").Replace("}", "");
|
|
|
|
|
|
foreach (var o in list)
|
|
|
|
|
|
{
|
|
|
|
|
|
o.WatchIdList = w;
|
|
|
|
|
|
}
|
2021-07-01 18:41:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_action(list);
|
2021-03-30 17:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-07-01 18:41:35 +08:00
|
|
|
|
//Debug.WriteLine("收到:" + returnData+"\r\n");
|
|
|
|
|
|
//System.IO.File.AppendAllText(System.Environment.CurrentDirectory + "接收到的数据.txt", returnData.Trim().Replace("\0", "")+"\r\n", Encoding.UTF8);
|
2021-03-30 17:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnError(SocketError error)
|
|
|
|
|
|
{
|
|
|
|
|
|
//base.OnError(error);
|
|
|
|
|
|
Debug.WriteLine(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|