Thothrpc เป็นแบบดรอปอินแบบองค์รวมที่เข้ากันได้ AOT อย่างเต็มที่มีน้ำหนักเบาดูเพล็กซ์และเฟรมเวิร์ก RPC แบบสองทิศทางสำหรับ. NET มันตายง่าย แต่ทรงพลัง มันเป็นแพลตฟอร์มที่ไม่เชื่อเรื่องพระเจ้าและโมดูลาร์อย่างสมบูรณ์โดยไม่มีสมมติฐานว่าคุณกำลังสร้างโครงการประเภทใด เลเยอร์การขนส่งและวัตถุ (สำหรับพารามิเตอร์และวิธีการส่งคืนวิธีการ) แยกจากไลบรารีพื้นฐานและการใช้งานที่กำหนดเองของเลเยอร์เหล่านี้เป็นเรื่องง่ายที่จะทำให้คุณมีอิสระสำหรับคุณในการสร้างระบบ RPC ของคุณเองได้อย่างง่ายดาย
แน่นอนว่ามันจะไม่ง่ายถ้าเลเยอร์เหล่านี้ไม่ได้รวมไว้สำหรับคุณ ห้องสมุดนี้มาพร้อมกับชั้นการขนส่ง UDP ที่เชื่อถือได้และสั่งซื้อที่สร้างขึ้นจาก LitenetLib และเลเยอร์การทำให้เป็นอนุกรมที่สร้างขึ้นจากชุดข้อความที่รวดเร็วพร้อมโซลูชันการขนส่งบนเว็บ HTTP/2 ที่ปลอดภัยบนแผนที่ถนน
ในการใช้รหัสตัวอย่างต่อไปนี้คุณต้องใช้แพ็คเกจ NuGet ทั้ง 3 ชุด
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 ที่กำลังจะมาถึง อย่างไรก็ตามการใช้ระบบเข้ารหัสของคุณเองนั้นง่ายด้วยการโทรกลับและการโทรกลับออก