GithubHelp home page GithubHelp logo

grpcnetframework's Introduction

GrpcNetframework

use Grpc service in .net framework
in GrpcLibrary use transfer.cm for Generated source code by the protocol buffer compiler

ChatServiceToServer.proto:

syntax = "proto3";
package GrpcLibrary;
service GrpcLibrary {
  rpc RequestStringFunction (RequestString) returns (RequestString);
  rpc SendMessageFunction (SendMessage) returns (SendMessage);
  rpc RequestByteFunction (RequestByte) returns (RequestByte);
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message Empty {}

message HelloRequest {
  string name = 1;
}
  
message HelloReply {
  string message = 1;
}

message RequestString {
    string sourceId = 1;
    repeated string destinationIDs = 2;
    string rqst = 3;
    string option = 4;
}

message SendMessage {
    string sourceID = 1;
    repeated string destinationIDs = 2;
    string msg = 3;
    string option = 4;
}

message RequestByte {
    string sourceID = 1;
    repeated string destinationIDs = 2;
    string msg = 3;
    bytes rqst = 4;
    string option = 5;
}

GrpcServer:

namespace GrpcServer
{
    class GrpcImpl : GrpcLibrary.GrpcLibrary.GrpcLibraryBase
    {
        // SayHello
        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
        }
    }
    class Program
    {
        static int[] listofPortNumber = new int[] { 10011, 10012, 10013, 10007 };
        static void Main(string[] args)
        {
            int Port = FreeTcpPort();

            Server server = new Server
            {
                Services = { GrpcLibrary.GrpcLibrary.BindService(new GrpcImpl()) },
                Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
            };
            server.Start();

            Console.WriteLine("GrpcService server listening on port " + Port);
            Console.ReadKey();

            server.ShutdownAsync().Wait();
        }
        public static int FreeTcpPort()
        {
            for (int i = 0; i < listofPortNumber.Length; i++)
            {
                try
                {
                    TcpListener l = new TcpListener(IPAddress.Loopback, listofPortNumber[i]);
                    l.Start();
                    int port = ((IPEndPoint)l.LocalEndpoint).Port;
                    l.Stop();
                    return port;
                }
                catch
                {
                    continue;
                }
            }
            return 0;
        }

    }
}

GrpcClient:

namespace GrpcClient
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] listofPortNumber = new int[] { 10011, 10012, 10013, 10007 };

            Channel channel = null;
            bool isConnected = false;
            for (int i = 0; i < listofPortNumber.Length; i++)
            {
                channel = new Channel("127.0.0.1:" + listofPortNumber[i], ChannelCredentials.Insecure);
                if (IsReady(channel))
                {
                    isConnected = true;
                    break;
                }
            }

            if (channel != null && isConnected)
            {
                var client = new GrpcLibrary.GrpcLibrary.GrpcLibraryClient(channel);
                while (true)
                {
                    try
                    {
                        Console.WriteLine("type something:  ");
                        var input = Console.ReadLine();
                        var reply = client.SayHello(new HelloRequest { Name = input });
                        Console.WriteLine("reply: " + reply.Message);

                        //channel.ShutdownAsync().Wait();
                        //Console.ReadKey();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
        public static bool IsReady(Channel channel)
        {
            channel.ConnectAsync();
            return channel.State == ChannelState.Ready;
        }

    }
}

grpcnetframework's People

Contributors

taherfattahi avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

vebko

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.