Kotlin Generator

NOTE: This article is preliminary since the protocol compiler has no support for Kotlin at the moment. It is being used by the developer to add support and to document the results. It's useless for general developers.

When a protocol is compiled to Kotlin, it generates a .kt with the name indicating the protocol. The code will also be placed in an appropriate package (with all names in lowercase.) For instance, a protocol defined in Chat.proto will generate the file chat_protocol.kt and near the start of the file will be



The base protocol compiler types are mapped to Kotlin types in the following way:

Protocol Compiler Type Kotlin Type
bool Boolean
int16 Short
int32 Int
int64 Long
double Double
string String
binary kotlin.UByteArray
enum T enum class T
T [] Array<T>
optional T T?

User-defined Types

The struct keyword, in the protocol source file, defines a new data type and is implemented in Kotlin as a data class.

Request and Reply Messages

Request and Reply messages are implemented as Kotlin sealed classes.


For this example, we'll define a protocol for a simple ACNET chat system (it won't have chat rooms.) The source for the protocol is

request register {
    string nickname;

reply registered {
    int32 id;

reply message {
    int64 time;
    string who; 
    string what;

request deliver {
    int32 id;
    string message;

reply delivered {

The protocol compiler only creates encoders/decoders for the messages. It doesn't care how you transfer them; it could be via ACNET, TCP, or simply written to a file. In this example, we're going to use ACNET and we've added fields to the messages which lend themselves to ACNET communication. Communication between chat clients and the server works like this:

  • The chat server creates a CHAT ACNET handle. This is the handle where clients will send their messages.
  • A client sends a register request to the server as a multiple-reply request. The nickname field will get filled with the user's nickname. The first reply for the request will be a registered reply where the id field holds a unique value for the client.
  • When a client wants to send a chat message, it send a single-reply request using the deliver request. the id field is the client's unique ID and the message field holds the text. The server will return a single delivered reply. Each text message is a new, single-reply request.
  • When the server receives a deliver request, it translates it into a message reply (filling in when the message arrived, who sent it, and the message.) This reply is sent to all open multiple-reply requests. Viewed another way, a client makes a multiple-reply request. The first reply is a registered reply. All remaining replies are message replies.
  • When a client cancels the initial, multiple-reply request, the server removes it from its tables/resources.

Running this source through the protocol compiler ("pc -l kotlin Chat.proto") yields chat_protocol.kt.