Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit 5ddebf5

Browse files
committed
Update to netcode.io 0.1.4 and fix connectivity to IPv4 addresses
1 parent cb81f89 commit 5ddebf5

File tree

10 files changed

+73
-35
lines changed

10 files changed

+73
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ browser/hostapp/manifest.windows.json
66
browser/chrome/chrome.zip
77
output/
88
netcode.io.wininstall/package.zip
9+
*.user

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ The API made available to Javascript clients as `window.netcode`. You can check
2828

2929
`window.netcode` provides one function: `createClient(callback)`:
3030

31-
### window.netcode.createClient(callback)
31+
### window.netcode.createClient(protocol, callback)
3232

3333
Creates a new netcode.io client which can be used for secure UDP communication using the netcode.io protocol. The callback is of the form `callback(err, client)`. If `err` is set, there was an error creating the client (and `client` will be null). Otherwise `client` is set and `err` is null. The returned client is an instance of `Client`.
3434

35+
`protocol` should be either `ipv4` or `ipv6` and determines what type of server address you can connect to.
36+
3537
**Parameters:**
38+
- `protocol`: Either `ipv4` or `ipv6`.
3639
- `callback`: A callback in the form `callback(err, client)` where `err` is either `null` or an instance of `Error`, and `client` is either `null` or an instance of `Client`.
3740

3841
### window.netcode.isNativeHelperInstalled(callback)

browser/chrome/netcode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ window.netcode = {
134134
message: [_typeCheckPresence, messageId]
135135
}, "*");
136136
},
137-
createClient: function(callback) {
137+
createClient: function(protocol, callback) {
138138
var messageId = _createMessage(function(type, args) {
139139
if (type == _resultClientCreated) {
140140
var clientId = args[0];
@@ -186,7 +186,7 @@ window.netcode = {
186186
});
187187
window.postMessage({
188188
type: "netcode.io-send",
189-
message: [_typeCreateClient, messageId]
189+
message: [_typeCreateClient, messageId, protocol == 'ipv6']
190190
}, "*");
191191
},
192192
}

netcode.io.demoserver/Program.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ class Program
2121
};
2222

2323
static bool running = true;
24+
25+
static string serverAddress = "127.0.0.1";
2426

25-
static string listenAddress = "[::]";
27+
static string serverPort = "40000";
2628

27-
static string serverAddress = "[::1]";
29+
static string httpAddress = "127.0.0.1";
30+
31+
static string httpPort = "8080";
32+
33+
static bool isServerIpv4 = true;
2834

2935
static void Main(string[] args)
3036
{
@@ -35,28 +41,48 @@ static void Main(string[] args)
3541
{
3642
nonInteractive = true;
3743
}
38-
else if (args[i] == "--listen-address")
44+
else if (args[i] == "--server-address")
3945
{
40-
listenAddress = args[i + 1];
46+
serverAddress = args[i + 1];
4147
i++;
4248
}
43-
else if (args[i] == "--server-address")
49+
else if (args[i] == "--server-port")
4450
{
45-
serverAddress = args[i + 1];
51+
serverPort = args[i + 1];
52+
i++;
53+
}
54+
else if (args[i] == "--http-address")
55+
{
56+
httpAddress = args[i + 1];
4657
i++;
4758
}
59+
else if (args[i] == "--http-port")
60+
{
61+
httpPort = args[i + 1];
62+
i++;
63+
}
64+
}
65+
66+
try
67+
{
68+
var ip = IPAddress.Parse(serverAddress);
69+
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
70+
{
71+
isServerIpv4 = false;
72+
}
4873
}
74+
catch { }
4975

5076
// Start web server.
51-
WebServer ws = new WebServer(SendResponse, "http://*:8080/");
77+
WebServer ws = new WebServer(SendResponse, "http://" + httpAddress + ":" + httpPort + "/");
5278
ws.Run();
5379

5480
// Run netcode.io server in another thread.
5581
var netcodeThread = new Thread(NetcodeServer);
5682
netcodeThread.IsBackground = !nonInteractive;
5783
netcodeThread.Start();
5884

59-
Console.WriteLine("netcode.io demo server started, open up http://*:8080/ to try it!");
85+
Console.WriteLine("netcode.io demo server started, open up http://" + httpAddress + ":" + httpPort + "/ to try it!");
6086
if (!nonInteractive)
6187
{
6288
Console.ReadKey();
@@ -73,8 +99,7 @@ private static void NetcodeServer()
7399
double deltaTime = 1.0 / 60.0;
74100

75101
var server = new Server(
76-
listenAddress + ":40000",
77-
serverAddress + ":40000",
102+
serverAddress + ":" + serverPort,
78103
0x1122334455667788L,
79104
_privateKey,
80105
0);
@@ -126,7 +151,7 @@ public static string SendResponse(HttpListenerRequest request, HttpListenerRespo
126151
var indexPath = Path.Combine(new FileInfo(asmPath).DirectoryName, "index.htm");
127152
using (var reader = new StreamReader(indexPath))
128153
{
129-
return reader.ReadToEnd();
154+
return reader.ReadToEnd().Replace("__PROTOCOL__", isServerIpv4 ? "ipv4" : "ipv6");
130155
}
131156
}
132157

@@ -136,7 +161,7 @@ public static string SendResponse(HttpListenerRequest request, HttpListenerRespo
136161

137162
var clientId = NetcodeLibrary.GetRandomUInt64();
138163
var token = NetcodeLibrary.GenerateConnectTokenFromPrivateKey(
139-
new[] { serverAddress + ":40000" },
164+
new[] { serverAddress + ":" + serverPort },
140165
30,
141166
clientId,
142167
0x1122334455667788L,

netcode.io.demoserver/index.htm

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
return new TextDecoder("ascii").decode(buf);
3030
}
3131

32+
var packetsSentCount = 0;
33+
var lastState = '';
34+
3235
function updateConnectionState(client) {
3336
client.getClientState(function (err, state) {
3437
if (err) {
@@ -37,13 +40,12 @@
3740
return;
3841
}
3942

43+
lastState = state;
4044
$("#netcode_connection").text(state);
4145
window.setTimeout(function () { updateConnectionState(client); }, 100);
4246
});
4347
}
4448

45-
var packetsSentCount = 0;
46-
4749
$(function () {
4850
window.setTimeout(function() {
4951
if (window.netcode) {
@@ -58,7 +60,7 @@
5860
$("#netcode_presence").html("<b>Available!</b>");
5961

6062
$("#netcode_client").text("Creating...");
61-
window.netcode.createClient(function (err, client) {
63+
window.netcode.createClient('__PROTOCOL__', function (err, client) {
6264
if (err) {
6365
console.error(err);
6466
$("#netcode_client").text(err.toString());
@@ -82,12 +84,14 @@
8284
$("#netcode_client").text("Connected!");
8385

8486
window.setInterval(function () {
85-
var buf = str2ab("pkt " + packetsSentCount + "! " + (new Date().toUTCString()));
86-
packetsSentCount++;
87-
$("#packets_sent_count").text(packetsSentCount);
88-
client.send(buf, function () {
89-
// Do nothing
90-
});
87+
if (lastState == 'connected') {
88+
var buf = str2ab("pkt " + packetsSentCount + "! " + (new Date().toUTCString()));
89+
packetsSentCount++;
90+
$("#packets_sent_count").text(packetsSentCount);
91+
client.send(buf, function () {
92+
// Do nothing
93+
});
94+
}
9195
}, 32);
9296
});
9397
});

netcode.io.demoserver/netcode.io.demoserver.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
</PropertyGroup>
3636
<ItemGroup>
3737
<Reference Include="netcode.io-csharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
38-
<HintPath>..\packages\netcode.io.0.1.3\lib\net45\netcode.io-csharp.dll</HintPath>
38+
<HintPath>..\packages\netcode.io.0.1.4\lib\net45\netcode.io-csharp.dll</HintPath>
3939
</Reference>
4040
<Reference Include="netcodeBinding, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
41-
<HintPath>..\packages\netcode.io.0.1.3\lib\net45\netcodeBinding.dll</HintPath>
41+
<HintPath>..\packages\netcode.io.0.1.4\lib\net45\netcodeBinding.dll</HintPath>
4242
</Reference>
4343
<Reference Include="System" />
4444
<Reference Include="System.Core" />
@@ -64,11 +64,11 @@
6464
</Content>
6565
</ItemGroup>
6666
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67-
<Import Project="..\packages\netcode.io.0.1.3\build\netcode.io.targets" Condition="Exists('..\packages\netcode.io.0.1.3\build\netcode.io.targets')" />
67+
<Import Project="..\packages\netcode.io.0.1.4\build\netcode.io.targets" Condition="Exists('..\packages\netcode.io.0.1.4\build\netcode.io.targets')" />
6868
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
6969
<PropertyGroup>
7070
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
7171
</PropertyGroup>
72-
<Error Condition="!Exists('..\packages\netcode.io.0.1.3\build\netcode.io.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\netcode.io.0.1.3\build\netcode.io.targets'))" />
72+
<Error Condition="!Exists('..\packages\netcode.io.0.1.4\build\netcode.io.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\netcode.io.0.1.4\build\netcode.io.targets'))" />
7373
</Target>
7474
</Project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="netcode.io" version="0.1.3" targetFramework="net452" />
3+
<package id="netcode.io" version="0.1.4" targetFramework="net452" />
44
</packages>

netcode.io.host/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,14 @@ static void Main(string[] args)
113113
{
114114
id = _random.Next();
115115
}
116+
var isIpV6 = false;
117+
if (messageArray.Count >= 3)
118+
{
119+
isIpV6 = messageArray[2].Value<bool>();
120+
}
116121
_clients[id] = new ManagedClient
117122
{
118-
client = new Client("::", 0),
123+
client = new Client(isIpV6 ? "::" : "0.0.0.0", 0),
119124
tickRate = 60,
120125
shouldRun = true,
121126
time = 0,

netcode.io.host/netcode.io.host.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
</PropertyGroup>
3636
<ItemGroup>
3737
<Reference Include="netcode.io-csharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
38-
<HintPath>..\packages\netcode.io.0.1.3\lib\net45\netcode.io-csharp.dll</HintPath>
38+
<HintPath>..\packages\netcode.io.0.1.4\lib\net45\netcode.io-csharp.dll</HintPath>
3939
</Reference>
4040
<Reference Include="netcodeBinding, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
41-
<HintPath>..\packages\netcode.io.0.1.3\lib\net45\netcodeBinding.dll</HintPath>
41+
<HintPath>..\packages\netcode.io.0.1.4\lib\net45\netcodeBinding.dll</HintPath>
4242
</Reference>
4343
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4444
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
@@ -61,11 +61,11 @@
6161
<None Include="packages.config" />
6262
</ItemGroup>
6363
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
64-
<Import Project="..\packages\netcode.io.0.1.3\build\netcode.io.targets" Condition="Exists('..\packages\netcode.io.0.1.3\build\netcode.io.targets')" />
64+
<Import Project="..\packages\netcode.io.0.1.4\build\netcode.io.targets" Condition="Exists('..\packages\netcode.io.0.1.4\build\netcode.io.targets')" />
6565
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
6666
<PropertyGroup>
6767
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
6868
</PropertyGroup>
69-
<Error Condition="!Exists('..\packages\netcode.io.0.1.3\build\netcode.io.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\netcode.io.0.1.3\build\netcode.io.targets'))" />
69+
<Error Condition="!Exists('..\packages\netcode.io.0.1.4\build\netcode.io.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\netcode.io.0.1.4\build\netcode.io.targets'))" />
7070
</Target>
7171
</Project>

netcode.io.host/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="netcode.io" version="0.1.3" targetFramework="net452" />
3+
<package id="netcode.io" version="0.1.4" targetFramework="net452" />
44
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
55
</packages>

0 commit comments

Comments
 (0)