Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }