Alex Bausk
Alex Bausk

Reputation: 718

How to approach an inter-language RPC protocol design for a simple API?

I am making a (very basic) distributed application that has a Python server and a C# client talking to each other via json over ZeroMQ. When a method is invoked in client, it composes a message and sends it to the server where a corresponding function is executed, so it's basically a sort of an RPC.

To understand what to do with the messages, the app relies on a 'protocol' (in fact a set of keywords) which is duplicated on client and server side, something like this:

namespace Draftsocket
{
    public static class Protocol
    {
        public struct ClientAction
        {
            //client-issued action lines
            public const string CMD = "COMMAND";
            public const string ERROR = "CLIENT_ERROR";
            public const string EVENT = "EVENT";
            public const string CONTINUE = "CONTINUE";
            public const string CONSOLE = "CONSOLE";
        }
    ...etc...

(link to complete file in C#)

class Protocol(object):

    class ClientAction:
        CMD = "COMMAND"
        ERROR = "CLIENT_ERROR"
        EVENT = "EVENT"
        CONTINUE = "CONTINUE"
        CONSOLE = "CONSOLE"
...etc...

(link to complete file in Python)

This is then used to compose and read messages, like this:

public static ClientMessage NewCommand(string Name)
{
    return new ClientMessage(Protocol.ClientAction.CMD, Protocol.Status.OK, Name);
}

This works for the time being but is generally awful: stringly-typed and inconvenient for anyone who will want to use the application as a framework to build on.

I can't decide on how to refactor this protocol in a way that would either automate/sync to each other the language-specific implementations or, for example, fire up a warning if the protocol are out of sync/outdated?

Guess that I should look into Enums and/or something of the sort MessagePack implements, but I don't feel qualified enough to proceed on my own with this.

TIA.

Upvotes: 1

Views: 414

Answers (1)

fejesjoco
fejesjoco

Reputation: 11903

I would use protobuf or thrift. They are meant to solve the problem of data exchange between different platforms. See this for example: Biggest differences of Thrift vs Protocol Buffers?

Upvotes: 2

Related Questions