C# - High Performance RPC Framework: Socean.RPC

Keywords: C# encoding JSON socket github

brief introduction

Socean.RPC is a high-performance RPC framework under.Net. The framework is targeted at high performance and high stability. The underlying layer is based on socket, and its code is concise. The total code size is about 2000 rows, without third party library references, and the frame performance is high. It can reach 10w+ per second processing capacity on dual core i5 notebook, and support 10000 long connection message sending (10 short messages per second). And low cpu share

Development background

There are too few wheels to use, so we can only get one by ourselves, hoping to promote the development of. Net community.

Introduction

server sample :

1. Defining entities

public class Book
{
  public string Name { get; set; }
}

Define Message Processor

public class DefaultMessageProcessor : IMessageProcessor
{

  public ResponseBase Process(string title, byte[] contentBytes)
  {

    if (title == "book/name/change")
    {
      var content = Encoding.UTF8.GetString(contentBytes);

      //here we use newtonsoft.Json serializer 
      //you need add refer "newtonsoft.Json.dll"
      var book = JsonConvert.DeserializeObject<Book>(content);
      book.Name = "new name";

      var responseContent = JsonConvert.SerializeObject(book);
      return new BytesResponse(Encoding.UTF8.GetBytes(responseContent));
    }

    if (title == "empty")
    {
      return new EmptyResponse();
    }

    return new ErrorResponse(ResponseErrorCode.SERVICE_NOT_FOUND);
  }
}

 

2. Start up services

var server = new KeepAliveRpcServer();
server.Bind(IPAddress.Any, 11111);
server.AutoReconnect = true;
server.MessageProcessor = new DefaultMessageProcessor();

server.Start(); 

 

client sample:

1. Defining entities

public class Book
{
  public string Name { get; set; }
}

2. Execute calls

public Book ChangeBookName(Book book)
{
  using (var rpcClient = ShortConnectionRpcClientFactory.Create(IPAddress.Parse("127.0.0.1"), 11111))
  {
    var requestContent = JsonConvert.SerializeObject(book);
    var response = rpcClient.Query("book/name/change", Encoding.UTF8.GetBytes(requestContent));
    var content = Encoding.UTF8.GetString(response.ContentBytes);
    return JsonConvert.DeserializeObject<Book>(content);
  }
}

Other

NetworkSettings class can modify parameters such as connection timeout

For performance testing, it is best to set NetworkSettings. ClientDetectReceive Interval to 1 on the client side and increase thread priority to ThreadPriority.Highest.

Project address

https://github.com/ch00486259/Socean.Rpc

The maximum concurrent processing capacity should be more than 30w. Who has 40 core servers can help me test it.

Posted by chord on Fri, 04 Oct 2019 13:31:45 -0700