-
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.
- Moved to 0.4.0 - Added Example.Line - Renamed ProtocolPeer.SendAsync(CancellationToken) to ProtocolPeer.FlushAsync(CancellationToken) - Cleared up end of stream logic - Fixed bug which would prevent peers fully disconnecting if a observer was subscribed
- Loading branch information
1 parent
5324dc7
commit a25a281
Showing
10 changed files
with
165 additions
and
24 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
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<LangVersion>7.2</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ProtoSocket\ProtoSocket.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,44 @@ | ||
using ProtoSocket; | ||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Pipelines; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Example.Line | ||
{ | ||
/// <summary> | ||
/// Encodes/decodes strings. | ||
/// </summary> | ||
public class LineCoder : IProtocolCoder<string> | ||
{ | ||
public bool Read(PipeReader reader, CoderContext<string> ctx, out string frame) { | ||
if (reader.TryRead(out ReadResult result) && !result.IsCompleted) { | ||
// create array (not the most efficient way we could do this) | ||
byte[] arr = result.Buffer.ToArray(); | ||
|
||
// set frame | ||
frame = Encoding.UTF8.GetString(arr); | ||
|
||
// advance | ||
reader.AdvanceTo(result.Buffer.End, result.Buffer.End); | ||
return true; | ||
} | ||
|
||
// we didn't find a frame | ||
frame = null; | ||
return false; | ||
} | ||
|
||
public void Reset() { | ||
} | ||
|
||
public Task WriteAsync(Stream stream, string frame, CoderContext<string> ctx, CancellationToken cancellationToken) { | ||
byte[] arr = Encoding.UTF8.GetBytes(frame); | ||
return stream.WriteAsync(arr, 0, arr.Length); | ||
} | ||
} | ||
} |
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,13 @@ | ||
using ProtoSocket; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Example.Line | ||
{ | ||
class LineConnection : ProtocolConnection<LineConnection, string> | ||
{ | ||
public LineConnection(ProtocolServer<LineConnection, string> server, ProtocolCoderFactory<string> coderFactory) : base(server, coderFactory) { | ||
} | ||
} | ||
} |
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,13 @@ | ||
using ProtoSocket; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Example.Line | ||
{ | ||
class LineServer : ProtocolServer<LineConnection, string> | ||
{ | ||
public LineServer() : base((p) => new LineCoder()) { | ||
} | ||
} | ||
} |
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,44 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Example.Line | ||
{ | ||
class ConsoleObserver : IObserver<string> | ||
{ | ||
public void OnCompleted() { | ||
} | ||
|
||
public void OnError(Exception error) { | ||
} | ||
|
||
public void OnNext(string value) { | ||
Console.WriteLine(value); | ||
} | ||
} | ||
|
||
class Program | ||
{ | ||
static async Task Main(string[] args) { | ||
LineServer server = new LineServer(); | ||
server.Configure("tcp://0.0.0.0:6060"); | ||
server.Start(); | ||
|
||
server.Connected += async (o, e) => { | ||
e.Peer.Subscribe(new ConsoleObserver()); | ||
}; | ||
|
||
while(true) { | ||
var conns = server.Connections; | ||
|
||
foreach(var c in conns) { | ||
Console.WriteLine($"IsConnected: {c.IsConnected} State: {c.State} CR: {c.CloseReason} CE: {(c.CloseException == null ? "null" : c.CloseException.Message)}"); | ||
} | ||
|
||
await Task.Delay(2000); | ||
} | ||
|
||
await Task.Delay(Timeout.InfiniteTimeSpan); | ||
} | ||
} | ||
} |
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