ThothRpc
v0.7.5-beta
THOTHRPC是.NET的液位,整體,完全AOT兼容,輕巧,完全雙鍊和雙向RPC框架。它很簡單,但功能強大。它完全是平台不可知和模塊化,沒有假設您正在構建哪種項目。傳輸和對象序列化(對於參數和方法返回)與基本庫是分開的,並且這些圖層的自定義實現很容易為您提供自由,讓您可以輕鬆構建自己的RPC系統。
當然,如果不包括這些層,這並不簡單。該庫帶有由LiteNetlib建造的可靠和有序的UDP傳輸層,並在路線圖上使用安全的HTTP/2基於安全的HTTP/2基於安全的HTTP/2基於Web的傳輸解決方案構建的序列化層。
要使用以下示例代碼,您需要這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傳輸中存在。但是,通過入口和出口回調,實現自己的加密系統很容易。