Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dodyg committed Nov 24, 2019
2 parents 0b6dbf0 + bc1325c commit 4808593
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
2 changes: 1 addition & 1 deletion projects/bedrock/echo/client/echo-client.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>EchoClient</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>echo-client</PackageId>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
</Project>
42 changes: 30 additions & 12 deletions projects/bedrock/echo/client/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,47 @@
using System.Text;
using System.Threading.Tasks;

// https://github.com/sebastienros/httpsocket/blob/master/src/HttpSocket.cs
// https://devblogs.microsoft.com/dotnet/system-io-pipelines-high-performance-io-in-net/
namespace TcpEcho
{
public class Program
{
static async Task Main(string[] args)
{
var clientSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
int port = 8087;
Console.WriteLine($"Connecting to port {port}");

Console.WriteLine("Connecting to port 8087");
using var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
await clientSocket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port));

clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, 8087));
var stream = new NetworkStream(clientSocket);
try
{
var stream = new NetworkStream(clientSocket);

Byte[] data = System.Text.Encoding.ASCII.GetBytes("Hello World Echo");
await stream.WriteAsync(data, 0, data.Length);
Console.Write("Ready for your input: ");
var input = Console.ReadLine();
while (input != "")
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes(input);
await stream.WriteAsync(data, 0, data.Length);

data = new Byte[256];
String response = String.Empty;
// Read the Tcp Server Response Bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
response = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
data = new Byte[256];
// Read the Tcp Server Response Bytes.
int bytes = stream.Read(data, 0, data.Length);
var response = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

Console.WriteLine("Received: {0}", response);
Console.Write("Ready for your input: ");
input = Console.ReadLine();
}
}
finally
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}

Console.WriteLine("Received: {0}", response);
}
}
}
2 changes: 1 addition & 1 deletion projects/bedrock/echo/server/echo-server.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>EchoServer</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>echo-server</PackageId>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
</Project>
49 changes: 30 additions & 19 deletions projects/bedrock/echo/server/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,36 @@ public EchoConnectionHandler(ILogger<EchoConnectionHandler> log)

public override async Task OnConnectedAsync(ConnectionContext connection)
{
using var tokenSource = new CancellationTokenSource();
connection.ConnectionClosed = tokenSource.Token;

while (true)
try
{
if (connection.ConnectionClosed.IsCancellationRequested)
break;

ReadResult result = await connection.Transport.Input.ReadAsync();
ReadOnlySequence<byte> buffer = result.Buffer;

_log.LogDebug("Receiving data");
_log.LogDebug("Receive connection on " + connection.ConnectionId);

foreach (ReadOnlyMemory<byte> x in buffer)
while (true)
{
await connection.Transport.Output.WriteAsync(x);
}
ReadResult result = await connection.Transport.Input.ReadAsync();
ReadOnlySequence<byte> buffer = result.Buffer;

_log.LogDebug("Receiving data on " + connection.ConnectionId);

if (result.IsCompleted)
break;
foreach (ReadOnlyMemory<byte> x in buffer)
{
await connection.Transport.Output.WriteAsync(x);
}

connection.Transport.Input.AdvanceTo(buffer.End);
if (result.IsCompleted)
{
_log.LogDebug("result.IsCompleted " + connection.ConnectionId);
break;
}

connection.Transport.Input.AdvanceTo(buffer.End);
}
}
finally
{
connection.Transport.Input.Complete();
connection.Transport.Output.Complete();
_log.LogDebug($"Connection {connection.ConnectionId} disconnected");
}
}
}
Expand Down Expand Up @@ -82,7 +90,10 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
});
})
.UseStartup<Startup>();
})
;
}).ConfigureLogging(builder =>
{
builder.SetMinimumLevel(LogLevel.Debug);
builder.AddConsole();
});
}
}

0 comments on commit 4808593

Please sign in to comment.