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
More NAT stuff
Added NetConnection.Owner
This commit is contained in:
@@ -79,6 +79,11 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IPEndPoint RemoteEndpoint { get { return m_remoteEndpoint; } }
|
public IPEndPoint RemoteEndpoint { get { return m_remoteEndpoint; } }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the owning NetPeer instance
|
||||||
|
/// </summary>
|
||||||
|
public NetPeer Owner { get { return m_owner; } }
|
||||||
|
|
||||||
internal NetConnection(NetPeer owner, IPEndPoint remoteEndpoint)
|
internal NetConnection(NetPeer owner, IPEndPoint remoteEndpoint)
|
||||||
{
|
{
|
||||||
m_owner = owner;
|
m_owner = owner;
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ namespace Lidgren.Network
|
|||||||
string token = tmp.ReadString();
|
string token = tmp.ReadString();
|
||||||
bool isHost = (hostByte != 0);
|
bool isHost = (hostByte != 0);
|
||||||
|
|
||||||
|
LogDebug("NAT introduction received; we are designated " + (isHost ? "host" : "client"));
|
||||||
|
|
||||||
NetOutgoingMessage punch;
|
NetOutgoingMessage punch;
|
||||||
|
|
||||||
if (!isHost && m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess) == false)
|
if (!isHost && m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess) == false)
|
||||||
@@ -56,21 +58,13 @@ namespace Lidgren.Network
|
|||||||
// send internal punch
|
// send internal punch
|
||||||
punch = CreateMessage(1);
|
punch = CreateMessage(1);
|
||||||
punch.Write(hostByte);
|
punch.Write(hostByte);
|
||||||
if (hostByte == 0)
|
|
||||||
{
|
|
||||||
// only client needs to send token
|
|
||||||
punch.Write(token);
|
punch.Write(token);
|
||||||
}
|
|
||||||
SendUnconnectedLibraryMessage(punch, NetMessageLibraryType.NatPunchMessage, remoteInternal);
|
SendUnconnectedLibraryMessage(punch, NetMessageLibraryType.NatPunchMessage, remoteInternal);
|
||||||
|
|
||||||
// send external punch
|
// send external punch
|
||||||
punch = CreateMessage(1);
|
punch = CreateMessage(1);
|
||||||
punch.Write(hostByte);
|
punch.Write(hostByte);
|
||||||
if (hostByte == 0)
|
|
||||||
{
|
|
||||||
// only client needs to send token
|
|
||||||
punch.Write(token);
|
punch.Write(token);
|
||||||
}
|
|
||||||
SendUnconnectedLibraryMessage(punch, NetMessageLibraryType.NatPunchMessage, remoteExternal);
|
SendUnconnectedLibraryMessage(punch, NetMessageLibraryType.NatPunchMessage, remoteExternal);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,11 +76,17 @@ namespace Lidgren.Network
|
|||||||
NetIncomingMessage tmp = new NetIncomingMessage(m_receiveBuffer, 1000); // never mind length
|
NetIncomingMessage tmp = new NetIncomingMessage(m_receiveBuffer, 1000); // never mind length
|
||||||
tmp.Position = (ptr * 8);
|
tmp.Position = (ptr * 8);
|
||||||
|
|
||||||
byte hostByte = tmp.ReadByte();
|
byte fromHostByte = tmp.ReadByte();
|
||||||
if (hostByte != 0)
|
if (fromHostByte == 0)
|
||||||
|
{
|
||||||
|
// it's from client
|
||||||
|
LogDebug("NAT punch received from " + senderEndpoint + " we're host, so we ignore this");
|
||||||
return; // don't alert hosts about nat punch successes; only clients
|
return; // don't alert hosts about nat punch successes; only clients
|
||||||
|
}
|
||||||
string token = tmp.ReadString();
|
string token = tmp.ReadString();
|
||||||
|
|
||||||
|
LogDebug("NAT punch received from " + senderEndpoint + " we're client, so we've succeeded - token is " + token);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Release punch success to client; enabling him to Connect() to msg.SenderIPEndPoint if token is ok
|
// Release punch success to client; enabling him to Connect() to msg.SenderIPEndPoint if token is ok
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ namespace Lidgren.Network
|
|||||||
internal Socket m_socket;
|
internal Socket m_socket;
|
||||||
internal byte[] m_macAddressBytes;
|
internal byte[] m_macAddressBytes;
|
||||||
private int m_listenPort;
|
private int m_listenPort;
|
||||||
private readonly AutoResetEvent m_messageReceivedEvent = new AutoResetEvent(false);
|
private AutoResetEvent m_messageReceivedEvent = new AutoResetEvent(false);
|
||||||
|
|
||||||
private readonly NetQueue<NetIncomingMessage> m_releasedIncomingMessages = new NetQueue<NetIncomingMessage>(16);
|
private readonly NetQueue<NetIncomingMessage> m_releasedIncomingMessages = new NetQueue<NetIncomingMessage>(16);
|
||||||
private readonly NetQueue<NetOutgoingMessage> m_unsentUnconnectedMessage = new NetQueue<NetOutgoingMessage>(4);
|
private readonly NetQueue<NetOutgoingMessage> m_unsentUnconnectedMessage = new NetQueue<NetOutgoingMessage>(4);
|
||||||
@@ -178,7 +178,10 @@ namespace Lidgren.Network
|
|||||||
m_socket.Close(2); // 2 seconds timeout
|
m_socket.Close(2); // 2 seconds timeout
|
||||||
}
|
}
|
||||||
if (m_messageReceivedEvent != null)
|
if (m_messageReceivedEvent != null)
|
||||||
|
{
|
||||||
m_messageReceivedEvent.Close();
|
m_messageReceivedEvent.Close();
|
||||||
|
m_messageReceivedEvent = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -400,25 +403,17 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
VerifyNetworkThread();
|
VerifyNetworkThread();
|
||||||
|
|
||||||
if (libType != NetMessageLibraryType.Connect && libType != NetMessageLibraryType.Discovery && libType != NetMessageLibraryType.DiscoveryResponse)
|
|
||||||
{
|
|
||||||
LogWarning("Received unconnected library message of type " + libType);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int payloadLengthBytes = NetUtility.BytesToHoldBits(payloadLengthBits);
|
int payloadLengthBytes = NetUtility.BytesToHoldBits(payloadLengthBits);
|
||||||
|
|
||||||
//
|
switch (libType)
|
||||||
// Handle nat introduction
|
|
||||||
//
|
|
||||||
if (libType == NetMessageLibraryType.NatIntroduction)
|
|
||||||
HandleNatIntroduction(ptr);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle Discovery
|
|
||||||
//
|
|
||||||
if (libType == NetMessageLibraryType.Discovery)
|
|
||||||
{
|
{
|
||||||
|
case NetMessageLibraryType.NatPunchMessage:
|
||||||
|
HandleNatPunch(ptr, senderEndpoint);
|
||||||
|
break;
|
||||||
|
case NetMessageLibraryType.NatIntroduction:
|
||||||
|
HandleNatIntroduction(ptr);
|
||||||
|
break;
|
||||||
|
case NetMessageLibraryType.Discovery:
|
||||||
if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.DiscoveryRequest))
|
if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.DiscoveryRequest))
|
||||||
{
|
{
|
||||||
NetIncomingMessage dm = CreateIncomingMessage(NetIncomingMessageType.DiscoveryRequest, payloadLengthBytes);
|
NetIncomingMessage dm = CreateIncomingMessage(NetIncomingMessageType.DiscoveryRequest, payloadLengthBytes);
|
||||||
@@ -428,11 +423,9 @@ namespace Lidgren.Network
|
|||||||
dm.m_senderEndpoint = senderEndpoint;
|
dm.m_senderEndpoint = senderEndpoint;
|
||||||
ReleaseMessage(dm);
|
ReleaseMessage(dm);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (libType == NetMessageLibraryType.DiscoveryResponse)
|
break;
|
||||||
{
|
case NetMessageLibraryType.DiscoveryResponse:
|
||||||
if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.DiscoveryResponse))
|
if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.DiscoveryResponse))
|
||||||
{
|
{
|
||||||
NetIncomingMessage dr = CreateIncomingMessage(NetIncomingMessageType.DiscoveryResponse, payloadLengthBytes);
|
NetIncomingMessage dr = CreateIncomingMessage(NetIncomingMessageType.DiscoveryResponse, payloadLengthBytes);
|
||||||
@@ -442,17 +435,15 @@ namespace Lidgren.Network
|
|||||||
dr.m_senderEndpoint = senderEndpoint;
|
dr.m_senderEndpoint = senderEndpoint;
|
||||||
ReleaseMessage(dr);
|
ReleaseMessage(dr);
|
||||||
}
|
}
|
||||||
return;
|
break;
|
||||||
}
|
|
||||||
|
case NetMessageLibraryType.Connect:
|
||||||
|
|
||||||
//
|
|
||||||
// Handle NetMessageLibraryType.Connect
|
|
||||||
//
|
|
||||||
|
|
||||||
if (!m_configuration.m_acceptIncomingConnections)
|
if (!m_configuration.m_acceptIncomingConnections)
|
||||||
{
|
{
|
||||||
LogWarning("Connect received; but we're not accepting incoming connections!");
|
LogWarning("Connect received; but we're not accepting incoming connections!");
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
string appIdent;
|
string appIdent;
|
||||||
@@ -485,21 +476,21 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
// malformed connect packet
|
// malformed connect packet
|
||||||
LogWarning("Malformed connect packet from " + senderEndpoint + " - " + ex.ToString());
|
LogWarning("Malformed connect packet from " + senderEndpoint + " - " + ex.ToString());
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (appIdent.Equals(m_configuration.AppIdentifier) == false)
|
if (appIdent.Equals(m_configuration.AppIdentifier) == false)
|
||||||
{
|
{
|
||||||
// wrong app ident
|
// wrong app ident
|
||||||
LogWarning("Connect received with wrong appidentifier (need '" + m_configuration.AppIdentifier + "' found '" + appIdent + "') from " + senderEndpoint);
|
LogWarning("Connect received with wrong appidentifier (need '" + m_configuration.AppIdentifier + "' found '" + appIdent + "') from " + senderEndpoint);
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ok, someone wants to connect to us, and we're accepting connections!
|
// ok, someone wants to connect to us, and we're accepting connections!
|
||||||
if (m_connections.Count >= m_configuration.MaximumConnections)
|
if (m_connections.Count >= m_configuration.MaximumConnections)
|
||||||
{
|
{
|
||||||
HandleServerFull(senderEndpoint);
|
HandleServerFull(senderEndpoint);
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetConnection conn = new NetConnection(this, senderEndpoint);
|
NetConnection conn = new NetConnection(this, senderEndpoint);
|
||||||
@@ -511,11 +502,15 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
// do connection approval before accepting this connection
|
// do connection approval before accepting this connection
|
||||||
AddPendingConnection(conn, approval);
|
AddPendingConnection(conn, approval);
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
AcceptConnection(conn);
|
AcceptConnection(conn);
|
||||||
return;
|
break;
|
||||||
|
default:
|
||||||
|
LogWarning("Received unconnected library message of type " + libType);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleUnconnectedUserMessage(int ptr, int payloadLengthBits, IPEndPoint senderEndpoint)
|
private void HandleUnconnectedUserMessage(int ptr, int payloadLengthBits, IPEndPoint senderEndpoint)
|
||||||
|
|||||||
Reference in New Issue
Block a user