Skip to content

Instantly share code, notes, and snippets.

@unarist
Created November 1, 2012 00:13
Show Gist options
  • Save unarist/3990787 to your computer and use it in GitHub Desktop.
Save unarist/3990787 to your computer and use it in GitHub Desktop.
Waku2Rpc for C# を実装した夢を見たんです
class Program : IClientMethods
{
void Main()
{
IWaku2Rpc<IServerMethods> client = Waku2Rpc.Create<IServerMethods>(this);
client.Chat(new Msg { name="", msg="", dummy=0 });
}
public void ChatNotify(int id, string name, string msg)
{
}
public void Info(string msg)
{
}
}
interface IWaku2Rpc : IDisposable
{
void Connect(string host, int port);
void Open(IWebSocket sock);
// receiverが実装しているプロトコルを洗い出してReceiveMethod作って登録
void AddReceiver(object receiver);
}
interface IWaku2Rpc<T> : IWaku2Rpc
{
T Methods {get;} // return (T)this
}
abstract class Waku2Rpc : IWaku2Rpc
{
//Waku2Rpcにサーバー呼び出すメソッドを追加したものをコンパイルしてインスタンスを返す
public static IWaku2Rpc<T> Create<T>() {}
public static IWaku2Rpc<T> Create<T>(object receiver)
{
var client = Create<T>();
client.AddReceiver(receiver);
return client;
}
// メッセージを受け取ってfuncidに応じてアンパック・呼び出しまでするメソッドを作る
public static Action<byte[], TInterface> CreateReceiveMethod<TInterface>();
}
// 以下
// https://github.com/kyubuns/waku2rpc/blob/master/example_chat/example_chat.yuu
// から自動生成
[module:Waku2Rpc.Protocol("yuu hash")]
namespace ExampleChat
{
class Msg
{
string name {get;set;}
string msg {get;set;} //PascalCaseにしたいけどクラス名と被る
int dummy {get;set;}
}
interface IServerMethods
{
[Waku2Rpc.Function(1)]
void Chat(Msg msg);
}
interface IClientMethods
{
[Waku2Rpc.Function(2)]
void ChatNotify(int id, string name, string msg);
[Waku2Rpc.Function(3)]
void Info(string msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment