You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-19 08:36:34 +09:00
Nat introduction added; sample added (not finished yet)
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
<ProductVersion>9.0.21022</ProductVersion>
|
<ProductVersion>9.0.30729</ProductVersion>
|
||||||
<SchemaVersion>2.0</SchemaVersion>
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
<ProjectGuid>{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}</ProjectGuid>
|
<ProjectGuid>{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}</ProjectGuid>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
@@ -65,6 +65,7 @@
|
|||||||
<Compile Include="NetIncomingMessage.Write.cs" />
|
<Compile Include="NetIncomingMessage.Write.cs" />
|
||||||
<Compile Include="NetIncomingMessageType.cs" />
|
<Compile Include="NetIncomingMessageType.cs" />
|
||||||
<Compile Include="NetMessageType.cs" />
|
<Compile Include="NetMessageType.cs" />
|
||||||
|
<Compile Include="NetNatIntroduction.cs" />
|
||||||
<Compile Include="NetOutgoingMessage.cs" />
|
<Compile Include="NetOutgoingMessage.cs" />
|
||||||
<Compile Include="NetOutgoingMessage.Write.cs" />
|
<Compile Include="NetOutgoingMessage.Write.cs" />
|
||||||
<Compile Include="NetPeer.ConnectionApproval.cs" />
|
<Compile Include="NetPeer.ConnectionApproval.cs" />
|
||||||
|
|||||||
@@ -446,7 +446,6 @@ namespace Lidgren.Network
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an endpoint description
|
/// Writes an endpoint description
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="endPoint"></param>
|
|
||||||
internal void Write(IPEndPoint endPoint)
|
internal void Write(IPEndPoint endPoint)
|
||||||
{
|
{
|
||||||
byte[] bytes = endPoint.Address.GetAddressBytes();
|
byte[] bytes = endPoint.Address.GetAddressBytes();
|
||||||
|
|||||||
@@ -41,5 +41,6 @@ namespace Lidgren.Network
|
|||||||
DebugMessage = 1 << 8, // Data (string)
|
DebugMessage = 1 << 8, // Data (string)
|
||||||
WarningMessage = 1 << 9, // Data (string)
|
WarningMessage = 1 << 9, // Data (string)
|
||||||
ErrorMessage = 1 << 10, // Data (string)
|
ErrorMessage = 1 << 10, // Data (string)
|
||||||
|
NatIntroduction = 1 << 11, // IPEndPoint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,8 @@ namespace Lidgren.Network
|
|||||||
Disconnect = 8,
|
Disconnect = 8,
|
||||||
Discovery = 9,
|
Discovery = 9,
|
||||||
DiscoveryResponse = 10,
|
DiscoveryResponse = 10,
|
||||||
NatIntroduction = 11,
|
NatPunchMessage = 11,
|
||||||
|
NatIntroduction = 12,
|
||||||
}
|
}
|
||||||
|
|
||||||
internal enum NetMessageType : byte
|
internal enum NetMessageType : byte
|
||||||
|
|||||||
49
Lidgren.Network/NetNatIntroduction.cs
Normal file
49
Lidgren.Network/NetNatIntroduction.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -509,8 +509,7 @@ namespace Lidgren.Network
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an endpoint description
|
/// Writes an endpoint description
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="endPoint"></param>
|
public void Write(IPEndPoint endPoint)
|
||||||
internal void Write(IPEndPoint endPoint)
|
|
||||||
{
|
{
|
||||||
byte[] bytes = endPoint.Address.GetAddressBytes();
|
byte[] bytes = endPoint.Address.GetAddressBytes();
|
||||||
Write((byte)bytes.Length);
|
Write((byte)bytes.Length);
|
||||||
|
|||||||
@@ -418,6 +418,12 @@ namespace Lidgren.Network
|
|||||||
|
|
||||||
int payloadLengthBytes = NetUtility.BytesToHoldBits(payloadLengthBits);
|
int payloadLengthBytes = NetUtility.BytesToHoldBits(payloadLengthBits);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle nat introduction
|
||||||
|
//
|
||||||
|
if (libType == NetMessageLibraryType.NatIntroduction)
|
||||||
|
HandleNatIntroduction(ptr, senderEndpoint);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Handle Discovery
|
// Handle Discovery
|
||||||
//
|
//
|
||||||
|
|||||||
65
Samples/MasterServerSample/MasterServer/MasterServer.csproj
Normal file
65
Samples/MasterServerSample/MasterServer/MasterServer.csproj
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProductVersion>9.0.30729</ProductVersion>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
<ProjectGuid>{47E2AC9A-F375-47A7-A4E6-9814D3A2954B}</ProjectGuid>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>MasterServer</RootNamespace>
|
||||||
|
<AssemblyName>MasterServer</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core">
|
||||||
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Xml.Linq">
|
||||||
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Data.DataSetExtensions">
|
||||||
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\..\Lidgren.Network\Lidgren.Network.csproj">
|
||||||
|
<Project>{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}</Project>
|
||||||
|
<Name>Lidgren.Network</Name>
|
||||||
|
</ProjectReference>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
<Target Name="BeforeBuild">
|
||||||
|
</Target>
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
</Target>
|
||||||
|
-->
|
||||||
|
</Project>
|
||||||
81
Samples/MasterServerSample/MasterServer/Program.cs
Normal file
81
Samples/MasterServerSample/MasterServer/Program.cs
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Lidgren.Network;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace MasterServer
|
||||||
|
{
|
||||||
|
public enum MasterServerMessageType
|
||||||
|
{
|
||||||
|
RegisterHost,
|
||||||
|
RequestHostList,
|
||||||
|
RequestIntroduction,
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
List<IPEndPoint> registeredHosts = new List<IPEndPoint>();
|
||||||
|
|
||||||
|
NetPeerConfiguration config = new NetPeerConfiguration("masterserver");
|
||||||
|
|
||||||
|
NetPeer peer = new NetPeer(config);
|
||||||
|
peer.Start();
|
||||||
|
|
||||||
|
// keep going until ESCAPE is pressed
|
||||||
|
Console.WriteLine("Press ESC to quit");
|
||||||
|
while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape)
|
||||||
|
{
|
||||||
|
NetIncomingMessage msg;
|
||||||
|
while((msg = peer.ReadMessage()) != null)
|
||||||
|
{
|
||||||
|
switch (msg.MessageType)
|
||||||
|
{
|
||||||
|
case NetIncomingMessageType.UnconnectedData:
|
||||||
|
//
|
||||||
|
// We've received a message from a client or a host
|
||||||
|
//
|
||||||
|
|
||||||
|
// by design, the first byte always indicates action
|
||||||
|
switch ((MasterServerMessageType)msg.ReadByte())
|
||||||
|
{
|
||||||
|
case MasterServerMessageType.RegisterHost:
|
||||||
|
// It's a host wanting to register its presence
|
||||||
|
registeredHosts.Add(msg.SenderEndpoint);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MasterServerMessageType.RequestHostList:
|
||||||
|
// It's a client wanting a list of registered hosts
|
||||||
|
foreach (IPEndPoint ep in registeredHosts)
|
||||||
|
{
|
||||||
|
// send registered host to client
|
||||||
|
NetOutgoingMessage om = peer.CreateMessage();
|
||||||
|
om.Write(ep);
|
||||||
|
peer.SendUnconnectedMessage(om, msg.SenderEndpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case MasterServerMessageType.RequestIntroduction:
|
||||||
|
// It's a client wanting to connect to a specific host
|
||||||
|
IPEndPoint rh = msg.ReadIPEndpoint();
|
||||||
|
peer.Introduce(rh, msg.SenderEndpoint);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NetIncomingMessageType.DebugMessage:
|
||||||
|
case NetIncomingMessageType.VerboseDebugMessage:
|
||||||
|
case NetIncomingMessageType.WarningMessage:
|
||||||
|
case NetIncomingMessageType.ErrorMessage:
|
||||||
|
// print diagnostics message
|
||||||
|
Console.WriteLine(msg.ReadString());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.Shutdown("shutting down");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyTitle("MasterServer")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("Microsoft")]
|
||||||
|
[assembly: AssemblyProduct("MasterServer")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid("649338d3-b80b-4710-b801-21e593935c21")]
|
||||||
|
|
||||||
|
// Version information for an assembly consists of the following four values:
|
||||||
|
//
|
||||||
|
// Major Version
|
||||||
|
// Minor Version
|
||||||
|
// Build Number
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
|
// by using the '*' as shown below:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||||
26
Samples/MasterServerSample/MasterServerSample.sln
Normal file
26
Samples/MasterServerSample/MasterServerSample.sln
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||||
|
# Visual Studio 2008
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MasterServer", "MasterServer\MasterServer.csproj", "{47E2AC9A-F375-47A7-A4E6-9814D3A2954B}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lidgren.Network", "..\..\Lidgren.Network\Lidgren.Network.csproj", "{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{47E2AC9A-F375-47A7-A4E6-9814D3A2954B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{47E2AC9A-F375-47A7-A4E6-9814D3A2954B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{47E2AC9A-F375-47A7-A4E6-9814D3A2954B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{47E2AC9A-F375-47A7-A4E6-9814D3A2954B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FA245447-5F23-4AA1-BD5F-8D2DDF33CFBD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
BIN
Samples/MasterServerSample/MasterServerSample.suo
Normal file
BIN
Samples/MasterServerSample/MasterServerSample.suo
Normal file
Binary file not shown.
Reference in New Issue
Block a user