internal bool ActuallySendPacket(byte[] data, int numBytes, NetEndPoint target, out bool connectionReset) { connectionReset = false; IPAddress ba = default(IPAddress); try { ba = NetUtility.GetCachedBroadcastAddress(); // TODO: refactor this check outta here if (target.Address.Equals(ba)) { // Some networks do not allow // a global broadcast so we use the BroadcastAddress from the configuration // this can be resolved to a local broadcast addresss e.g 192.168.x.255 targetCopy.Address = m_configuration.BroadcastAddress; targetCopy.Port = target.Port; m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); } else if (m_configuration.DualStack && m_configuration.LocalAddress.AddressFamily == AddressFamily.InterNetworkV6) { NetUtility.CopyEndpoint(target, targetCopy); //Maps to IPv6 for Dual Mode } int bytesSent = m_socket.SendTo(data, 0, numBytes, SocketFlags.None, targetCopy); if (numBytes != bytesSent) { LogWarning("Failed to send the full " + numBytes + "; only " + bytesSent + " bytes sent in packet!"); } // LogDebug("Sent " + numBytes + " bytes"); } catch (SocketException sx) { if (sx.SocketErrorCode == SocketError.WouldBlock) { // send buffer full? LogWarning("Socket threw exception; would block - send buffer full? Increase in NetPeerConfiguration"); return(false); } if (sx.SocketErrorCode == SocketError.ConnectionReset) { // connection reset by peer, aka connection forcibly closed aka "ICMP port unreachable" connectionReset = true; return(false); } LogError("Failed to send packet: " + sx); } catch (Exception ex) { LogError("Failed to send packet: " + ex); } finally { if (target.Address.Equals(ba)) { m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false); } } return(true); }
// // Release - just send the packet straight away // internal void SendPacket(int numBytes, NetEndPoint target, int numMessages, out bool connectionReset) { #if USE_RELEASE_STATISTICS m_statistics.PacketSent(numBytes, numMessages); #endif connectionReset = false; IPAddress ba = default(IPAddress); try { // TODO: refactor this check outta here ba = NetUtility.GetCachedBroadcastAddress(); if (target.Address == ba) { m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); } int bytesSent = m_socket.SendTo(m_sendBuffer, 0, numBytes, SocketFlags.None, target); if (numBytes != bytesSent) { LogWarning("Failed to send the full " + numBytes + "; only " + bytesSent + " bytes sent in packet!"); } } catch (SocketException sx) { if (sx.SocketErrorCode == SocketError.WouldBlock) { // send buffer full? LogWarning("Socket threw exception; would block - send buffer full? Increase in NetPeerConfiguration"); return; } if (sx.SocketErrorCode == SocketError.ConnectionReset) { // connection reset by peer, aka connection forcibly closed aka "ICMP port unreachable" connectionReset = true; return; } LogError("Failed to send packet: " + sx); } catch (Exception ex) { LogError("Failed to send packet: " + ex); } finally { if (target.Address == ba) { m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false); } } return; }