using System;
using System.IO;
using System.Text;

public interface IRpcHandler 
{
    /// <summary>
    /// 处理服务器发来的RPC消息
    /// </summary>
    /// <param name="inStream">消息正文</param>
    /// <param name="outData">回复的数据缓存，把回复内容写在这里</param>
    /// <param name="reply">回复的委托，调用回复消息，参数是回复数组的有效大小</param>
    void Handle(MemoryStream inStream, byte[] outData, Action<int> reply);
}

public abstract class ARpcHandler : IRpcHandler
{
    public void Handle(MemoryStream inStream, byte[] outData, Action<int> reply)
    {
        string msg = Encoding.UTF8.GetString(inStream.GetBuffer(), (int)inStream.Position, (int)(inStream.Length - inStream.Position));
        Handle(msg, (replyMsg)=> {
            int size = Encoding.UTF8.GetBytes(replyMsg, 0, replyMsg.Length, outData, 0);
            reply.Invoke(size);
        });
    }

    protected abstract void Handle(string inMsg, Action<string> reply);
}
