Skip to content

Commit

Permalink
Updated TcpEchoServer sample to target .net core and not throw except…
Browse files Browse the repository at this point in the history
…ions in console (#4403)

* Docs page fix

* Added and updated TcpEchoService.Server sample

* Fixed watched actor error in console

* Reference common.props
  • Loading branch information
IgorFedchenko authored May 6, 2020
1 parent a804b05 commit d3e4f92
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 117 deletions.
18 changes: 18 additions & 0 deletions src/Akka.sln
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Coordination", "core\A
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Coordination.Tests", "core\Akka.Coordination.Tests\Akka.Coordination.Tests.csproj", "{6AA74665-DFE0-450B-8F66-19125ABBB1C7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TcpEchoServer", "TcpEchoServer", "{52A36134-AC41-4F38-9D47-1124B0C9CDD2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TcpEchoService.Server", "examples\TcpEchoService.Server\TcpEchoService.Server.csproj", "{8AD2DF54-B79F-490B-B2C6-94EDA397055F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -976,6 +980,18 @@ Global
{6AA74665-DFE0-450B-8F66-19125ABBB1C7}.Release|x64.Build.0 = Release|Any CPU
{6AA74665-DFE0-450B-8F66-19125ABBB1C7}.Release|x86.ActiveCfg = Release|Any CPU
{6AA74665-DFE0-450B-8F66-19125ABBB1C7}.Release|x86.Build.0 = Release|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Debug|x64.ActiveCfg = Debug|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Debug|x64.Build.0 = Debug|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Debug|x86.ActiveCfg = Debug|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Debug|x86.Build.0 = Debug|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Release|Any CPU.Build.0 = Release|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Release|x64.ActiveCfg = Release|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Release|x64.Build.0 = Release|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Release|x86.ActiveCfg = Release|Any CPU
{8AD2DF54-B79F-490B-B2C6-94EDA397055F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1070,6 +1086,8 @@ Global
{FC5216C2-3C98-4691-8E72-205EBF9594D0} = {76F58DC4-19F1-43EF-A6E2-EC1CC8395AC5}
{ADDA116F-7AB5-41DE-A95B-D5E64688E9A0} = {01167D3C-49C4-4CDE-9787-C176D139ACDD}
{6AA74665-DFE0-450B-8F66-19125ABBB1C7} = {01167D3C-49C4-4CDE-9787-C176D139ACDD}
{52A36134-AC41-4F38-9D47-1124B0C9CDD2} = {D3AF8295-AEB5-4324-AA82-FCC0014AC310}
{8AD2DF54-B79F-490B-B2C6-94EDA397055F} = {52A36134-AC41-4F38-9D47-1124B0C9CDD2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {03AD8E21-7507-4E68-A4E9-F4A7E7273164}
Expand Down
30 changes: 29 additions & 1 deletion src/examples/TcpEchoService.Server/Actors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,44 @@ namespace TcpEchoService.Server
public class EchoService : ReceiveActor
{
private readonly IActorRef _manager = Context.System.Tcp();
private IActorRef _tcpListener;
private IActorRef _stopCommander;

public EchoService(EndPoint endpoint)
{
_manager.Tell(new Tcp.Bind(Self, endpoint));

// Store TcpListener in case if we will want to Unbing
Receive<Tcp.Bound>(_ => _tcpListener = Sender);

// To behave as TCP listener, actor should be able to handle Tcp.Connected messages
Receive<Tcp.Connected>(connected =>
{
Console.WriteLine("Remote address {0} connected", connected.RemoteAddress);
Sender.Tell(new Tcp.Register(Context.ActorOf(Props.Create(() => new EchoConnectionHandler(connected.RemoteAddress, Sender)))));
var handler = Context.ActorOf(Props.Create(() => new EchoConnectionHandler(connected.RemoteAddress, Sender)));
Sender.Tell(new Tcp.Register(handler));
});

// Close connection before exit
Receive<StopServer>(_ =>
{
_stopCommander = Sender;
_tcpListener?.Tell(Tcp.Unbind.Instance);
});
// Report that close completed
Receive<Tcp.Unbound>(_ => _stopCommander?.Tell("Done"));
}

public class StopServer { }
}

public class EchoConnectionHandler : ReceiveActor
{
private readonly IActorRef _connection;

public EchoConnectionHandler(EndPoint remote, IActorRef connection)
{
_connection = connection;
// we want to know when the connection dies (without using Tcp.ConnectionClosed)
Context.Watch(connection);

Expand All @@ -57,5 +77,13 @@ public EchoConnectionHandler(EndPoint remote, IActorRef connection)
Context.Stop(Self);
});
}

/// <inheritdoc />
protected override void PostStop()
{
base.PostStop();

_connection.Tell(Tcp.Close.Instance);
}
}
}
6 changes: 0 additions & 6 deletions src/examples/TcpEchoService.Server/App.config

This file was deleted.

6 changes: 5 additions & 1 deletion src/examples/TcpEchoService.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

using System;
using System.Net;
using System.Threading.Tasks;
using Akka.Actor;

namespace TcpEchoService.Server
{
class Program
{
static void Main(string[] args)
static async Task Main(string[] args)
{
using (var system = ActorSystem.Create("echo-server-system"))
{
Expand All @@ -28,6 +29,9 @@ static void Main(string[] args)
Console.WriteLine("TCP server is listening on *:{0}", port);
Console.WriteLine("ENTER to exit...");
Console.ReadLine();

// Close connection to avoid error message in console
await actor.Ask(new EchoService.StopServer());
}
}
}
Expand Down
43 changes: 0 additions & 43 deletions src/examples/TcpEchoService.Server/Properties/AssemblyInfo.cs

This file was deleted.

79 changes: 13 additions & 66 deletions src/examples/TcpEchoService.Server/TcpEchoService.Server.csproj
Original file line number Diff line number Diff line change
@@ -1,66 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{825196A4-4B08-401F-8994-E2DB7C77A8B7}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TcpEchoService.Server</RootNamespace>
<AssemblyName>TcpEchoService.Server</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Actors.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\core\Akka\Akka.csproj">
<Project>{5deddf90-37f0-48d3-a0b0-a5cbd8a7e377}</Project>
<Name>Akka</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>
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(NetCoreTestVersion)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\core\Akka\Akka.csproj" />
</ItemGroup>

</Project>

0 comments on commit d3e4f92

Please sign in to comment.