/// <summary> /// Wait until specified status is not set /// </summary> public static void WaitForStatus(this NetPeer peer, NetPeerStatus status) { while (peer.Status != status) { Thread.Sleep(5); } }
/// <summary> /// Waits until specified status in not cleared. /// </summary> public static void WaitForStatusCleared(this NetPeer peer, NetPeerStatus status, TimeSpan maxWait) { var start = DateTime.Now; while (peer.Status == status && (start + maxWait) > DateTime.Now) { Thread.Sleep(5); } }
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 IPEndPoint iep = null; iep = new IPEndPoint(m_configuration.LocalAddress, m_configuration.Port); EndPoint ep = (EndPoint)iep; m_socket = new PlatformSocket(); m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize; m_socket.SendBufferSize = m_configuration.SendBufferSize; m_socket.Blocking = false; m_socket.Bind(ep); m_socket.Setup(); IPEndPoint boundEp = m_socket.LocalEndPoint as IPEndPoint; LogDebug("Socket bound to " + boundEp + ": " + m_socket.IsBound); m_listenPort = boundEp.Port; 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 = new byte[8]; NetRandom.Instance.NextBytes(macBytes); #if IS_FULL_NET_AVAILABLE try { System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress(); if (pa != null) { macBytes = pa.GetAddressBytes(); LogVerbose("Mac address is " + NetUtility.ToHexString(macBytes)); } else { LogWarning("Failed to get Mac address"); } } catch (NotSupportedException) { // not supported; lets just keep the random bytes set above } #endif 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); #if WINDOWS_PHONE SHA1 s = new SHA1Managed(); m_uniqueIdentifier = BitConverter.ToInt64(s.ComputeHash(combined), 0); #else m_uniqueIdentifier = BitConverter.ToInt64(SHA1.Create().ComputeHash(combined), 0); #endif m_status = NetPeerStatus.Running; } }
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.Add(hs); // shut down connections foreach (NetConnection conn in list) conn.Shutdown(m_shutdownReason); } } // one final heartbeat, will send stuff and do disconnect Heartbeat(); lock (m_initializeLock) { try { if (m_socket != null) { // Wrapped this in a try for MonoGame. I think we are bombing here // because there are no clients connecting during tests. try { m_socket.Shutdown(SocketShutdown.Receive); } catch (Exception socketException) { LogDebug("Exception trying to Shutdown Socket: " + socketException.Message); } m_socket.Close(2); // 2 seconds timeout } if (m_messageReceivedEvent != null) { m_messageReceivedEvent.Set(); m_messageReceivedEvent.Close(); m_messageReceivedEvent = null; } } finally { m_socket = null; m_status = NetPeerStatus.NotRunning; LogDebug("Shutdown complete"); } m_receiveBuffer = null; m_sendBuffer = null; m_unsentUnconnectedMessages.Clear(); m_connections.Clear(); m_handshakes.Clear(); } return; }
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 = 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 = new byte[8]; MWCRandom.Instance.NextBytes(macBytes); #if IS_MAC_AVAILABLE try { System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress(); if (pa != null) { macBytes = pa.GetAddressBytes(); LogVerbose("Mac address is " + NetUtility.ToHexString(macBytes)); } else { LogWarning("Failed to get Mac address"); } } catch (NotSupportedException) { // not supported; lets just keep the random bytes set above } #endif IPEndPoint boundEp = m_socket.LocalEndPoint as IPEndPoint; 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.CreateSHA1Hash(combined), 0); m_status = NetPeerStatus.Running; } }
// Can be called from either thread internal void Disconnected() { this.status = NetPeerStatus.Closed; }
private void ExecutePeerShutdown() { VerifyNetworkThread(); LogDebug("Shutting down..."); // disconnect and make one final heartbeat lock (m_connections) { foreach (NetConnection conn in m_connections) conn.Shutdown(m_shutdownReason); } lock (m_handshakes) { foreach (NetConnection conn in m_handshakes.Values) conn.Shutdown(m_shutdownReason); } // one final heartbeat, will send stuff and do disconnect Heartbeat(); lock (m_initializeLock) { try { if (m_socket != null) { m_socket.Shutdown(SocketShutdown.Receive); m_socket.Close(2); // 2 seconds timeout } if (m_messageReceivedEvent != null) { m_messageReceivedEvent.Close(); m_messageReceivedEvent = null; } } finally { m_socket = null; m_status = NetPeerStatus.NotRunning; LogDebug("Shutdown complete"); } m_receiveBuffer = null; m_sendBuffer = null; m_unsentUnconnectedMessages.Clear(); m_connections.Clear(); m_handshakes.Clear(); } return; }
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; }
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 = 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 = new byte[8]; MWCRandom.Instance.NextBytes(macBytes); /*#if IS_MAC_AVAILABLE && !UNITY_WEBPLAYER * try * { * System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress(); * if (pa != null) * { * macBytes = pa.GetAddressBytes(); * LogVerbose("Mac address is " + NetUtility.ToHexString(macBytes)); * } * else * { * LogWarning("Failed to get Mac address"); * } * } * catch (NotSupportedException) * { * // not supported; lets just keep the random bytes set above * } #endif*/ IPEndPoint boundEp = m_socket.LocalEndPoint as IPEndPoint; 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.CreateSHA1Hash(combined), 0); m_status = NetPeerStatus.Running; } }
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.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(); Thread.Sleep(10); lock (m_initializeLock) { try { if (m_socket != null) { try { m_socket.Shutdown(SocketShutdown.Receive); } catch { } m_socket.Close(2); // 2 seconds timeout } if (m_messageReceivedEvent != null) { try { m_messageReceivedEvent.Set(); m_messageReceivedEvent.Close(); } catch (ObjectDisposedException) { // For some reason, inside Godot this seems to throw ObjectDisposedExceptions on client shutdown. // If it's already disposed then I guess this is fine? } finally { m_messageReceivedEvent = null; } } } finally { m_socket = null; m_status = NetPeerStatus.NotRunning; LogDebug("Shutdown complete"); } m_receiveBuffer = null; m_sendBuffer = null; m_unsentUnconnectedMessages.Clear(); m_connections.Clear(); m_handshakes.Clear(); } return; }
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.Add(hs); } } // shut down connections foreach (NetConnection conn in list) { conn.Shutdown(m_shutdownReason); } } } // one final heartbeat, will send stuff and do disconnect Heartbeat(); lock (m_initializeLock) { try { if (m_socket != null) { // Wrapped this in a try for MonoGame. I think we are bombing here // because there are no clients connecting during tests. try { m_socket.Shutdown(SocketShutdown.Receive); } catch (Exception socketException) { LogDebug("Exception trying to Shutdown Socket: " + socketException.Message); } m_socket.Close(2); // 2 seconds timeout } if (m_messageReceivedEvent != null) { m_messageReceivedEvent.Set(); m_messageReceivedEvent.Close(); m_messageReceivedEvent = null; } } finally { m_socket = null; m_status = NetPeerStatus.NotRunning; LogDebug("Shutdown complete"); } m_receiveBuffer = null; m_sendBuffer = null; m_unsentUnconnectedMessages.Clear(); m_connections.Clear(); m_handshakes.Clear(); } return; }
private void InitializeNetwork() { lock (m_initializeLock) { m_configuration.Lock(); if (m_status == NetPeerStatus.Running) { return; } InitializePools(); m_releasedIncomingMessages.Clear(); m_unsentUnconnectedMessages.Clear(); m_handshakes.Clear(); // bind to socket IPEndPoint iep = null; iep = new IPEndPoint(m_configuration.LocalAddress, m_configuration.Port); EndPoint ep = (EndPoint)iep; m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize; m_socket.SendBufferSize = m_configuration.SendBufferSize; m_socket.Blocking = false; m_socket.Bind(ep); IPEndPoint boundEp = m_socket.LocalEndPoint as IPEndPoint; LogDebug("Socket bound to " + boundEp + ": " + m_socket.IsBound); m_listenPort = boundEp.Port; 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 = new byte[8]; NetRandom.Instance.NextBytes(macBytes); #if IS_MAC_AVAILABLE System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress(); if (pa != null) { macBytes = pa.GetAddressBytes(); LogVerbose("Mac address is " + NetUtility.ToHexString(macBytes)); } else { LogWarning("Failed to get Mac address"); } #endif 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(SHA1.Create().ComputeHash(combined), 0); m_status = NetPeerStatus.Running; } }
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.Add(hs); } } // shut down connections foreach (NetConnection conn in list) { conn.Shutdown(m_shutdownReason); } } } // one final heartbeat, will send stuff and do disconnect Heartbeat(); lock (m_initializeLock) { try { if (m_socket != null) { m_socket.Shutdown(SocketShutdown.Receive); m_socket.Close(2); // 2 seconds timeout } if (m_messageReceivedEvent != null) { m_messageReceivedEvent.Close(); m_messageReceivedEvent = null; } } finally { m_socket = null; m_status = NetPeerStatus.NotRunning; LogDebug("Shutdown complete"); } m_receiveBuffer = null; m_sendBuffer = null; m_unsentUnconnectedMessages.Clear(); m_connections.Clear(); m_handshakes.Clear(); } return; }
internal NetPeer( IPEndPoint endPoint, string token, bool isClient, long creationTick) { // Probably no need to pool this class since users may want to hold on // to them after closing and they aren't created all that often anyway this.ClosedByUser = false; this.payloadSeqOut = 0; this.AckRequested = false; this.traffic = new NetTraffic(creationTick); this.outgoing = new Queue<NetEvent>(); this.endPoint = endPoint; this.isClient = isClient; this.token = token; this.notificationSeq = 1; if (isClient) // Client peers are created after a successful connection this.status = NetPeerStatus.Connected; else // Host peers are created in the process of to connecting to them this.status = NetPeerStatus.Connecting; }
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.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(); Thread.Sleep(10); lock (m_initializeLock) { try { if (m_socket != null) { try { m_socket.Shutdown(SocketShutdown.Receive); } catch { } m_socket.Close(2); // 2 seconds timeout } if (m_messageReceivedEvent != null) { m_messageReceivedEvent.Set(); m_messageReceivedEvent.Close(); m_messageReceivedEvent = null; } } finally { m_socket = null; m_status = NetPeerStatus.NotRunning; LogDebug("Shutdown complete"); } m_receiveBuffer = null; m_sendBuffer = null; m_unsentUnconnectedMessages.Clear(); m_connections.Clear(); m_connectionLookup.Clear(); m_handshakes.Clear(); } return; }
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 = 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; } }
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.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(); Thread.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; }
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 IPEndPoint iep = null; iep = new IPEndPoint(m_configuration.LocalAddress, m_configuration.Port); EndPoint ep = (EndPoint)iep; m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize; m_socket.SendBufferSize = m_configuration.SendBufferSize; m_socket.Blocking = false; 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 } IPEndPoint boundEp = m_socket.LocalEndPoint as IPEndPoint; LogDebug("Socket bound to " + boundEp + ": " + m_socket.IsBound); m_listenPort = boundEp.Port; 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 = new byte[8]; NetRandom.Instance.NextBytes(macBytes); #if IS_MAC_AVAILABLE try { System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress(); if (pa != null) { macBytes = pa.GetAddressBytes(); LogVerbose("Mac address is " + NetUtility.ToHexString(macBytes)); } else { LogWarning("Failed to get Mac address"); } } catch (NotSupportedException) { // not supported; lets just keep the random bytes set above } #endif 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(SHA1.Create().ComputeHash(combined), 0); m_status = NetPeerStatus.Running; } }
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 IPEndPoint iep = null; iep = new IPEndPoint(m_configuration.LocalAddress, m_configuration.Port); EndPoint ep = (EndPoint)iep; m_socket = new Socket(iep.AddressFamily, SocketType.Dgram, ProtocolType.Udp); m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize; m_socket.SendBufferSize = m_configuration.SendBufferSize; m_socket.Blocking = false; 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 } IPEndPoint boundEp = m_socket.LocalEndPoint as IPEndPoint; LogDebug("Socket bound to " + boundEp + ": " + m_socket.IsBound); m_listenPort = boundEp.Port; 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 = new byte[8]; NetRandom.Instance.NextBytes(macBytes); #if IS_MAC_AVAILABLE try { System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress(); if (pa != null) { macBytes = pa.GetAddressBytes(); LogVerbose("Mac address is " + NetUtility.ToHexString(macBytes)); } else { LogWarning("Failed to get Mac address"); } } catch (NotSupportedException) { // not supported; lets just keep the random bytes set above } #endif 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(SHA1.Create().ComputeHash(combined), 0); m_status = NetPeerStatus.Running; } }
private void InitializeNetwork() { lock (m_initializeLock) { m_configuration.Lock(); if (m_status == NetPeerStatus.Running) return; InitializePools(); m_releasedIncomingMessages.Clear(); m_unsentUnconnectedMessages.Clear(); m_handshakes.Clear(); // bind to socket IPEndPoint iep = null; iep = new IPEndPoint(m_configuration.LocalAddress, m_configuration.Port); EndPoint ep = (EndPoint)iep; m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize; m_socket.SendBufferSize = m_configuration.SendBufferSize; m_socket.Blocking = false; m_socket.Bind(ep); IPEndPoint boundEp = m_socket.LocalEndPoint as IPEndPoint; LogDebug("Socket bound to " + boundEp + ": " + m_socket.IsBound); m_listenPort = boundEp.Port; 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 = new byte[8]; NetRandom.Instance.NextBytes(macBytes); #if IS_MAC_AVAILABLE System.Net.NetworkInformation.PhysicalAddress pa = NetUtility.GetMacAddress(); if (pa != null) { macBytes = pa.GetAddressBytes(); LogVerbose("Mac address is " + NetUtility.ToHexString(macBytes)); } else { LogWarning("Failed to get Mac address"); } #endif 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(SHA1.Create().ComputeHash(combined), 0); m_status = NetPeerStatus.Running; } }
/// <summary> /// Make sure this is called before exposing the peer to the main thread. /// </summary> internal void Connected() { this.status = NetPeerStatus.Connected; }