282 lines
8.1 KiB
C#
282 lines
8.1 KiB
C#
using AOT;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Runtime.InteropServices;
|
||
using TEngine;
|
||
using UnityEngine;
|
||
|
||
public class KuaiShouClient : MonoBehaviour
|
||
{
|
||
|
||
/// <summary>
|
||
/// <20>汾id
|
||
/// </summary>
|
||
int version = 1;
|
||
|
||
|
||
/// <summary>
|
||
/// IPC<50>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||
/// </summary>
|
||
public static bool IsConnect
|
||
{
|
||
get; private set;
|
||
}
|
||
|
||
// IPC·<43><C2B7>
|
||
string IPCPath;
|
||
|
||
// ˽<>˷<EFBFBD><CBB7><EFBFBD>Code
|
||
string PreviewRoomCode;
|
||
|
||
static IntPtr ipcHandle;
|
||
|
||
|
||
public delegate void _OnDataReceived(string content, uint len, IntPtr ptr);
|
||
public delegate void _OnConnected(IntPtr ptr);
|
||
public delegate void _OnDisconnect(IntPtr ptr);
|
||
public delegate void _OnLog(string content, uint len, IntPtr ptr);
|
||
static Queue<string> msgQueue = new Queue<string>();
|
||
|
||
|
||
void Start()
|
||
{
|
||
Debug.Log("?!!!!!!!!!KuaiShouClient");
|
||
|
||
string[] commandLineArgs = System.Environment.GetCommandLineArgs();
|
||
var CommandLineArgs = new Dictionary<string, string>();
|
||
|
||
for (int i = 0; i < commandLineArgs.Length; i++)
|
||
{
|
||
if (commandLineArgs[i] == "-c" || commandLineArgs[i] == "-pos" || commandLineArgs[i] == "-ipc")
|
||
{
|
||
CommandLineArgs[commandLineArgs[i]] = commandLineArgs[i + 1];
|
||
}
|
||
}
|
||
|
||
foreach (var item in CommandLineArgs)
|
||
{
|
||
Debug.Log(item);
|
||
}
|
||
|
||
if (CommandLineArgs.TryGetValue("-c", out string str))
|
||
{
|
||
PreviewRoomCode = str;
|
||
}
|
||
|
||
if (CommandLineArgs.TryGetValue("-ipc", out string str1))
|
||
{
|
||
IPCPath = str1;
|
||
}
|
||
|
||
IsConnect = false;
|
||
|
||
if (!string.IsNullOrEmpty(IPCPath))
|
||
{
|
||
ipcHandle = new IntPtr(GetHashCode());
|
||
|
||
IpcInterface.SetDataReceivedCallback(OnDataReceived, ipcHandle);
|
||
IpcInterface.SetConnectedCallback(OnConnected, ipcHandle);
|
||
IpcInterface.SetDisconnectCallback(OnDisconnect, ipcHandle);
|
||
IpcInterface.SetLogCallback(OnLog, ipcHandle);
|
||
int result = IpcInterface.InitIpc(IPCPath, (uint)IPCPath.Length, IpcInterface.IPC_CS_TYPE.CLIENT);
|
||
|
||
|
||
if (result == 0)
|
||
{
|
||
IsConnect = true;
|
||
//EventMgr.FireEvent(TEventType.IPCStateChange);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("IPC result:" + result.ToString());
|
||
}
|
||
}
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
// lock (msgQueue)
|
||
{
|
||
if (msgQueue.Count > 0)
|
||
{
|
||
while (msgQueue.Count > 0)
|
||
{
|
||
ParseMsg(msgQueue.Dequeue());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>Ϣ
|
||
/// </summary>
|
||
/// <param name="content"></param>
|
||
void ParseMsg(string content)
|
||
{
|
||
Dictionary<string, object> msg = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
|
||
|
||
if (msg != null)
|
||
{
|
||
if (msg.TryGetValue("type", out var t))
|
||
{
|
||
Debug.Log("type:" + t);
|
||
}
|
||
Dictionary<string, object> data = null;
|
||
|
||
if (msg.TryGetValue("data", out var d))
|
||
{
|
||
data = JsonConvert.DeserializeObject<Dictionary<string, object>>(d.ToString());
|
||
}
|
||
|
||
switch (t)
|
||
{
|
||
case "SC_SET_CODE":
|
||
if (data != null && data.TryGetValue("code", out var code))
|
||
{
|
||
Debug.Log("SC_SET_CODE code:" + code.ToString());
|
||
string oldCode = PreviewRoomCode;
|
||
PreviewRoomCode = code.ToString();
|
||
GameEvent.Send("SetRoomId", PreviewRoomCode);
|
||
}
|
||
break;
|
||
case "SC_QUIT":
|
||
if (data != null && data.TryGetValue("message", out var message))
|
||
{
|
||
Debug.Log("SC_QUIT msg:" + message);
|
||
}
|
||
Application.Quit();
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public void SendMsg(string msgType, Dictionary<string, object> msgData)
|
||
{
|
||
if (ipcHandle != IntPtr.Zero)
|
||
{
|
||
Dictionary<string, object> data = new Dictionary<string, object>()
|
||
{
|
||
{ "type" ,msgType },
|
||
{ "data" , msgData },
|
||
{ "version" , version }
|
||
};
|
||
string msg = JsonUtility.ToJson(data);
|
||
int result = IpcInterface.SendData(msg, (uint)msg.Length);
|
||
if (result == -1)
|
||
{
|
||
Debug.LogError("IPC<50><43>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("IPC <20><>ʼ<EFBFBD><CABC>ʧ<EFBFBD><CAA7>");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
|
||
/// </summary>
|
||
/// <param name="content"></param>
|
||
/// <param name="len"></param>
|
||
/// <param name="intPtr"></param>
|
||
[MonoPInvokeCallback(typeof(_OnDataReceived))]
|
||
static void OnDataReceived(IntPtr contentPtr, uint len, IntPtr ptr)
|
||
{
|
||
string content = Marshal.PtrToStringUTF8(contentPtr, (int)len);
|
||
if (string.IsNullOrEmpty(content))
|
||
{
|
||
Debug.Log("ipc call func : OnDataReceived ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString() + " len" + len.ToString() + " content: is null or empty");
|
||
return;
|
||
}
|
||
|
||
Debug.Log("ipc call func : OnDataReceived ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString() + " len" + len.ToString() + " content:" + content);
|
||
|
||
if (ptr == ipcHandle)
|
||
{
|
||
// lock (msgQueue)
|
||
// {
|
||
msgQueue.Enqueue(content);
|
||
// }
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("ipc call func : OnDataReceived ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString() + " len" + len.ToString() + " content:" + content + " 不匹配");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20><><EFBFBD><EFBFBD>
|
||
/// </summary>
|
||
/// <param name="ptr"></param>
|
||
[MonoPInvokeCallback(typeof(_OnConnected))]
|
||
static void OnConnected(IntPtr ptr)
|
||
{
|
||
Debug.Log("ipc call func : OnConnected ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString());
|
||
if (ptr == ipcHandle)
|
||
{
|
||
Debug.Log("ipc conneted");
|
||
IsConnect = true;
|
||
//EventMgr.FireEvent(TEventType.IPCStateChange);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD>
|
||
/// </summary>
|
||
/// <param name="ptr"></param>
|
||
[MonoPInvokeCallback(typeof(_OnDisconnect))]
|
||
static void OnDisconnect(IntPtr ptr)
|
||
{
|
||
// Debug.Log("ipc call func : OnDisconnect ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString());
|
||
if (ptr == ipcHandle)
|
||
{
|
||
IsConnect = false;
|
||
//EventMgr.FireEvent(TEventType.IPCStateChange);
|
||
Debug.Log(message: "ipc disconneted");
|
||
//<2F>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD>Ӻ<EFBFBD><D3BA>Զ<EFBFBD><D4B6>ر<EFBFBD><D8B1><EFBFBD>Ϸ
|
||
Application.Quit();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20>յ<EFBFBD><D5B5><EFBFBD>־
|
||
/// </summary>
|
||
/// <param name="content"></param>
|
||
/// <param name="len"></param>
|
||
/// <param name="ptr"></param>
|
||
[MonoPInvokeCallback(typeof(_OnLog))]
|
||
static void OnLog(IntPtr contentPtr, uint len, IntPtr ptr)
|
||
{
|
||
string content = Marshal.PtrToStringUTF8(contentPtr, (int)len);
|
||
// Debug.Log("ipc call func : OnLog ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString() + " len" + len.ToString() + " content:" + content);
|
||
|
||
//if (ptr == ipcHandle)
|
||
//{
|
||
// Debug.Log("ipc log:" + content);
|
||
//}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20>ͷ<EFBFBD>ipc<70><63>Ϣ
|
||
/// </summary>
|
||
public void ReleaseIPC()
|
||
{
|
||
if (ipcHandle != IntPtr.Zero)
|
||
{
|
||
ipcHandle = IntPtr.Zero;
|
||
IpcInterface.ReleaseIpc();
|
||
}
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
ReleaseIPC();
|
||
}
|
||
}
|