123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Networking.Transport;
- using KairoEngine.Core;
- using Sirenix.OdinInspector;
- namespace KairoEngine.Multiplayer
- {
- [HideMonoScript]
- public class ServerNetMsgController : NetMsgController
- {
- public ServerBehaviour server;
- public NetworkTick networkTick;
-
- private List<List<NetMsg>> sendMsgBuffer = new List<List<NetMsg>>();
- private List<List<NetMsg>> receivedMsgBuffer = new List<List<NetMsg>>();
- private void Start()
- {
- GetNetMessageTypes();
- networkTick.eventStreamName = server.eventStreamName;
- for (int i = 0; i < server.players; i++)
- {
- sendMsgBuffer.Add(new List<NetMsg>());
- receivedMsgBuffer.Add(new List<NetMsg>());
- }
- }
- private void OnEnable()
- {
- GenericEvents.StartListening($"{server.eventStreamName}_OnTick", OnTick);
- GenericEvents.StartListening($"{server.eventStreamName}_Listening", ServerStarted);
- }
- private void OnDisable()
- {
- GenericEvents.StopListening($"{server.eventStreamName}_OnTick", OnTick);
- GenericEvents.StopListening($"{server.eventStreamName}_Listening", ServerStarted);
- }
- private void ServerStarted(string text)
- {
- networkTick.StartTickSystem();
- }
-
- public override void ReceiveData(ref DataStreamReader stream, ServerBehaviour server, int clientId)
- {
- uint tick = stream.ReadUInt();
- uint msgCount = stream.ReadByte();
- //Debug.Log($"Receiving {msgCount} messages from client {clientId}");
- for (int i = 0; i < msgCount; i++)
- {
- uint code = stream.ReadByte();
- NetMsg typeMsg = FindWithCode(code);
- if(typeMsg != null)
- {
- NetMsg msg = typeMsg.ReceiveMessage(server, ref stream, clientId);
- if(msg != null)
- {
- receivedMsgBuffer[clientId].Add(msg);
- }
- }
- else Debug.LogError($"Could not find NetMsg with code {code}");
- }
- }
- public override void SendData(uint code, NetMsg netMsg, ServerBehaviour server, int clientId)
- {
- sendMsgBuffer[clientId].Add(netMsg);
- }
- private void OnTick(int tick)
- {
- SendBufferedData();
- ReadBufferedData();
- }
- private void SendBufferedData()
- {
- if(!server.m_Driver.IsCreated) return;
- for (int i = 0; i < sendMsgBuffer.Count; i++)
- {
- if(sendMsgBuffer[i].Count == 0) continue;
- if(!server.m_Connections[i].IsCreated) continue;
- DataStreamWriter writer = new DataStreamWriter();
- server.m_Driver.BeginSend(server.m_Connections[i], out writer);
- writer.WriteUInt((uint)networkTick.tick); // Current Tick Header
- writer.WriteByte((byte)sendMsgBuffer[i].Count); // Message count Header
- for (int a = 0; a < sendMsgBuffer[i].Count; a++)
- {
- NetMsg netMsg = sendMsgBuffer[i][a];
- FindWithCode(netMsg.code).SendMessage(server, netMsg, ref writer, i);
- }
- server.m_Driver.EndSend(writer);
- sendMsgBuffer[i].Clear();
- }
- }
- private void ReadBufferedData()
- {
- for (int i = 0; i < receivedMsgBuffer.Count; i++)
- {
- for (int a = 0; a < receivedMsgBuffer[i].Count; a++)
- {
- NetMsg msg = receivedMsgBuffer[i][a];
- msg.ReadMessage(server, i);
- }
- receivedMsgBuffer[i].Clear();
- }
- }
- }
- }
|