예제 #1
0
        /// <summary>
        /// Process network events on this host.
        /// This will send and receive packets as well as handle connections and disconnects.
        /// </summary>
        public void ProcessEvents()
        {
            //
            if (!IsOpen)
            {
                throw new InvalidOperationException("Unable to process network events, local host not open.");
            }

            while (true)
            {
                byte error;
                int  connectionId, channelId, dataSize;
                var  type = NetworkTransport.ReceiveFromHost(Id, out connectionId, out channelId, Buffer, Buffer.Length, out dataSize, out error);

                // Danny: In rare instances, this will be encountered instead of NetworkEventType.Disconnect.
                if ((NetworkError)error == NetworkError.Timeout)
                {
                    Debug.LogError("NetworkHost.ProcessEvents() : Detected Timeout.");
                    type = NetworkEventType.DisconnectEvent;
                }
                else if (error > 0)
                {
                    throw new NetworkException(string.Format("Receive - Host {0}", Id), error);
                }

                if (type == NetworkEventType.Nothing)
                {
                    return;                                    // Exit function
                }
//				if( error > 0 )
//                    Debug.LogFormat( "{4} Event: Host {0} Connection {1} Channel {2} Size {3}", Id, connectionId, channelId, dataSize, type );

                var channel    = _Channels.Values.FirstOrDefault(c => c.Id == channelId);
                var connection = _Connections.Find(c => c.Id == connectionId);

                switch (type)
                {
                case NetworkEventType.BroadcastEvent:

                    // Get remote info
                    string remoteAddress;
                    int    messageSize, remotePort;
                    NetworkTransport.GetBroadcastConnectionInfo(Id, out remoteAddress, out remotePort, out error);
                    if (error > 0)
                    {
                        throw new NetworkException(string.Format("Broadcast - Get Info {0}", Id), error);
                    }

                    // Avoid messages from the local host
                    if (!IsLocalAddress(remoteAddress))
                    {
                        // Get remote message
                        NetworkTransport.GetBroadcastConnectionMessage(Id, Buffer, Buffer.Length, out messageSize, out error);
                        if (error > 0)
                        {
                            throw new NetworkException(string.Format("Broadcast - Get Message {0}", Id), error);
                        }

                        // Extract message
                        var message = new byte[messageSize];
                        Array.Copy(Buffer, message, messageSize);

                        // Create packet object
                        var bPacket = new BroadcastPacket(remoteAddress, remotePort, message);
                        _BroadcastPackets.Enqueue(bPacket);

                        // Too many packets, start clearing them...!
                        if (_BroadcastPackets.Count > MAX_PACKET_QUEUE_SIZE)
                        {
                            Debug.LogWarningFormat("Too many broadcast packets in queue. Discarding oldest packet.");
                            _BroadcastPackets.Dequeue();
                        }
                    }

                    break;

                case NetworkEventType.ConnectEvent:

                    // Create new remote
                    // Will be null if a remote is connecting to the local host
                    // Won't be null if the local host is connecting to a remote host
                    if (connection == null)
                    {
                        connection = new NetworkConnection(this, connectionId);
                        _Connections.Add(connection);
                    }

                    //
                    if (!connection.IsConnected)
                    {
                        connection.OnConnected();
                    }

                    // Trigger Connection Event
                    OnConnect(connection);
                    break;

                case NetworkEventType.DisconnectEvent:

                    Debug.Log("Got disconnect event.");

                    // Remove remote connection
                    if (connection != null)
                    {
                        _Connections.Remove(connection);
                    }
                    else
                    {
                        Debug.LogWarningFormat("Disconnect: {0} wasn't in connections?", connectionId);
                    }

                    // Trigger Disconnection Event
                    OnDisconnect(connection);
                    break;

                case NetworkEventType.DataEvent:

                    // Copy packet from buffer
                    var data = new byte[dataSize];
                    Array.Copy(Buffer, data, dataSize);

                    // Debug.LogFormat( "-- Received a packet ( {0} bytes )", data.Length );

                    // Create packet object
                    var dPacket = new Packet(connection, channel, data);
                    _Packets.Enqueue(dPacket);

                    // Too many packets, start clearing them...!
                    if (_Packets.Count > MAX_PACKET_QUEUE_SIZE)
                    {
                        Debug.LogWarningFormat("Too many packets in queue. Discarding oldest packet.");
                        _Packets.Dequeue();
                    }

                    break;
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Receive any broadcast packets received by the local host.
 /// </summary>
 /// <returns></returns>
 public bool ReceiveBroadcast(out BroadcastPacket packet)
 {
     packet = ReceiveBroadcast();
     return(packet != null);
 }