Esempio n. 1
0
        public void Update()
        {
            if (!NetworkTransport.IsStarted)
            {
                return;
            }

            while (true)
            {
                int recConnectionID;
                int recChannelID;
                int dataLength;

                byte             error;
                NetworkEventType recNetworkEvent = NetworkTransport.ReceiveFromHost(hostID, out recConnectionID, out recChannelID, dataBuffer, dataBuffer.Length, out dataLength, out error);

                if (recNetworkEvent == NetworkEventType.Nothing)
                {
                    break;
                }

                NetEvent networkEvent = new NetEvent();
                networkEvent.netInterface = this;
                networkEvent.connectionID = recConnectionID;
                networkEvent.channelID    = recChannelID;
                networkEvent.eventType    = recNetworkEvent;
                networkEvent.error        = (NetworkError)error;

                if (networkEvent.eventType == NetworkEventType.DataEvent)
                {
                    networkEvent.data = new byte[dataLength];
                    System.Array.Copy(dataBuffer, networkEvent.data, dataLength);
                }
                eventQueue.Add(networkEvent);
            }

            foreach (var networkEvent in eventQueue)
            {
                if (networkEvent.error != NetworkError.Ok)
                {
                    Debug.LogError("NetworkEvent Error: " + networkEvent.error + " " + hostID + " " + connectionID);
                }

                if (networkEvent.eventType == NetworkEventType.ConnectEvent)
                {
                    isConnected = true;

                    try
                    {
                        onConnected?.Invoke(networkEvent);
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }
                else if (networkEvent.eventType == NetworkEventType.DisconnectEvent)
                {
                    try
                    {
                        onDisconnected?.Invoke(networkEvent);
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }

                    isConnected  = false;
                    connectionID = -1;
                    foreach (var channelInfo in channelNameToInfoDict.Values)
                    {
                        channelInfo.id = -1;
                    }
                }
                else if (networkEvent.eventType == NetworkEventType.DataEvent)
                {
                    MemoryStream stream = new MemoryStream(networkEvent.data);
                    BinaryReader reader = new BinaryReader(stream);

                    NetPacket packet = NetPacket.ReadNext(reader);
                    packet.receivedTime = Time.time;
                    packet.networkEvent = networkEvent;

                    reader.Close();
                    stream.Close();

                    try
                    {
                        onPacket?.Invoke(packet);
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }
            }
            eventQueue.Clear();
        }
Esempio n. 2
0
 protected override void OnConnect(NetEvent networkEvent)
 {
     connectedClientIDs.Add(networkEvent.connectionID);
 }
Esempio n. 3
0
 protected override void OnDisconnect(NetEvent networkEvent)
 {
     connectedClientIDs.Remove(networkEvent.connectionID);
 }
Esempio n. 4
0
 protected virtual void OnDisconnect(NetEvent networkEvent)
 {
 }