Esempio n. 1
0
        void OnEventConnectCancel(UdpEvent ev)
        {
            if (CheckState(UdpSocketState.Running))
            {
                UdpConnection cn;

                if (connLookup.TryGetValue(ev.EndPoint, out cn))
                {
                    // if we are connecting, destroy connection
                    if (cn.CheckState(UdpConnectionState.Connecting))
                    {
                        // notify user thread
                        Raise(UdpEvent.PUBLIC_CONNECT_FAILED, ev.EndPoint);

                        // destroy this connection
                        cn.ChangeState(UdpConnectionState.Destroy);
                    }

                    // if we are connected, disconnect
                    else if (ev.Connection.CheckState(UdpConnectionState.Connected))
                    {
                        ev.Connection.SendCommand(UdpCommandType.Disconnected);
                        ev.Connection.ChangeState(UdpConnectionState.Disconnected);
                    }
                }
            }
        }
Esempio n. 2
0
 void OnEventAccept(UdpEvent ev)
 {
     if (pendingConnections.Remove(ev.EndPoint))
     {
         AcceptConnection(ev.EndPoint);
     }
 }
Esempio n. 3
0
 void OnEventRefuse(UdpEvent ev)
 {
     if (pendingConnections.Remove(ev.EndPoint))
     {
         SendRefusedCommand(ev.EndPoint);
     }
 }
Esempio n. 4
0
        void OnEventClose(UdpEvent ev)
        {
            if (ChangeState(UdpSocketState.Running, UdpSocketState.Shutdown))
            {
                for (int i = 0; i < connList.Count; ++i)
                {
                    UdpConnection cn = connList[i];
                    cn.SendCommand(UdpCommandType.Disconnected);
                    cn.ChangeState(UdpConnectionState.Disconnected);
                }

                if (platform.Close() == false)
                {
                    UdpLog.Error("failed to shutdown socket interface, platform code: {0}", platform.PlatformError.ToString());
                }

                connList.Clear();
                connLookup.Clear();
                eventQueueIn.Clear();
                pendingConnections.Clear();

                GetReadStream().Data      = null;
                GetWriteStream(0, 0).Data = null;
                UdpLog.Error("OnEventClose: {0}", ev.EndPoint.ToString());
            }
        }
Esempio n. 5
0
        internal void Raise(int eventType)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type = eventType;
            Raise(ev);
        }
        public bool Poll(out UdpEvent ev, out UdpSocket socket)
        {
            bool allowRestart = (index != 0);

RESTART:
            for (int i = index; i < sockets.Length; ++i)
            {
                if (sockets[i].Poll(out ev))
                {
                    index  = i + 1;
                    socket = sockets[i];
                    return(true);
                }
            }

            if (allowRestart)
            {
                index        = 0;
                allowRestart = false;
                goto RESTART;
            }

            ev     = default(UdpEvent);
            socket = null;
            return(false);
        }
Esempio n. 7
0
        internal void Raise(int eventType, UdpEndPoint endpoint)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type     = eventType;
            ev.EndPoint = endpoint;
            Raise(ev);
        }
Esempio n. 8
0
 void OnEventDisconect(UdpEvent ev)
 {
     if (ev.Connection.CheckState(UdpConnectionState.Connected))
     {
         ev.Connection.SendCommand(UdpCommandType.Disconnected);
         ev.Connection.ChangeState(UdpConnectionState.Disconnected);
     }
 }
Esempio n. 9
0
        internal void Raise(int eventType, UdpConnection connection)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type       = eventType;
            ev.Connection = connection;
            Raise(ev);
        }
Esempio n. 10
0
        internal void Raise(int eventType, int intval)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type           = eventType;
            ev.OptionIntValue = intval;
            Raise(ev);
        }
Esempio n. 11
0
        internal void Raise(int eventType, UdpConnection connection, object obj)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type       = eventType;
            ev.Connection = connection;
            ev.Object     = obj;
            Raise(ev);
        }
Esempio n. 12
0
 internal void OnEventConnectionOption(UdpEvent ev)
 {
     switch (ev.Option)
     {
     case UdpConnectionOption.AlwaysSendMtu:
         alwaysSendMtu = ev.OptionIntValue == 1;
         break;
     }
 }
Esempio n. 13
0
        internal void Raise(int eventType, UdpEndPoint endpoint, object obj)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type     = eventType;
            ev.EndPoint = endpoint;
            ev.Object   = obj;
            Raise(ev);
        }
Esempio n. 14
0
        internal void Raise(int eventType, UdpConnection connection, UdpConnectionOption option, int value)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type           = eventType;
            ev.Connection     = connection;
            ev.Option         = option;
            ev.OptionIntValue = value;
            Raise(ev);
        }
Esempio n. 15
0
 void OnEventClose(UdpEvent ev)
 {
     if (ChangeState(udpSocketState.Running, udpSocketState.Shutdown))
     {
         if (platform.Close() == false)
         {
             UdpLog.Error("failed to shutdown socket interface, platform code: {0}", platform.PlatformError.ToString());
         }
     }
 }
Esempio n. 16
0
        internal void Raise(int eventType, UdpConnection connection, object obj, UdpSendFailReason reason)
        {
            UdpEvent ev = new UdpEvent();

            ev.Type         = eventType;
            ev.Connection   = connection;
            ev.FailedReason = reason;
            ev.Object       = obj;
            Raise(ev);
        }
Esempio n. 17
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());
                }
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Poll socket for any events
        /// </summary>
        /// <param name="ev">The current event on this socket</param>
        /// <returns>True if a new event is available, False otherwise</returns>
        public bool Poll(ref UdpEvent ev)
        {
            lock (eventQueueOut) {
                if (eventQueueOut.Count > 0)
                {
                    ev = eventQueueOut.Dequeue();
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 19
0
        void ProcessIncommingEvents(bool returnOnStart)
        {
            while (true)
            {
                UdpEvent ev = default(UdpEvent);

                lock (eventQueueIn) {
                    if (eventQueueIn.Count > 0)
                    {
                        ev = eventQueueIn.Dequeue();
                    }
                }

                if (ev.Type == 0)
                {
                    return;
                }

                switch (ev.Type)
                {
                case UdpEvent.INTERNAL_START:
                    OnEventStart(ev);

                    if (returnOnStart)
                    {
                        return;
                    }
                    else
                    {
                        break;
                    }

                case UdpEvent.INTERNAL_CONNECT: OnEventConnect(ev); break;

                case UdpEvent.INTERNAL_CONNECT_CANCEL: OnEventConnectCancel(ev); break;

                case UdpEvent.INTERNAL_ACCEPT: OnEventAccept(ev); break;

                case UdpEvent.INTERNAL_REFUSE: OnEventRefuse(ev); break;

                case UdpEvent.INTERNAL_DISCONNECT: OnEventDisconect(ev); break;

                case UdpEvent.INTERNAL_CLOSE: OnEventClose(ev); break;

                case UdpEvent.INTERNAL_SEND: OnEventSend(ev); break;

                case UdpEvent.INTERNAL_CONNECTION_OPTION: OnEventConnectionOption(ev); break;

                case UdpEvent.INTERNAL_SLEEP: OnEventSleep(ev); break;
                }
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Peek the next event from the socket
        /// </summary>
        /// <param name="ev">The next event on this socket</param>
        /// <returns>True if an event is available, False otherwise</returns>
        public bool Peek(out UdpEvent ev)
        {
            lock (eventQueueOut) {
                if (eventQueueOut.Count > 0)
                {
                    ev = eventQueueOut.Peek();
                    return(true);
                }
            }

            ev = default(UdpEvent);
            return(false);
        }
Esempio n. 21
0
        internal void OnEventConnectionOption(UdpEvent ev)
        {
            switch (ev.Option)
            {
            case UdpConnectionOption.AlwaysSendMtu:
                alwaysSendMtu = ev.OptionIntValue == 1;
                break;

            case UdpConnectionOption.MtuSize:
                mtu = UdpMath.Clamp(ev.OptionIntValue, socket.Config.MtuMin, socket.Config.MtuMax);
                break;
            }
        }
Esempio n. 22
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);
         }
     }
 }
Esempio n. 23
0
 void Raise(UdpEvent ev)
 {
     if (ev.IsInternal)
     {
         lock (eventQueueIn) {
             eventQueueIn.Enqueue(ev);
         }
     }
     else
     {
         lock (eventQueueOut) {
             eventQueueOut.Enqueue(ev);
         }
     }
 }
Esempio n. 24
0
        void OnEventConnectCancel(UdpEvent ev)
        {
            if (CheckState(udpSocketState.Running))
            {
                UdpConnection cn;

                if (connLookup.TryGetValue(ev.EndPoint, out cn))
                {
                    if (cn.CheckState(UdpConnectionState.Connecting))
                    {
                        // notify user thread
                        Raise(UdpEvent.PUBLIC_CONNECT_FAILED, ev.EndPoint);

                        // destroy this connection
                        cn.ChangeState(UdpConnectionState.Destroy);
                    }
                }
            }
        }
Esempio n. 25
0
        public bool Poll(ref UdpEvent ev)
        {
            bool allowRestart = true;

            RESTART:
            for (int i = index; i < sockets.Length; ++i) {
                if (sockets[i].Poll(ref ev)) {
                    index = i + 1;
                    return true;
                }
            }

            if (allowRestart) {
                index = 0;
                allowRestart = false;
                goto RESTART;
            }

            return false;
        }
Esempio n. 26
0
        void Raise(UdpEvent ev)
        {
            if (ev.IsInternal)
            {
                lock (eventQueueIn) {
                    eventQueueIn.Enqueue(ev);
                }
            }
            else
            {
                lock (eventQueueOut) {
                    eventQueueOut.Enqueue(ev);
                }

                if (Config.UseAvailableEventEvent)
                {
                    availableEvent.Set();
                }
            }
        }
Esempio n. 27
0
        public bool Poll(ref UdpEvent ev)
        {
            bool allowRestart = true;

RESTART:
            for (int i = index; i < sockets.Length; ++i)
            {
                if (sockets[i].Poll(ref ev))
                {
                    index = i + 1;
                    return(true);
                }
            }

            if (allowRestart)
            {
                index        = 0;
                allowRestart = false;
                goto RESTART;
            }

            return(false);
        }
Esempio n. 28
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);
                }
            }
        }
Esempio n. 29
0
 void OnEventConnectionOption(UdpEvent ev)
 {
     ev.Connection.OnEventConnectionOption(ev);
 }
Esempio n. 30
0
 void OnEventSleep(UdpEvent ev)
 {
     UdpLog.Debug("sleeping network thread for {0} ms", ev.OptionIntValue);
     Thread.Sleep(ev.OptionIntValue);
 }
Esempio n. 31
0
 void OnEventSend(UdpEvent ev)
 {
     ev.Connection.SendObject(ev.Object);
 }
Esempio n. 32
0
        internal void OnEventConnectionOption(UdpEvent ev)
        {
            switch (ev.Option) {
                case UdpConnectionOption.AlwaysSendMtu:
                    alwaysSendMtu = ev.OptionIntValue == 1;
                    break;

                case UdpConnectionOption.MtuSize:
                    mtu = UdpMath.Clamp(ev.OptionIntValue, socket.Config.MtuMin, socket.Config.MtuMax);
                    break;
            }
        }