-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.IO; | ||
using System.Net; | ||
using System.Threading; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace OrangeCabinet.Tests | ||
{ | ||
public class TestSimpleV6 | ||
{ | ||
public TestSimpleV6(ITestOutputHelper testOutputHelper) | ||
{ | ||
OcDate.AddSeconds = 60 * 60 * 9; | ||
// OcLogger.Writer = new StreamWriter(new FileStream("Test.log", FileMode.Append)); | ||
OcLogger.Verbose = true; | ||
OcLogger.Transfer = new OcLoggerTransfer | ||
{ | ||
Transfer = msg => testOutputHelper.WriteLine(msg.ToString()), | ||
Raw = false | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void Test() | ||
{ | ||
var serverBinder = new OcBinder(new SampleV6Callback()) | ||
{ | ||
SocketAddressFamily = OcSocketAddressFamily.Ipv6, | ||
BindPort = 8710, | ||
}; | ||
var server = new OcLocal(serverBinder); | ||
server.Start(); | ||
// server.WaitFor(); | ||
|
||
// ----- | ||
using var clientBinder = new OcBinder(new SampleV6Callback()) | ||
{ | ||
SocketAddressFamily = OcSocketAddressFamily.Ipv6, | ||
BindPort = 18710, | ||
}; | ||
var client = new OcRemote(clientBinder, "::1", 8710); | ||
for (int j = 0; j < 3; j++) | ||
{ | ||
client.Send($"{j}".OxToBytes()); | ||
} | ||
// ----- | ||
|
||
// ... | ||
Thread.Sleep(1000); | ||
server.SendTo("hello from server", new IPEndPoint(IPAddress.Parse("::1"), 8710)); | ||
server.Shutdown(); | ||
|
||
OcLogger.Close(); | ||
} | ||
} | ||
|
||
public class SampleV6Callback : OcCallback | ||
{ | ||
private const string Key = "inc"; | ||
|
||
public override void Incoming(OcRemote remote, byte[] message) | ||
{ | ||
OcLogger.Info($"Received: {message.OxToString()} ({remote})"); | ||
|
||
int inc = remote.GetValue<int>(Key); | ||
inc++; | ||
remote.SetValue(Key, inc); | ||
|
||
remote.Send($"{inc}".OxToBytes()); | ||
if (inc > 10) | ||
{ | ||
remote.ClearValue(Key); | ||
remote.Escape(); | ||
} | ||
} | ||
|
||
public override void Timeout(OcRemote remote) | ||
{ | ||
OcLogger.Info($"Timeout: {remote}"); | ||
} | ||
|
||
public override void Shutdown(OcRemote remote) | ||
{ | ||
OcLogger.Info($"Shutdown: {remote}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Net.Sockets; | ||
|
||
namespace OrangeCabinet; | ||
|
||
/// <summary> | ||
/// Address family. | ||
/// </summary> | ||
public enum OcSocketAddressFamily | ||
{ | ||
/// <summary> | ||
/// Ipv4. | ||
/// </summary> | ||
Ipv4, | ||
|
||
/// <summary> | ||
/// Ipv6. | ||
/// If selected, v4 socket is treated as v6 socket. | ||
/// </summary> | ||
Ipv6 | ||
} | ||
|
||
/// <summary> | ||
/// Address family resolver. | ||
/// </summary> | ||
internal static class OcSocketAddressFamilyResolver | ||
{ | ||
/// <summary> | ||
/// Resolve for 'AddressFamily' . | ||
/// </summary> | ||
/// <param name="socketAddressFamily">address family</param> | ||
/// <returns>AddressFamily</returns> | ||
internal static AddressFamily Resolve(OcSocketAddressFamily socketAddressFamily) | ||
{ | ||
return socketAddressFamily switch | ||
{ | ||
OcSocketAddressFamily.Ipv6 => AddressFamily.InterNetworkV6, | ||
_ => AddressFamily.InterNetwork | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters