2020-12-07 21:19:16 +00:00
|
|
|
|
using QuantumUNET.Messages;
|
2020-12-16 09:08:38 +00:00
|
|
|
|
using QuantumUNET.Transport;
|
2020-12-07 21:19:16 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-12-02 12:42:26 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2020-12-04 22:14:53 +00:00
|
|
|
|
namespace QuantumUNET
|
2020-12-02 12:42:26 +00:00
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
internal class QLocalClient : QNetworkClient
|
2020-12-02 12:42:26 +00:00
|
|
|
|
{
|
|
|
|
|
public override void Disconnect()
|
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
QClientScene.HandleClientDisconnect(m_Connection);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
if (m_Connected)
|
|
|
|
|
{
|
|
|
|
|
PostInternalMessage(33);
|
|
|
|
|
m_Connected = false;
|
|
|
|
|
}
|
2021-06-18 22:39:21 +01:00
|
|
|
|
|
2020-12-16 09:08:38 +00:00
|
|
|
|
m_AsyncConnect = ConnectState.Disconnected;
|
2021-12-20 15:11:31 -08:00
|
|
|
|
m_LocalServer.RemoveLocalClient();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void InternalConnectLocalServer(bool generateConnectMsg)
|
|
|
|
|
{
|
|
|
|
|
if (m_FreeMessages == null)
|
|
|
|
|
{
|
|
|
|
|
m_FreeMessages = new Stack<InternalMsg>();
|
|
|
|
|
for (var i = 0; i < 64; i++)
|
|
|
|
|
{
|
|
|
|
|
var t = default(InternalMsg);
|
|
|
|
|
m_FreeMessages.Push(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-18 22:39:21 +01:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
m_LocalServer = QNetworkServer.instance;
|
|
|
|
|
m_Connection = new QULocalConnectionToServer(m_LocalServer);
|
2020-12-16 09:08:38 +00:00
|
|
|
|
SetHandlers(m_Connection);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
m_Connection.connectionId = m_LocalServer.AddLocalClient(this);
|
2020-12-16 09:08:38 +00:00
|
|
|
|
m_AsyncConnect = ConnectState.Connected;
|
|
|
|
|
SetActive(true);
|
|
|
|
|
RegisterSystemHandlers(true);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
if (generateConnectMsg)
|
|
|
|
|
{
|
|
|
|
|
PostInternalMessage(32);
|
|
|
|
|
}
|
2021-06-18 22:39:21 +01:00
|
|
|
|
|
2020-12-02 12:42:26 +00:00
|
|
|
|
m_Connected = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal override void Update() => ProcessInternalMessages();
|
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
internal void AddLocalPlayer(QPlayerController localPlayer)
|
2020-12-02 12:42:26 +00:00
|
|
|
|
{
|
2020-12-21 00:50:24 +01:00
|
|
|
|
Debug.Log($"Local client AddLocalPlayer {localPlayer.Gameobject.name} conn={m_Connection.connectionId}");
|
2020-12-02 12:42:26 +00:00
|
|
|
|
m_Connection.isReady = true;
|
|
|
|
|
m_Connection.SetPlayerController(localPlayer);
|
2020-12-03 11:56:32 +00:00
|
|
|
|
var unetView = localPlayer.UnetView;
|
2020-12-02 12:42:26 +00:00
|
|
|
|
if (unetView != null)
|
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
QClientScene.SetLocalObject(unetView.NetId, localPlayer.Gameobject);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
unetView.SetConnectionToServer(m_Connection);
|
|
|
|
|
}
|
2021-06-18 22:39:21 +01:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
QClientScene.InternalAddPlayer(unetView, localPlayer.PlayerControllerId);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PostInternalMessage(byte[] buffer, int channelId)
|
|
|
|
|
{
|
2020-12-21 00:50:24 +01:00
|
|
|
|
var item = m_FreeMessages.Count == 0 ? default : m_FreeMessages.Pop();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
item.buffer = buffer;
|
|
|
|
|
item.channelId = channelId;
|
|
|
|
|
m_InternalMsgs.Add(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PostInternalMessage(short msgType)
|
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
var networkWriter = new QNetworkWriter();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
networkWriter.StartMessage(msgType);
|
|
|
|
|
networkWriter.FinishMessage();
|
|
|
|
|
PostInternalMessage(networkWriter.AsArray(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessInternalMessages()
|
|
|
|
|
{
|
|
|
|
|
if (m_InternalMsgs.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
var internalMsgs = m_InternalMsgs;
|
|
|
|
|
m_InternalMsgs = m_InternalMsgs2;
|
2020-12-21 00:50:24 +01:00
|
|
|
|
foreach (var msg in internalMsgs)
|
2020-12-02 12:42:26 +00:00
|
|
|
|
{
|
2020-12-03 11:56:32 +00:00
|
|
|
|
if (s_InternalMessage.Reader == null)
|
2020-12-02 12:42:26 +00:00
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
s_InternalMessage.Reader = new QNetworkReader(msg.buffer);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-12-21 00:50:24 +01:00
|
|
|
|
s_InternalMessage.Reader.Replace(msg.buffer);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
}
|
2021-06-18 22:39:21 +01:00
|
|
|
|
|
2020-12-03 11:56:32 +00:00
|
|
|
|
s_InternalMessage.Reader.ReadInt16();
|
2020-12-21 00:50:24 +01:00
|
|
|
|
s_InternalMessage.ChannelId = msg.channelId;
|
2020-12-16 09:08:38 +00:00
|
|
|
|
s_InternalMessage.Connection = connection;
|
2020-12-03 11:56:32 +00:00
|
|
|
|
s_InternalMessage.MsgType = s_InternalMessage.Reader.ReadInt16();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
m_Connection.InvokeHandler(s_InternalMessage);
|
2020-12-21 00:50:24 +01:00
|
|
|
|
m_FreeMessages.Push(msg);
|
2020-12-16 09:08:38 +00:00
|
|
|
|
connection.lastMessageTime = Time.time;
|
2020-12-02 12:42:26 +00:00
|
|
|
|
}
|
2021-06-18 22:39:21 +01:00
|
|
|
|
|
2020-12-02 12:42:26 +00:00
|
|
|
|
m_InternalMsgs = internalMsgs;
|
|
|
|
|
m_InternalMsgs.Clear();
|
2020-12-21 00:50:24 +01:00
|
|
|
|
foreach (var msg in m_InternalMsgs2)
|
2020-12-02 12:42:26 +00:00
|
|
|
|
{
|
2020-12-21 00:50:24 +01:00
|
|
|
|
m_InternalMsgs.Add(msg);
|
2020-12-02 12:42:26 +00:00
|
|
|
|
}
|
2021-06-18 22:39:21 +01:00
|
|
|
|
|
2020-12-02 12:42:26 +00:00
|
|
|
|
m_InternalMsgs2.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
internal void InvokeHandlerOnClient(short msgType, QMessageBase msg, int channelId)
|
2020-12-02 12:42:26 +00:00
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
var networkWriter = new QNetworkWriter();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
networkWriter.StartMessage(msgType);
|
|
|
|
|
msg.Serialize(networkWriter);
|
|
|
|
|
networkWriter.FinishMessage();
|
|
|
|
|
InvokeBytesOnClient(networkWriter.AsArray(), channelId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void InvokeBytesOnClient(byte[] buffer, int channelId) => PostInternalMessage(buffer, channelId);
|
|
|
|
|
|
|
|
|
|
private const int k_InitialFreeMessagePoolSize = 64;
|
|
|
|
|
|
2021-11-20 19:55:54 +00:00
|
|
|
|
private List<InternalMsg> m_InternalMsgs = new();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
|
2021-11-20 19:55:54 +00:00
|
|
|
|
private readonly List<InternalMsg> m_InternalMsgs2 = new();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
|
|
|
|
|
private Stack<InternalMsg> m_FreeMessages;
|
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
private QNetworkServer m_LocalServer;
|
2020-12-02 12:42:26 +00:00
|
|
|
|
|
|
|
|
|
private bool m_Connected;
|
|
|
|
|
|
2021-11-20 19:55:54 +00:00
|
|
|
|
private readonly QNetworkMessage s_InternalMessage = new();
|
2020-12-02 12:42:26 +00:00
|
|
|
|
|
|
|
|
|
private struct InternalMsg
|
|
|
|
|
{
|
|
|
|
|
internal byte[] buffer;
|
|
|
|
|
|
|
|
|
|
internal int channelId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|