buffer 0.0.5
A protocol buffer & RPC library
To use this package, run the following command in your project's root directory:
						Manual usage
						
						Put the following dependency into your project's dependences section:
					
A simple and practical protocol buffer & RPC library.
Quick Start:
import std.stdio;
import buffer.message;
mixin (LoadBufferFile!"message.buffer");
mixin (LoadBufferScript!`
	message(3) Sample {
		string	name;
		int32	age;
		int16	sex;
	}
`);
// Simple:
void main()
{
    Sample sample = new Sample();
    sample.name = "Tom";
    sample.age = 20;
    sample.sex = 1;
    ubyte[] buf = sample.serialize();
    writeln(buf);
    Sample sam = Message.deserialize!Sample(buf);
    writeln("msgid:\t", sam.messageId);
    writeln("name:\t",  sam.name);
    writeln("age:\t",   sam.age);
    writeln("sex:\t",   sam.sex);
}
// Advanced:
void main()
{
    // Set magic number, encryption method and key.
    Message.settings(1229, CryptType.XTEA, "1234");
    Sample sample = new Sample();
    sample.name = "Tom";
    sample.age = 20;
    sample.sex = 1;
    ubyte[] buf = sample.serialize();
    writeln(buf);
	
	ushort messageId;
    TypeInfo_Class messageClass;
    string method;
    Message.getMessageInfo(buf, messageId, messageClass, method);
    switch (messageClass.name)
    {
    case "app.Sample":
        Sample sam = Message.deserialize!Sample(buf);
        writeln("msgid:\t", sam.messageId);
        writeln("name:\t",  sam.name);
        writeln("age:\t",   sam.age);
        writeln("sex:\t",   sam.sex);
        break;
    case "...":
        break;
    default:
        break;
    }
}
// RPC Client:
mixin(LoadBufferScript!`
    message(1) LoginInfo {
        string name;
        string password;
    }
    message(2) LoginRetInfo {
        int32  id;
        string name;
    }
`);
ubyte[] TcpRequestHandler(ubyte[] data)
{
    TcpSocket socket = new TcpSocket();
    socket.blocking = true;
    socket.bind(new InternetAddress("127.0.0.1", 0));
    socket.connect(new InternetAddress("127.0.0.1", 10000));
    socket.send(data);
    ubyte[] rec_data = new ubyte[1024];
    long len = socket.receive(rec_data);
    socket.close();
    return rec_data[0..len];
}
void main()
{
    Message.settings(1229, CryptType.XTEA, "1234");
    Client.bindTcpRequestHandler(data => TcpRequestHandler(data));
    LoginRetInfo ret = Client.call!(LoginInfo, LoginRetInfo)("Login", "admin", "123456");
    if (ret !is null)
    {
        writeln(ret.id);
        writeln(ret.name);
    }
}
// RPC Server:
class Business
{
    mixin(LoadBufferScript!`
        message(1) LoginInfo {
            string name;
            string password;
        }
        message(2) LoginRetInfo {
            int32  id;
            string name;
        }
    `);
    LoginRetInfo Login(string name, string password)
    {
        // Access the database, check the user name and password, assuming the validation passed, the user's ID is 1
        int userId = 1;
        // ...
        // Check OK.
        LoginRetInfo ret = new LoginRetInfo();
        ret.id = userId;
        ret.name = name;
        return ret;
    }
}
__gshared Server!(Business) server;
void main()
{
    Message.settings(1229, CryptType.XTEA, "1234");
    server = new Server!(Business)();
    TcpSocket socket = new TcpSocket();
    socket.blocking = true;
    socket.bind(new InternetAddress("127.0.0.1", 10000));
    socket.listen(10);
    while (true)
    {
        Socket accept = socket.accept();
        spawn(&acceptHandler, cast(shared Socket) accept);
    }
}
void acceptHandler(shared Socket accept)
{
    Socket socket = cast(Socket) accept;
    while (true)
    {
        ubyte[] data = new ubyte[1024];
        long len = socket.receive(data);
        if (len > 0)
        {
            ubyte[] ret_data = server.Handler(data[0..len]);
            if (ret_data != null)
                socket.send(ret_data);
        }
    }
}
- 0.0.5 released 8 years ago
- shove70/buffer
- MIT License
- Copyright © 2017, Shove
- Authors:
- Dependencies:
- cryption
- Versions:
- 
						
 Show all 74 versions0.2.41 2022-Jan-25 0.2.40 2021-Jun-09 0.2.39 2020-Dec-03 0.2.38 2020-Oct-14 0.2.37 2020-Sep-01 
- Download Stats:
- 
						- 
								0 downloads today 
- 
								3 downloads this week 
- 
								3 downloads this month 
- 
								8309 downloads total 
 
- 
								
- Score:
- 2.1
- Short URL:
- buffer.dub.pm