Refine Service Api a bit

This commit is contained in:
K35 2022-01-07 22:10:24 +00:00
parent bb46da08de
commit 48a0713045
5 changed files with 19 additions and 4 deletions

View File

@ -75,7 +75,7 @@ class Program {
public static async Task Start() { public static async Task Start() {
var server = new Example.JRpcServer<int>(); var server = new Example.JRpcServer<int>();
server.AddService("TestService", new TestSrvimpl()); server.AddService(new TestSrvimpl());
var transport = new CopyTransportS1(); var transport = new CopyTransportS1();
var sess = server.GetSession(transport, 0); var sess = server.GetSession(transport, 0);

View File

@ -10573,6 +10573,12 @@ var CSharpTarget = class extends CompileTarget {
a(0, ``); a(0, ``);
a(0, `public abstract class ${definition.name}Server<TContext> : JRpcService<TContext> {`); a(0, `public abstract class ${definition.name}Server<TContext> : JRpcService<TContext> {`);
a(0, ``); a(0, ``);
a(1, `public override string Name {`);
a(2, `get {`);
a(3, `return "${definition.name}";`);
a(2, `}`);
a(1, `}`);
a(0, ``);
a(1, `public ${definition.name}Server() {`); a(1, `public ${definition.name}Server() {`);
for (const fnc of definition.functions) { for (const fnc of definition.functions) {
a(2, `this.RegisterFunction("${fnc.name}");`); a(2, `this.RegisterFunction("${fnc.name}");`);

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/jrpcgen", "name": "@hibas123/jrpcgen",
"version": "1.0.15", "version": "1.0.16",
"main": "lib/index.js", "main": "lib/index.js",
"license": "MIT", "license": "MIT",
"packageManager": "yarn@3.1.1", "packageManager": "yarn@3.1.1",

View File

@ -219,6 +219,12 @@ export class CSharpTarget extends CompileTarget<{ CSharp_Namespace: string }> {
`public abstract class ${definition.name}Server<TContext> : JRpcService<TContext> {` `public abstract class ${definition.name}Server<TContext> : JRpcService<TContext> {`
); );
a(0, ``);
a(1, `public override string Name {`);
a(2, `get {`);
a(3, `return "${definition.name}";`);
a(2, `}`);
a(1, `}`);
a(0, ``); a(0, ``);
a(1, `public ${definition.name}Server() {`); a(1, `public ${definition.name}Server() {`);
for (const fnc of definition.functions) { for (const fnc of definition.functions) {

View File

@ -16,9 +16,9 @@ public class JRpcServer<TContext>
this.Services = new Dictionary<string, JRpcService<TContext>>(); this.Services = new Dictionary<string, JRpcService<TContext>>();
} }
public void AddService(string name, JRpcService<TContext> service) public void AddService(JRpcService<TContext> service)
{ {
this.Services.Add(name, service); this.Services.Add(service.Name, service);
} }
public JRpcServerSession<TContext> GetSession(JRpcTransport transport, TContext context) public JRpcServerSession<TContext> GetSession(JRpcTransport transport, TContext context)
@ -184,8 +184,11 @@ public class JRpcServerSession<TContext>
public abstract class JRpcService<TContext> public abstract class JRpcService<TContext>
{ {
public abstract string Name { get; }
public HashSet<string> Functions = new HashSet<string>(); public HashSet<string> Functions = new HashSet<string>();
protected void RegisterFunction(string name) protected void RegisterFunction(string name)
{ {
this.Functions.Add(name); this.Functions.Add(name);