msgtrans dlang
v0.1.4
Структура транспорта сообщений. На основе протоколов передачи TCP, WebSocket, UDP.
import msgtrans;
import hunt.logging;
void main ()
{
MessageTransportServer server = new MessageTransportServer( " test " );
server.addChannel( new TcpServerChannel ( 9001 ));
server.addChannel( new WebSocketServerChannel ( 9002 , " /ws " ));
server.acceptor((TransportContext ctx) {
infof( " New connection: id=%d " , ctx.id());
});
server.start();
}
@TransportServer( " test " )
@TransportClient( " test " )
class MyExecutor : AbstractExecutor !(MyExecutor)
{
@MessageId( MESSAGE . HELLO )
void hello (TransportContext ctx, MessageBuffer buffer)
{
string name = cast ( string ) buffer.data;
string welcomeText = " Hello " ~ name;
ctx.send( new MessageBuffer ( MESSAGE . WELCOME , cast ( ubyte []) welcomeText));
}
}
import msgtrans;
import hunt.logging;
void main ()
{
MessageTransportClient client = new MessageTransportClient( " test " );
client.channel( new TcpClientChannel ( " 127.0.0.1 " , 9001 )).connect();
string name = " zoujiaqing " ;
auto buffer = new MessageBuffer( MESSAGE . HELLO , name. dup );
client.send(buffer);
getchar();
client.close();
}
@TransportClient( " test " )
class MyExecutor : AbstractExecutor !(MyExecutor)
{
@MessageId( MESSAGE . WELCOME )
void welcome (TransportContext ctx, MessageBuffer buffer)
{
auto message = unserialize ! WelcomeMessage(buffer.data);
infof( " message: %s " , message.welcome);
}
}