using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using System;
|
using System.Text;
|
using System.IO;
|
|
public class NetPkgCtl
|
{
|
private static List<NetPackage> packages = new List<NetPackage>(); //发送接收封包存储列表
|
private static List<NetPackage> tempPackages = new List<NetPackage>();//暂停时缓存的封包列表
|
public static bool stopRecieve = false;
|
|
public static void RecordPackage(ServerType socketType, string bytes, NetPackagetType packType, string packageName, string fields, List<string> fieldDetails)
|
{
|
#if UNITY_EDITOR
|
var package = new NetPackage();
|
package.socketType = socketType;
|
package.time = DateTime.Now.ToString("HH:mm:ss:fff");
|
package.type = packType;
|
|
if (string.IsNullOrEmpty(packageName))
|
{
|
package.content = bytes.Replace(",", " ");
|
var byteStr = package.content.Split(' ');
|
if (byteStr.Length > 1)
|
{
|
package.name = StringUtility.Contact("H", byteStr[0], byteStr[1], "(未注册)");
|
}
|
}
|
else
|
{
|
package.content = bytes.Replace(",", " ");
|
package.name = packageName;
|
package.fields = fields;
|
package.fieldDetails = fieldDetails;
|
}
|
|
if (!stopRecieve)
|
{
|
packages.Add(package);
|
}
|
else
|
{
|
tempPackages.Add(package);
|
}
|
#endif
|
}
|
|
public static List<NetPackage> GetPackages()
|
{
|
return packages;
|
}
|
|
public static List<NetPackage> GetTempPackages()
|
{
|
return tempPackages;
|
}
|
|
public static void WriteAllNetLog(bool @details)
|
{
|
if (packages != null)
|
{
|
var count = 0;
|
var lines = new List<string>();
|
for (int i = packages.Count - 1; i >= 0; i--)
|
{
|
if (count > 20000)
|
{
|
break;
|
}
|
|
var package = packages[i];
|
var line = string.Empty;
|
line = StringUtility.Contact(package.type == NetPackagetType.Client ? "【发送】\t" : "【接收】\t", package.time, ":", package.name, @details ? "\r\n" : "-->" + package.fields);
|
|
if (@details && package.fieldDetails != null)
|
{
|
for (int j = 0; j < package.fieldDetails.Count; j++)
|
{
|
line = StringUtility.Contact(line, "\t\t\t", package.fieldDetails[j], "\r\n");
|
}
|
}
|
|
lines.Add(line);
|
count++;
|
}
|
|
File.WriteAllLines(Application.dataPath + "/PackageLogs_" + (@details ? "details" : "abstract") + "_" + DateTime.Now.ToString("HH_mm_ss") + ".txt", lines.ToArray());
|
}
|
}
|
}
|
|
public class NetPackage
|
{
|
public ServerType socketType;
|
public bool show = false;
|
public string time;
|
public string serverTime;
|
public string content;
|
public string name;
|
public NetPackagetType type;
|
public string fields;
|
public List<string> fieldDetails;
|
}
|
|
public enum NetPackagetType
|
{
|
Client = 1,
|
Server = 2,
|
All = 3,
|
}
|