private void InitializeNetwork()
        {
            lock (m_initializeLock)
            {
                // make sure this is properly set up again, if required.

                // start network thread if not running
                NetPeerManager.StartNetworkThread();

                m_configuration.Lock();

                if (m_status == NetPeerStatus.Running)
                {
                    return;
                }

                if (m_configuration.m_enableUPnP)
                {
                    m_upnp = new NetUPnP(this);
                }

                InitializePools();

                m_releasedIncomingMessages.Clear();
                m_unsentUnconnectedMessages.Clear();

                _handshakeManager.Handshakes.Clear();

                // bind to socket
                BindSocket(false);

                m_receiveBuffer            = new byte[m_configuration.ReceiveBufferSize];
                m_sendBuffer               = new byte[m_configuration.SendBufferSize];
                m_readHelperMessage        = new NetIncomingMessage(NetIncomingMessageType.Error);
                m_readHelperMessage.m_data = m_receiveBuffer;

                byte[] macBytes = NetUtility.GetMacAddressBytes();

                var    boundEp  = m_socket.LocalEndPoint as NetEndPoint;
                byte[] epBytes  = BitConverter.GetBytes(boundEp.GetHashCode());
                byte[] combined = new byte[epBytes.Length + macBytes.Length];
                Array.Copy(epBytes, 0, combined, 0, epBytes.Length);
                Array.Copy(macBytes, 0, combined, epBytes.Length, macBytes.Length);
                m_uniqueIdentifier = BitConverter.ToInt64(NetUtility.ComputeSHAHash(combined), 0);

                m_status = NetPeerStatus.Running;
            }
        }
Example #2
0
        private void InitializeNetwork()
        {
            lock (m_initializeLock)
            {
                m_configuration.Lock();

                if (m_status == NetPeerStatus.Running)
                {
                    return;
                }

                if (m_configuration.m_enableUPnP)
                {
                    m_upnp = new NetUPnP(this);
                }

                InitializePools();

                m_releasedIncomingMessages.Clear();
                m_unsentUnconnectedMessages.Clear();
                m_handshakes.Clear();

                // bind to socket
                BindSocket(false);

                m_receiveBuffer = ArrayPool <byte> .Shared.Rent(m_configuration.ReceiveBufferSize);

                m_sendBuffer = ArrayPool <byte> .Shared.Rent(m_configuration.SendBufferSize);

                m_readHelperMessage = new NetIncomingMessage(NetIncomingMessageType.Error)
                {
                    m_buf = m_receiveBuffer
                };

                var macBytes = NetUtility.GetMacAddressBytes() ?? Array.Empty <byte>();

                var boundEp  = m_socket.LocalEndPoint as NetEndPoint;
                var hashCode = boundEp !.GetHashCode();
                var epBytes  = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref hashCode, 1));
                // ReSharper disable once SuggestVarOrType_Elsewhere
                Span <byte> combined = stackalloc byte[epBytes.Length + macBytes.Length];
                //Array.Copy(epBytes, 0, combined, 0, epBytes.Length);
                epBytes.CopyTo(combined.Slice(0, epBytes.Length));
                //Array.Copy(macBytes, 0, combined, epBytes.Length, macBytes.Length);
                macBytes.CopyTo(combined.Slice(epBytes.Length, macBytes.Length));
                m_uniqueIdentifier = MemoryMarshal.Cast <byte, long>(NetUtility.ComputeSHAHash(combined, stackalloc byte[32]))[0];