brpc server supports all protocols in the same port, and it makes deployment and maintenance more convenient in most of the time. Since the format of different protocols is very different, it is hard to support all protocols in the same port unambiguously. In consider of decoupling and extensibility, it is also hard to build a multiplexer for all protocols. Thus our way is to classify all protocols into three categories and try one by one:
...
...
@@ -8,5 +8,145 @@ brpc server supports all protocols in the same port, and it makes deployment and
Considering that there will be only one protocol in most connections, we record the result of last selection so that it will be tried first when further data comes. It reduces the overhead of matching protocols to nearly zero for long connections. Although the process of matching protocols will be run everytime for short connections, the bottleneck of short connections is not in here and this method is still fast enough. If there are lots of new protocols added into brpc in the future, we may consider some heuristic methods to match protocols.
# Multi-protocol support in client side
# Multi-protocol support in the client side
Unlike the server side that protocols must be dynamically determined based on the data on the connection, the client side as the originator, naturally know their own protocol format. As long as the protocol data is sent through connection pool or short connection, which means it has exclusive usage of that connection, then the protocol can have any complex (or bad) format. Since the client will record the protocol when sending the data, it will use that recored protocol to parse the data without any matching overhead when responses come back. There is no magic number in some protocols like memcache, redis, it is hard to distinguish them in the server side, but it is no problem in the client side.
# Support new protocols
brpc is designed to add new protocols at any time, just proceed as follows:
> The protocol that begins with nshead has unified support, just read [this](nshead_service.md).
## add ProtocolType
Add new protocol type in ProtocolType in [options.proto](https://github.com/brpc/brpc/blob/master/src/brpc/options.proto). If you need to add new protocol, please contact us to add it for you to make sure there is no conflict with protocols of others.
Currently we support in ProtocolType(at the end of 2016):
```c++
enumProtocolType{
PROTOCOL_UNKNOWN=0;
PROTOCOL_BAIDU_STD=1;
PROTOCOL_STREAMING_RPC=2;
PROTOCOL_HULU_PBRPC=3;
PROTOCOL_SOFA_PBRPC=4;
PROTOCOL_RTMP=5;
PROTOCOL_HTTP=6;
PROTOCOL_PUBLIC_PBRPC=7;
PROTOCOL_NOVA_PBRPC=8;
PROTOCOL_NSHEAD_CLIENT=9;// implemented in brpc-ub
PROTOCOL_NSHEAD=10;
PROTOCOL_HADOOP_RPC=11;
PROTOCOL_HADOOP_SERVER_RPC=12;
PROTOCOL_MONGO=13;// server side only
PROTOCOL_UBRPC_COMPACK=14;
PROTOCOL_DIDX_CLIENT=15;// Client side only
PROTOCOL_REDIS=16;// Client side only
PROTOCOL_MEMCACHE=17;// Client side only
PROTOCOL_ITP=18;
PROTOCOL_NSHEAD_MCPACK=19;
PROTOCOL_DISP_IDL=20;// Client side only
PROTOCOL_ERSDA_CLIENT=21;// Client side only
PROTOCOL_UBRPC_MCPACK2=22;// Client side only
}
```
## Implement Callbacks
All callbacks are defined in struct Protocol, which is defined in [protocol.h](https://github.com/brpc/brpc/blob/master/src/brpc/protocol.h). Among all these callbacks, `parse` is a callback that must be implmented. Besides, `process_request` must be implemented in the server side and `serialize_request`, `pack_request`, `process_response` must be implemented in the client side.
It is difficult to implement callbacks of the protocol. These codes is not like the codes that ordinary users use which has good prompts and protections. You have to figure it out how to handle similar code in other protocols and implement your own protocol, then send it to us to do code review.