Esempio n. 1
0
        //protected abstract void OnServerReceive(byte[] data);

        //protected abstract void ServerSend(object target, byte[] data);

        //protected abstract void ServerBroadcast(byte[] data);

        #endregion

        #region CLIENT STUFF

        protected void ClientSendSerializable(byte[] data)
        {
            if (Initialized)
            {
                NetworkRouter.Send(InternalId, data); // TODO change to NetworkRouter.Send(identity.Id, data);
            }
        }
Esempio n. 2
0
        public void Update()
        {
            try
            {
                var polled = false;
                // poll loop to retrieve more than one data per frame
                while (!polled)
                {
                    if (_host.CheckEvents(out var netEvent) <= 0)
                    {
                        if (_host.Service(0, out netEvent) <= 0)
                        {
                            break;
                        }
                        polled = true;
                    }

                    var data = new ServerEventData(netEvent.ChannelID, netEvent.Peer.ID, netEvent.Peer.IP);

                    switch (netEvent.Type)
                    {
                    case EventType.Connect:
                        AddPeer(netEvent.Peer);
                        OnConnect?.Invoke(data);
                        NetworkRouter.PeerConnection(data.PeerId);
                        break;

                    case EventType.Disconnect:
                        RemovePeer(netEvent.Peer);
                        OnDisconnect?.Invoke(data);
                        NetworkRouter.PeerDisconnection(data.PeerId);
                        break;

                    case EventType.Timeout:
                        RemovePeer(netEvent.Peer);
                        OnTimeout?.Invoke(data);
                        NetworkRouter.PeerTimeout(data.PeerId);
                        break;

                    case EventType.Receive:
                        var buffer = new byte[netEvent.Packet.Length];
                        netEvent.Packet.CopyTo(buffer);
                        netEvent.Packet.Dispose();
                        data.Content = buffer;
                        OnReceive?.Invoke(data);
                        NetworkRouter.PeerReceive(data.PeerId, data.ChannelId, data.Content);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                UnityEngine.Debug.Log("ERROR Update : " + e);
            }
        }
Esempio n. 3
0
        public void Update()
        {
            if (!_host.IsSet)
            {
                return;
            }

            var polled = false;

            // poll loop to retrieve more than one data per frame
            while (!polled)
            {
                if (_host.CheckEvents(out var netEvent) <= 0)
                {
                    if (_host.Service(0, out netEvent) <= 0)
                    {
                        break;
                    }
                    polled = true;
                }

                var data = new ClientEventData(netEvent.ChannelID, netEvent.Peer.ID);

                switch (netEvent.Type)
                {
                case EventType.Connect:
                    OnConnect?.Invoke(data);
                    NetworkRouter.PeerConnection(data.PeerId);
                    break;

                case EventType.Disconnect:
                    OnDisconnect?.Invoke(data);
                    NetworkRouter.PeerDisconnection(data.PeerId);
                    break;

                case EventType.Timeout:
                    OnTimeout?.Invoke(data);
                    NetworkRouter.PeerTimeout(data.PeerId);
                    break;

                case EventType.Receive:
                    // Get the byte[] from the netEvent.Packet
                    var buffer = new byte[netEvent.Packet.Length];
                    netEvent.Packet.CopyTo(buffer);
                    netEvent.Packet.Dispose();
                    data.Content = buffer;

                    OnReceive?.Invoke(data);
                    NetworkRouter.PeerReceive(data.PeerId, data.ChannelId, data.Content);
                    break;
                }
            }
        }
Esempio n. 4
0
        private void StartClientNetwork()
        {
            _isClient = true;
            Client    = new ClientNetwork();

            Client.OnConnect += data => Debug.Log($"Connected to server {data.PeerId}");
//            Client.OnReceive += data => Debug.Log($"Received message from server {data.PeerId}");

            NetworkRouter.RegisterByChannel(ChannelType.SNetIdentity, SpawnMessageHeader, SpawnEntity);

            Client.Create();

            if (connectClientOnStart)
            {
                Client.Connect(networkAddress, networkPort, maxChannels);
            }
        }
Esempio n. 5
0
        public void Initialize(string id)
        {
            // TODO Initialize Called when Spawn Message is received on the client side
            Register(id);
            if (!IsServer)
            {
                return;
            }

            var trans = transform;
            // TODO Send Spawn Message to Clients
            var msg = new ObjectSpawnMessage
            {
                Id       = id,
                AssetId  = AssetId,
                SceneId  = SceneId,
                Position = trans.position,
                Rotation = trans.rotation
            };

            Debug.Log($"Sending spawn message : {msg.Id}, {msg.AssetId}, {msg.SceneId}");
            NetworkRouter.SendByChannel(ChannelType.SNetIdentity, SNetManager.SpawnMessageHeader, msg);
        }
Esempio n. 6
0
 public Rip2Protocol(NetworkRouter router, LoggingModule logger) : base(router, logger)
 {
 }
Esempio n. 7
0
 protected GenericProtocol(NetworkRouter router, LoggingModule logger)
 {
     _router = router;
     _logger = logger;
 }
Esempio n. 8
0
 protected void NetworkRouterRegister(RouterCallback callback)
 {
     NetworkRouter.Register(InternalId, callback); // TODO change to NetworkRouter.Register(??identity??, callback);  (V)_(;,,;)_(V)
 }