Пример #1
0
        // ============= Internal Threads =================

        void InternalUpdate()
        {
            List <EndPoint> toRemove = new List <EndPoint>();

            while (state == State.Bound)
            {
                toRemove.Clear();
                foreach (Connection conn in Connections.Values())
                {
                    if (conn.LastSeen.AddSeconds(MaxTimeout) < DateTime.Now)
                    {
                        Send(conn.Conn, SignatureMessage.disconnectSigMes);
                        OnDisconnect?.Invoke(conn);
                        toRemove.Add(conn.Conn);
                    }
                }

                for (int i = 0; i < toRemove.Count; ++i)
                {
                    Connections.Remove(toRemove[i]);
                }

                Thread.Sleep(1000);
#if SLEEPY_STATS
                stats.Flush();
#endif
            }
        }
Пример #2
0
        void InternalConnect()
        {
            try
            {
                client.Connect(IP, Port);
                _Connecting = false;

                client.NoDelay     = NoDelay;
                client.SendTimeout = SendTimeout;

                sendThread = new Thread(() => { SendLoop(this); });
                sendThread.IsBackground = true;
                sendThread.Start();

                ReceiveLoop(this);
            }
            catch (SocketException exception)
            {
                Log.WriteNow("Client Recv: failed to connect to ip=" + IP + " port=" + Port + " reason=" + exception);
                OnDisconnect?.Invoke();
            }
            catch (ThreadInterruptedException) { }
            catch (ThreadAbortException) { }
            catch (Exception exception)
            {
                Log.WriteNow("Client Recv Exception: " + exception);
            }

            sendThread?.Interrupt();

            _Connecting = false;

            client?.Close();
        }
Пример #3
0
 void InternalOnConnect()
 {
     if (AutoSetupEncryption)
     {
         SetupEncryption();
     }
     OnConnect?.Invoke();
 }
Пример #4
0
        // ============== Processing Data ===============

        void RecvLoop()
        {
            try
            {
                byte[]   buffer = new byte[MaxBufferSize];
                EndPoint from   = serverEndPoint;

                while (state != State.Terminated)
                {
                    int len = socket.ReceiveFrom(buffer, ref from);
                    lastSeen = DateTime.Now;

#if SLEEPY_STATS
                    //Log.Write($"RECV: {len}");
                    stats.RecvBytesTotal += (ulong)len;
                    stats.RecvTotal++;
#endif

                    if (len == SignatureMessage.connMesLen)
                    {
                        SignatureMessage sigMes = SignatureMessage.Deserialize(buffer);
                        if (sigMes.IsValid)
                        {
                            if (sigMes.Connect)
                            {
                                state = State.Connected;
                                OnConnect?.Invoke();
                            }
                            else
                            {
                                ForceDisconnect(false);
                            }
                            return;
                        }
                    }

                    OnPacket(buffer, len);
                }
            }
            catch (ThreadAbortException) { }
            catch (ThreadInterruptedException) { }
            catch (SocketException) { /* Noramlly WSA Blcoking Call Canceled when closing */ }
            catch (Exception e)
            {
                Log.Write("RecvLoop Exception On Client, reason: " + e.Message + "\n<b>Stack:</b> " + e.StackTrace);
            }
        }
Пример #5
0
        public void ForceDisconnect(bool notifyServer = true)
        {
            if (state != State.Connected)
            {
                return;
            }
            state = State.Disconnecting;

            if (notifyServer)
            {
                Send(SignatureMessage.disconnectSigMes);
            }
            OnDisconnect?.Invoke();

            CleanupThread();
            state = State.ReadyToConnect;
        }
Пример #6
0
 private void OnConnectionSucess(string message)
 {
     ConnectionMessage?.Invoke(this, message);
 }
Пример #7
0
        // =============== Process Data ===================

        void RecvLoop()
        {
            try
            {
                byte[]   buffer = new byte[MaxBufferSize];
                EndPoint from   = serverEndPoint;
                while (state != State.Terminated)
                {
                    int len = socket.ReceiveFrom(buffer, ref from);

                    Connections.TryGetValue(from, out Connection conn);

#if SLEEPY_STATS
                    //Log.Write($"RECV: {len}");
                    stats.RecvBytesTotal += (ulong)len;
                    stats.RecvTotal++;
#endif

                    conn?.UpdateLastSeen();

                    if (len == SignatureMessage.connMesLen)
                    {
                        SignatureMessage sigMes = SignatureMessage.Deserialize(buffer);
                        if (sigMes.IsValid)
                        {
                            if (sigMes.Connect) // client Asking to connect
                            {
                                if (conn == null)
                                {
                                    conn = new Connection(from);
                                    Connections.Add(from, conn);
                                    Send(conn.Conn, SignatureMessage.connectSigMes);
                                    OnConnect?.Invoke(conn);
                                }
                            }
                            else // client asking to disconnect
                            {
                                if (conn != null)
                                {
                                    Connections.Remove(from);
                                    Send(conn.Conn, SignatureMessage.disconnectSigMes);
                                    OnDisconnect?.Invoke(conn);
                                }
                            }
                            continue;
                        }
                    }

                    if (conn != null)
                    {
                        OnPacket(conn, buffer, len);
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (ThreadInterruptedException) { }
            catch (SocketException) { /* Noramlly WSA Blcoking Call Canceled when closing */ }
            catch (Exception e)
            {
                Log.Write("RecvLoop Exception On Server, reason: " + e.Message + "\n<b>Stack:</b> " + e.StackTrace);
            }
        }
Пример #8
0
 void InternalOnDisconnect()
 {
     OnDisconnect?.Invoke();
 }