Esempio n. 1
0
 private void HandleDataReceived(INetSocket clientSocket, IBuffer buffer)
 {
     if (buffer.Size > 0)
     {
         ReceiveData();
     }
     OnMessage(this, buffer);
 }
Esempio n. 2
0
 private void HandleDataReceived(INetSocket clientSocket, IBuffer buffer)
 {
     if (buffer.Size > 0)
     {
         ReceiveData();
     }
     OnMessage(this, buffer);
 }
 static INetProtocol ConstructSecureWebsocket(INetSocket socket)
 {
     if (!Server.Config.WebClient)
     {
         return(null);
     }
     return(new SecureSocket(socket));
 }
        /// <summary>
        /// Creates a new <see cref="NetConnection"/> instance with a socket.
        /// </summary>
        /// <param name="socketConnection">Socket</param>
        protected NetConnection(Socket socketConnection)
        {
            this.Id = Guid.NewGuid();

            if (socketConnection != null)
            {
                this.Socket = new NetSocket(socketConnection);
            }
        }
        public SecureSocket(INetSocket socket)
        {
            raw = socket;

            wrapper   = new WrapperStream();
            wrapper.s = this;

            ssl = new SslStream(wrapper);
            new Thread(IOThread).Start();
        }
Esempio n. 6
0
        public NetChannel(INetSocket Socket)
        {
            this.Socket = Socket;
            this.OnMessage = delegate { };
            this.OnFault = delegate { };

            this.Socket.OnReceived = HandleDataReceived;
            this.Socket.OnFault = (socket, exception) => { OnFault(this, exception); };

            //this.certificate = new X509Certificate2(@"C:\Programs\OpenSSL\openssl-1.0.2d\cert.pem", "password");
            //this.certificate.PrivateKey 
        }
Esempio n. 7
0
        public NetChannel(INetSocket Socket)
        {
            this.Socket    = Socket;
            this.OnMessage = delegate { };
            this.OnFault   = delegate { };

            this.Socket.OnReceived = HandleDataReceived;
            this.Socket.OnFault    = (socket, exception) => { OnFault(this, exception); };

            //this.certificate = new X509Certificate2(@"C:\Programs\OpenSSL\openssl-1.0.2d\cert.pem", "password");
            //this.certificate.PrivateKey
        }
Esempio n. 8
0
        private void HandleAccept(INetSocket clientSocket)
        {
            // FIXME: !!!Move into higher API calls outside of NetSocket
            // do we "new-up" channels or maintain existing channels?
            // how many "new" instances of objects will we be creating and is that GC efficient?
            // var buffer = new Buffer(args.Buffer, args.Offset, args.BytesTransferred);
            // var channel = new NetChannel(this, buffer); // ??? do we provide the channel or the buffer to the caller?
            var channel = new NetChannel(clientSocket);
            channel.Fault((c, exception) => { OnFault(exception); });

            StartAccept();
            OnConnect(channel);
        }
Esempio n. 9
0
        internal Player(INetSocket socket)
        {
            Socket = socket;
            SetIP(Socket.IP);

            spamChecker = new SpamChecker(this);
            SessionID   = Interlocked.Increment(ref sessionCounter) & SessionIDMask;

            for (int b = 0; b < BlockBindings.Length; b++)
            {
                BlockBindings[b] = (BlockID)b;
            }
        }
Esempio n. 10
0
        private void HandleAccept(INetSocket clientSocket)
        {
            // FIXME: !!!Move into higher API calls outside of NetSocket
            // do we "new-up" channels or maintain existing channels?
            // how many "new" instances of objects will we be creating and is that GC efficient?
            // var buffer = new Buffer(args.Buffer, args.Offset, args.BytesTransferred);
            // var channel = new NetChannel(this, buffer); // ??? do we provide the channel or the buffer to the caller?
            var channel = new NetChannel(clientSocket);

            channel.Fault((c, exception) => { OnFault(exception); });

            StartAccept();
            OnConnect(channel);
        }
Esempio n. 11
0
        public Player(INetSocket socket, IGameSession session)
        {
            Socket  = socket;
            Session = session;
            SetIP(Socket.IP);

            spamChecker = new SpamChecker(this);
            session.ID  = Interlocked.Increment(ref sessionCounter) & SESSION_ID_MASK;

            for (int b = 0; b < BlockBindings.Length; b++)
            {
                BlockBindings[b] = (BlockID)b;
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Dispose the <see cref="NetConnection"/> resources.
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                }

                if (this.Socket != null)
                {
                    this.Socket.Dispose();
                    this.Socket = null;
                }

                this._disposedValue = true;
            }
        }
Esempio n. 13
0
        private void ExecutePeerShutdown()
        {
            VerifyNetworkThread();

            LogDebug("Shutting down...");

            // disconnect and make one final heartbeat
            var list = new List <NetConnection>(m_handshakes.Count + m_connections.Count);

            lock (m_connections)
            {
                foreach (var conn in m_connections)
                {
                    if (conn != null)
                    {
                        list.Add(conn);
                    }
                }
            }

            lock (m_handshakes)
            {
                foreach (var hs in m_handshakes.Values)
                {
                    if (hs != null && list.Contains(hs) == false)
                    {
                        list.Add(hs);
                    }
                }
            }

            // shut down connections
            foreach (NetConnection conn in list)
            {
                conn.Shutdown(m_shutdownReason);
            }

            FlushDelayedPackets();

            // one final heartbeat, will send stuff and do disconnect
            Heartbeat();

            NetUtility.Sleep(10);

            lock (m_initializeLock)
            {
                try
                {
                    if (m_socket != null)
                    {
                        try
                        {
                            m_socket.Shutdown(SocketShutdown.Receive);
                        }
                        catch (Exception ex)
                        {
                            LogDebug("Socket.Shutdown exception: " + ex.ToString());
                        }

                        try
                        {
                            m_socket.Close(2);                             // 2 seconds timeout
                        }
                        catch (Exception ex)
                        {
                            LogDebug("Socket.Close exception: " + ex.ToString());
                        }
                    }
                }
                finally
                {
                    m_socket = null;
                    m_status = NetPeerStatus.NotRunning;
                    LogDebug("Shutdown complete");

                    // wake up any threads waiting for server shutdown
                    if (m_messageReceivedEvent != null)
                    {
                        m_messageReceivedEvent.Set();
                    }
                }

                m_lastSocketBind = float.MinValue;
                m_receiveBuffer  = null;
                m_sendBuffer     = null;
                m_unsentUnconnectedMessages.Clear();
                m_connections.Clear();
                m_connectionLookup.Clear();
                m_handshakes.Clear();
            }

            return;
        }
Esempio n. 14
0
 static INetProtocol ConstructClassic(INetSocket socket)
 {
     return(new ClassicHandshakeParser(socket));
 }
Esempio n. 15
0
 public ClassicHandshakeParser(INetSocket s)
 {
     socket = s;
 }
Esempio n. 16
0
 public Classic0015Protocol(INetSocket s) : base(s)
 {
     ProtocolVersion = 2;             // made up but less than Server.VERSION_0016
 }
Esempio n. 17
0
        private void BindSocket(bool reBind)
        {
            double now = NetTime.Now;

            if (now - m_lastSocketBind < 1.0)
            {
                LogDebug("Suppressed socket rebind; last bound " + (now - m_lastSocketBind) + " seconds ago");
                return;                 // only allow rebind once every second
            }
            m_lastSocketBind = now;

            using (var mutex = new Mutex(false, "Global\\lidgrenSocketBind"))
            {
                try
                {
                    mutex.WaitOne();

                    if (m_socket == null)
                    {
                        m_socket = new NetSocket(m_configuration.LocalAddress.AddressFamily);
                    }

                    if (reBind)
                    {
                        m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                    }

                    m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize;
                    m_socket.SendBufferSize    = m_configuration.SendBufferSize;
                    m_socket.Blocking          = false;

                    if (m_configuration.DualStack)
                    {
                        if (m_configuration.LocalAddress.AddressFamily != AddressFamily.InterNetworkV6)
                        {
                            LogWarning("Configuration specifies Dual Stack but does not use IPv6 local address; Dual stack will not work.");
                        }
                        else
                        {
                            m_socket.DualMode = true;
                        }
                    }

                    var ep = (EndPoint) new NetEndPoint(m_configuration.LocalAddress, reBind ? m_listenPort : m_configuration.Port);
                    m_socket.Bind(ep);

                    try
                    {
                        const uint IOC_IN            = 0x80000000;
                        const uint IOC_VENDOR        = 0x18000000;
                        uint       SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
                        m_socket.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
                    }
                    catch
                    {
                        // ignore; SIO_UDP_CONNRESET not supported on this platform
                    }
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }

            var boundEp = m_socket.LocalEndPoint as NetEndPoint;

            LogDebug("Socket bound to " + boundEp + ": " + m_socket.IsBound);
            m_listenPort = boundEp.Port;
        }