ThothRpc
v0.7.5-beta
ThothRPC는 .NET 용 드롭 인, 전체 론적, 완전 AOT 호환, 경량, 가벼운 이중 및 양방향 RPC 프레임 워크입니다. 단순하지만 강력합니다. 그것은 완전히 플랫폼의 불가지론적이고 모듈 식으로, 어떤 종류의 프로젝트를 구축하고 있는지 가정하지 않습니다. 전송 및 객체 직렬화 (매개 변수 및 메소드 리턴의 경우) 레이어는 기본 라이브러리와 분리되어 있으며 이러한 레이어의 사용자 정의 구현은 쉽게 소유 한 RPC 시스템을 쉽게 빌드 할 수있는 자유를 제공 할 수 있습니다.
물론,이 레이어가 포함되지 않은 경우 간단하지 않을 것입니다. 이 라이브러리에는 Litenetlib에서 구축 된 신뢰할 수 있고 주문한 UDP 전송 계층과 로드맵에서 안전한 HTTP/2 웹 기반 전송 솔루션이있는 빠른 메시지 팩으로 구축 된 직렬화 레이어가 제공됩니다.
다음 샘플 코드를 사용하려면이 3 개의 Nuget 패키지가 필요합니다.
dotnet add package ThothRpc
dotnet add package ThothRpc.LiteNetLib
dotnet add package ThothRpc.MessagePack
--또는--
Install-Package ThothRpc
Install-Package ThothRpc.LiteNetLib
Install-Package ThothRpc.MessagePack
public interface IClientService
{
[ ThothMethod ] // indicates that this method is callable from server
void PrintServerTime ( DateTime time ) ;
Task GetHelloWorld ( ) ;
}
public interface IServerService
{
[ ThothMethod ] // indicates that this method is callable from client
string GetHelloWorld ( ) ;
} // hubs are thread-safe and can be single instanced for your entire app,
// or you can have multiple instances - its up to you
var hub = ServerHubBuilder . BuildServer ( )
. UseTransport < LiteNetRpcServer > ( )
. UseMessagePack ( ) // any object that is serializable by MessagePack can be used in parameters or return values
. Build ( ) ;
var serverService = new ServerService ( hub ) ;
// register methods can be called multiple times to register multiple services to the same hub
hub . RegisterAs < IServerService > ( serverService ) ;
hub . Listen ( 9050 , "SomeConnectionKey" ) ;
// Thread.Sleep(60000);
// hub.Dispose(); // closes the connection
public class ServerService : IServerService
{
readonly ServerHub _hub ;
public ServerService ( ServerHub hub )
{
_hub = hub ;
Task . Run ( async ( ) =>
{
// print the current time to all clients every second
while ( true )
{
var now = DateTime . Now ;
// Fire and forget
_hub . InvokeForgetAllClients < IClientService > ( DeliveryMode . Sequenced ,
c => c . PrintServerTime ( now ) ) ;
await Task . Delay ( 1000 ) ;
}
} ) ;
}
public string GetHelloWorld ( ) // called from client
{
return "Hello World From Server!" ;
}
} var hub = ClientHubBuilder . BuildClient ( )
. UseTransport < LiteNetRpcClient > ( )
. UseMessagePack ( )
. Build ( ) ;
var clientService = new ClientService ( hub ) ;
hub . RegisterAs < IClientService > ( clientService ) ;
await hub . ConnectAsync ( "localhost" , 9050 , "SomeConnectionKey" ) ;
await clientService . GetHelloWorld ( ) ;
public class ClientService : IClientService
{
readonly ClientHub _hub ;
public ClientService ( ClientHub hub )
{
_hub = hub ;
}
public async Task GetHelloWorld ( )
{
// Method invocations not using fire-and-forget with a udp transport are always delivered reliable and ordered.
var helloWorld = await _hub . InvokeServerAsync < IServerService , string >
( s => s . GetHelloWorld ( ) ) ;
Console . WriteLine ( helloWorld ) ;
}
public void PrintServerTime ( DateTime time ) // called from server
{
Console . WriteLine ( $ "Server time: { time } " ) ;
}
}아래 코드는 위와 동일하지만 이번에는 강력한 타이핑이 없습니다.
var hub = ServerHubBuilder . BuildServer ( )
. UseTransport < LiteNetRpcServer > ( )
. UseMessagePack ( )
. Build ( ) ;
var serverService = new ServerService ( hub ) ;
hub . Register ( serverService , "ServerService" ) ;
hub . Listen ( 9050 , "SomeConnectionKey" ) ;
public class ServerService
{
readonly ServerHub _hub ;
public ServerService ( ServerHub hub )
{
_hub = hub ;
Task . Run ( async ( ) =>
{
while ( true )
{
_hub . InvokeForgetAllClients ( DeliveryMode . Sequenced ,
"ClientService" , "PrintServerTime" , DateTime . Now ) ;
await Task . Delay ( 1000 ) ;
}
} ) ;
}
[ ThothMethod ]
public string GetHelloWorld ( )
{
return "Hello World From Server!" ;
}
} var hub = ClientHubBuilder . BuildClient ( )
. UseTransport < LiteNetRpcClient > ( )
. UseMessagePack ( )
. Build ( ) ;
var clientService = new ClientService ( hub ) ;
hub . Register ( clientService , "ClientService" ) ;
await hub . ConnectAsync ( "localhost" , 9050 , "SomeConnectionKey" ) ;
await clientService . GetHelloWorld ( ) ;
public class ClientService
{
readonly ClientHub _hub ;
public ClientService ( ClientHub hub )
{
_hub = hub ;
}
public async Task GetHelloWorld ( )
{
var helloWorld = await _hub . InvokeServerAsync < string >
( "ServerService" , "GetHelloWorld" ) ;
Console . WriteLine ( helloWorld ) ;
}
[ ThothMethod ]
public void PrintServerTime ( DateTime time )
{
Console . WriteLine ( $ "Server time: { time } " ) ;
}
} Thoth (현재 상태)는…
참고 : 현재 암호화 된 보안 트래픽은 아직 기능이 아니지만 다가오는 HTTP/2 전송에 존재합니다. 그러나 유입 및 egress 콜백으로 자신의 암호화 시스템을 구현하는 것은 쉽습니다.