grpc_host 1.0.1 copy "grpc_host: ^1.0.1" to clipboard
grpc_host: ^1.0.1 copied to clipboard

discontinued
outdated

Hosts gRPC services

example/grpc_host_example.dart

import 'package:grpc/grpc.dart';
import 'package:grpc_host/grpc_host.dart';
import 'demo_service.pbgrpc.dart';

void main() async {
  final host = ExampleHost();
  host.serve();
}

/// This class hosts the server, which will
/// will run several isolates, each waiting for
/// a gRPC connection to be established.
///
/// The number of isolates is a funcion of
/// the parameters defined by HostSettings,
/// isolatesMultiplier * the number of CPU
/// cores, plus the extraIsolates parameter.
class ExampleHost extends Host {
  ExampleHost() : super(ExampleHost.run);

  @override
  HostSettings get hostSettings {
    return HostSettings(port: 9000, isolatesMultiplier: 2);
  }

  /// This method will be run for each of the spawned isolates
  static void run(HostParameters parms) async {
    // It will launch the ExampleServicesHost instance
    // that itself will finally host a set of
    // services run by the isolate.
    final host = ExampleServicesHost(parms);
    await host.run();
  }
}

class ExampleServicesHost extends ServicesHost {
  ExampleServicesHost(HostParameters parameters) : super(parameters);

  @override
  List<Service> get services => [GDemoService()];
}

class GDemoService extends GDemoServiceBase {
  @override
  Future<GPersonInsertResult> insertPerson(
      ServiceCall call, GPerson request) async {
    // insert the person in the database and return the result
    // this is purely demonstrative.
    final ret = GPersonInsertResult(key: request.key);
    return ret;
  }
}