Exemplo n.º 1
0
        void RecvNetworkData()
        {
            if (platform.RecvPoll(1))
            {
                int         byteReceived = 0;
                UdpEndPoint ep           = UdpEndPoint.Any;

                if (platform.RecvFrom(receiveBuffer, receiveBuffer.Length, ref byteReceived, ref ep))
                {
#if DEBUG
                    if (random.NextDouble() < Config.SimulatedLoss)
                    {
                        UdpLog.Info("simulated loss of packet from {0}", ep.ToString());
                        return;
                    }
#endif
                    UdpConnection cn;

                    if (connLookup.TryGetValue(ep, out cn))
                    {
                        cn.OnPacket(new UdpBitStream(receiveBuffer, byteReceived));
                    }
                    else
                    {
                        RecvUnconnectedPacket(new UdpBitStream(receiveBuffer, byteReceived), ep);
                    }
                }
            }
        }
Exemplo n.º 2
0
        void NetworkLoop()
        {
            while (true)
            {
                try {
                    UdpLog.Info("socket created");
                    while (state == udpSocketState.Created)
                    {
                        ProcessIncommingEvents(true);
                        Thread.Sleep(1);
                    }

                    UdpLog.Info("socket started");
                    while (state == udpSocketState.Running)
                    {
                        RecvDelayedPackets();
                        RecvNetworkData();
                        ProcessTimeouts();
                        ProcessIncommingEvents(false);
                        frame += 1;
                    }

                    UdpLog.Info("socket closed");
                } catch (Exception exn) {
                    UdpLog.Error(exn.ToString());
                }
            }
        }
Exemplo n.º 3
0
        void OnStateConnected(UdpConnectionState oldState)
        {
            if (oldState == UdpConnectionState.Connecting)
            {
                UdpLog.Info("connected to {0}", endpoint.ToString());

                if (IsServer)
                {
                    SendCommand(UdpCommandType.Accepted);
                }

                socket.Raise(UdpEvent.PUBLIC_CONNECTED, this);
            }
        }
Exemplo n.º 4
0
 void OnEventStart(UdpEvent ev)
 {
     if (ChangeState(udpSocketState.Created, udpSocketState.Running))
     {
         if (platform.Bind(ev.EndPoint))
         {
             UdpLog.Info("socket bound to {0}", platform.EndPoint.ToString());
         }
         else
         {
             UdpLog.Error("could not bind socket, platform code: {0}, platform error: {1}", platform.PlatformError.ToString(), platform.PlatformErrorString);
         }
     }
 }
Exemplo n.º 5
0
        void OnEventConnect(UdpEvent ev)
        {
            if (CheckState(UdpSocketState.Running))
            {
                UdpConnection cn = CreateConnection(ev.EndPoint, UdpConnectionMode.Client);

                if (cn == null)
                {
                    UdpLog.Error("could not create connection for endpoint {0}", ev.EndPoint.ToString());
                }
                else
                {
                    UdpLog.Info("connecting to {0}", ev.EndPoint.ToString());
                }
            }
        }
Exemplo n.º 6
0
        bool SendConnectRequest()
        {
            if (connectAttempts < socket.Config.ConnectRequestAttempts)
            {
                if (connectAttempts != 0)
                {
                    UdpLog.Info("retrying connection to {0}", endpoint.ToString());
                }

                SendCommand(UdpCommandType.Connect);

                connectTimeout   = socket.GetCurrentTime() + socket.Config.ConnectRequestTimeout;
                connectAttempts += 1u;
                return(true);
            }

            return(false);
        }
Exemplo n.º 7
0
        void NetworkLoop()
        {
            UdpLog.Info("socket created");
            while (state == udpSocketState.Created)
            {
                ProcessIncommingEvents();
                Thread.Sleep(1);
            }

            UdpLog.Info("socket started");
            while (state == udpSocketState.Running)
            {
                RecvNetworkData();
                ProcessTimeouts();
                ProcessIncommingEvents();
                frame += 1;
            }

            UdpLog.Info("socket closing");
        }
Exemplo n.º 8
0
        void OnEventStart(UdpEvent ev)
        {
            UdpLog.Info("binding socket using platform '{0}'", platform.GetType());

            if (ChangeState(UdpSocketState.Created, UdpSocketState.Running))
            {
                if (platform.Bind(ev.EndPoint))
                {
                    // send started event
                    Raise(UdpEvent.PUBLIC_STARTED, platform.EndPoint);

                    // log that we started
                    UdpLog.Info("socket bound to {0}", platform.EndPoint.ToString());
                }
                else
                {
                    // send started failed event
                    Raise(UdpEvent.PUBLIC_START_FAILED);

                    // log error
                    UdpLog.Error("could not bind socket, platform code: {0}, platform error: {1}", platform.PlatformError.ToString(), platform.PlatformErrorString);
                }
            }
        }