1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-18 08:06:33 +09:00

Nat introduction added; sample added (not finished yet)

This commit is contained in:
lidgren
2010-05-10 09:43:45 +00:00
parent 0810dfc1c5
commit 66802928b6
12 changed files with 269 additions and 5 deletions

View File

@@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}</ProjectGuid>
<OutputType>Library</OutputType>
@@ -65,6 +65,7 @@
<Compile Include="NetIncomingMessage.Write.cs" />
<Compile Include="NetIncomingMessageType.cs" />
<Compile Include="NetMessageType.cs" />
<Compile Include="NetNatIntroduction.cs" />
<Compile Include="NetOutgoingMessage.cs" />
<Compile Include="NetOutgoingMessage.Write.cs" />
<Compile Include="NetPeer.ConnectionApproval.cs" />

View File

@@ -446,7 +446,6 @@ namespace Lidgren.Network
/// <summary>
/// Writes an endpoint description
/// </summary>
/// <param name="endPoint"></param>
internal void Write(IPEndPoint endPoint)
{
byte[] bytes = endPoint.Address.GetAddressBytes();

View File

@@ -41,5 +41,6 @@ namespace Lidgren.Network
DebugMessage = 1 << 8, // Data (string)
WarningMessage = 1 << 9, // Data (string)
ErrorMessage = 1 << 10, // Data (string)
NatIntroduction = 1 << 11, // IPEndPoint
}
}

View File

@@ -48,7 +48,8 @@ namespace Lidgren.Network
Disconnect = 8,
Discovery = 9,
DiscoveryResponse = 10,
NatIntroduction = 11,
NatPunchMessage = 11,
NatIntroduction = 12,
}
internal enum NetMessageType : byte

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Net;
namespace Lidgren.Network
{
public partial class NetPeer
{
public void Introduce(IPEndPoint host, IPEndPoint client)
{
// send message to client
NetOutgoingMessage msg = CreateMessage(10);
msg.Write(false);
msg.WritePadBits();
msg.Write(host);
SendUnconnectedLibraryMessage(msg, NetMessageLibraryType.NatIntroduction, client);
// send message to host
msg = CreateMessage(10);
msg.Write(true);
msg.WritePadBits();
msg.Write(client);
SendUnconnectedLibraryMessage(msg, NetMessageLibraryType.NatIntroduction, host);
}
private void HandleNatIntroduction(int ptr, IPEndPoint senderEndpoint)
{
VerifyNetworkThread();
// read intro
NetIncomingMessage tmp = new NetIncomingMessage(m_receiveBuffer, 1000); // never mind length
tmp.Position = (ptr * 8);
bool isHost = (tmp.ReadByte() == 0 ? false : true);
IPEndPoint ep = tmp.ReadIPEndpoint();
// quickly; send nat punch
NetOutgoingMessage punch = CreateMessage(0);
SendUnconnectedLibraryMessage(punch, NetMessageLibraryType.NatPunchMessage, ep);
if (!isHost)
{
NetIncomingMessage intro = CreateIncomingMessage(NetIncomingMessageType.NatIntroduction, 10);
intro.Write(ep);
intro.m_senderEndpoint = senderEndpoint;
ReleaseMessage(intro);
}
}
}
}

View File

@@ -509,8 +509,7 @@ namespace Lidgren.Network
/// <summary>
/// Writes an endpoint description
/// </summary>
/// <param name="endPoint"></param>
internal void Write(IPEndPoint endPoint)
public void Write(IPEndPoint endPoint)
{
byte[] bytes = endPoint.Address.GetAddressBytes();
Write((byte)bytes.Length);

View File

@@ -418,6 +418,12 @@ namespace Lidgren.Network
int payloadLengthBytes = NetUtility.BytesToHoldBits(payloadLengthBits);
//
// Handle nat introduction
//
if (libType == NetMessageLibraryType.NatIntroduction)
HandleNatIntroduction(ptr, senderEndpoint);
//
// Handle Discovery
//