You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-19 00:26:30 +09:00
- Patch from MonoGame; makes the BroadcastAddress configurable and changes text encoding from ASCII to UTF8, required for Android support
This commit is contained in:
@@ -26,6 +26,7 @@ Global
|
|||||||
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||||
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Release|x86.ActiveCfg = Release|Any CPU
|
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|Any CPU.ActiveCfg = Debug|x86
|
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|Any CPU.ActiveCfg = Debug|x86
|
||||||
|
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|Any CPU.Build.0 = Debug|x86
|
||||||
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
|
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
|
||||||
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|Mixed Platforms.Build.0 = Debug|x86
|
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|Mixed Platforms.Build.0 = Debug|x86
|
||||||
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|x86.ActiveCfg = Debug|x86
|
{6691874A-1766-4A08-A72A-B1132FAB8E58}.Debug|x86.ActiveCfg = Debug|x86
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public NetXorEncryption(string key)
|
public NetXorEncryption(string key)
|
||||||
{
|
{
|
||||||
m_key = Encoding.ASCII.GetBytes(key);
|
m_key = Encoding.UTF8.GetBytes(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ namespace Lidgren.Network
|
|||||||
/// String to hash for key
|
/// String to hash for key
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public NetXtea(string key)
|
public NetXtea(string key)
|
||||||
: this(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(key)), 32)
|
: this(SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(key)), 32)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#define IS_MAC_AVAILABLE
|
#if !__ANDROID__ && !IOS
|
||||||
|
#define IS_MAC_AVAILABLE
|
||||||
|
#endif
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|||||||
@@ -131,7 +131,13 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
// TODO: refactor this check outta here
|
// TODO: refactor this check outta here
|
||||||
if (target.Address == IPAddress.Broadcast)
|
if (target.Address == IPAddress.Broadcast)
|
||||||
|
{
|
||||||
|
// 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
|
||||||
|
target.Address = m_configuration.BroadcastAddress;
|
||||||
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
||||||
|
}
|
||||||
|
|
||||||
int bytesSent = m_socket.SendTo(data, 0, numBytes, SocketFlags.None, target);
|
int bytesSent = m_socket.SendTo(data, 0, numBytes, SocketFlags.None, target);
|
||||||
if (numBytes != bytesSent)
|
if (numBytes != bytesSent)
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ namespace Lidgren.Network
|
|||||||
private readonly string m_appIdentifier;
|
private readonly string m_appIdentifier;
|
||||||
private string m_networkThreadName;
|
private string m_networkThreadName;
|
||||||
private IPAddress m_localAddress;
|
private IPAddress m_localAddress;
|
||||||
|
private IPAddress m_broadcastAddress;
|
||||||
internal bool m_acceptIncomingConnections;
|
internal bool m_acceptIncomingConnections;
|
||||||
internal int m_maximumConnections;
|
internal int m_maximumConnections;
|
||||||
internal int m_defaultOutgoingMessageCapacity;
|
internal int m_defaultOutgoingMessageCapacity;
|
||||||
@@ -76,6 +77,12 @@ namespace Lidgren.Network
|
|||||||
m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated;
|
m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated;
|
||||||
m_networkThreadName = "Lidgren network thread";
|
m_networkThreadName = "Lidgren network thread";
|
||||||
m_localAddress = IPAddress.Any;
|
m_localAddress = IPAddress.Any;
|
||||||
|
m_broadcastAddress = IPAddress.Broadcast;
|
||||||
|
var ip = NetUtility.GetBroadcastAddress();
|
||||||
|
if (ip != null)
|
||||||
|
{
|
||||||
|
m_broadcastAddress = ip;
|
||||||
|
}
|
||||||
m_port = 0;
|
m_port = 0;
|
||||||
m_receiveBufferSize = 131071;
|
m_receiveBufferSize = 131071;
|
||||||
m_sendBufferSize = 131071;
|
m_sendBufferSize = 131071;
|
||||||
@@ -285,6 +292,17 @@ namespace Lidgren.Network
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IPAddress BroadcastAddress
|
||||||
|
{
|
||||||
|
get { return m_broadcastAddress; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (m_isLocked)
|
||||||
|
throw new NetException(c_isLockedMessage);
|
||||||
|
m_broadcastAddress = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the local port to bind to. Defaults to 0. Cannot be changed once NetPeer is initialized.
|
/// Gets or sets the local port to bind to. Defaults to 0. Cannot be changed once NetPeer is initialized.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
var sha = GetHashAlgorithm();
|
var sha = GetHashAlgorithm();
|
||||||
|
|
||||||
byte[] tmp = Encoding.ASCII.GetBytes(username + ":" + password);
|
byte[] tmp = Encoding.UTF8.GetBytes(username + ":" + password);
|
||||||
byte[] innerHash = sha.ComputeHash(tmp);
|
byte[] innerHash = sha.ComputeHash(tmp);
|
||||||
|
|
||||||
byte[] total = new byte[innerHash.Length + salt.Length];
|
byte[] total = new byte[innerHash.Length + salt.Length];
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ namespace Lidgren.Network
|
|||||||
"MAN:\"ssdp:discover\"\r\n" +
|
"MAN:\"ssdp:discover\"\r\n" +
|
||||||
"MX:3\r\n\r\n";
|
"MX:3\r\n\r\n";
|
||||||
|
|
||||||
byte[] arr = System.Text.Encoding.ASCII.GetBytes(str);
|
byte[] arr = System.Text.Encoding.UTF8.GetBytes(str);
|
||||||
|
|
||||||
peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
||||||
peer.RawSend(arr, 0, arr.Length, new IPEndPoint(IPAddress.Broadcast, 1900));
|
peer.RawSend(arr, 0, arr.Length, new IPEndPoint(IPAddress.Broadcast, 1900));
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRA
|
|||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
#if !__ANDROID__ && !IOS
|
||||||
#define IS_FULL_NET_AVAILABLE
|
#define IS_FULL_NET_AVAILABLE
|
||||||
|
#endif
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@@ -257,11 +259,94 @@ namespace Lidgren.Network
|
|||||||
return new string(c);
|
return new string(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IPAddress GetBroadcastAddress()
|
||||||
|
{
|
||||||
|
#if __ANDROID__
|
||||||
|
try{
|
||||||
|
Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Android.App.Application.Context.GetSystemService(Android.App.Activity.WifiService);
|
||||||
|
if (wifi.IsWifiEnabled)
|
||||||
|
{
|
||||||
|
var dhcp = wifi.DhcpInfo;
|
||||||
|
|
||||||
|
int broadcast = (dhcp.IpAddress & dhcp.Netmask) | ~dhcp.Netmask;
|
||||||
|
byte[] quads = new byte[4];
|
||||||
|
for (int k = 0; k < 4; k++)
|
||||||
|
{
|
||||||
|
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
|
||||||
|
}
|
||||||
|
return new IPAddress(quads);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch // Catch Access Denied Errors
|
||||||
|
{
|
||||||
|
return IPAddress.Broadcast;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if IS_FULL_NET_AVAILABLE
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NetworkInterface ni = GetNetworkInterface();
|
||||||
|
if (ni == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPInterfaceProperties properties = ni.GetIPProperties();
|
||||||
|
foreach (UnicastIPAddressInformation unicastAddress in properties.UnicastAddresses)
|
||||||
|
{
|
||||||
|
if (unicastAddress != null && unicastAddress.Address != null && unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||||
|
{
|
||||||
|
var mask = unicastAddress.IPv4Mask;
|
||||||
|
byte[] ipAdressBytes = unicastAddress.Address.GetAddressBytes();
|
||||||
|
byte[] subnetMaskBytes = mask.GetAddressBytes();
|
||||||
|
|
||||||
|
if (ipAdressBytes.Length != subnetMaskBytes.Length)
|
||||||
|
throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
|
||||||
|
|
||||||
|
byte[] broadcastAddress = new byte[ipAdressBytes.Length];
|
||||||
|
for (int i = 0; i < broadcastAddress.Length; i++)
|
||||||
|
{
|
||||||
|
broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
|
||||||
|
}
|
||||||
|
return new IPAddress(broadcastAddress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch // Catch any errors
|
||||||
|
{
|
||||||
|
return IPAddress.Broadcast;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return IPAddress.Broadcast;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets my local IP address (not necessarily external) and subnet mask
|
/// Gets my local IP address (not necessarily external) and subnet mask
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static IPAddress GetMyAddress(out IPAddress mask)
|
public static IPAddress GetMyAddress(out IPAddress mask)
|
||||||
{
|
{
|
||||||
|
mask = null;
|
||||||
|
#if __ANDROID__
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Android.App.Application.Context.GetSystemService(Android.App.Activity.WifiService);
|
||||||
|
if (!wifi.IsWifiEnabled) return null;
|
||||||
|
var dhcp = wifi.DhcpInfo;
|
||||||
|
|
||||||
|
int addr = dhcp.IpAddress;
|
||||||
|
byte[] quads = new byte[4];
|
||||||
|
for (int k = 0; k < 4; k++)
|
||||||
|
{
|
||||||
|
quads[k] = (byte) ((addr >> k * 8) & 0xFF);
|
||||||
|
}
|
||||||
|
return new IPAddress(quads);
|
||||||
|
}
|
||||||
|
catch // Catch Access Denied errors
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
#if IS_FULL_NET_AVAILABLE
|
#if IS_FULL_NET_AVAILABLE
|
||||||
NetworkInterface ni = GetNetworkInterface();
|
NetworkInterface ni = GetNetworkInterface();
|
||||||
if (ni == null)
|
if (ni == null)
|
||||||
@@ -280,7 +365,6 @@ namespace Lidgren.Network
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
mask = null;
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user