-
Notifications
You must be signed in to change notification settings - Fork 7
RPC socket usage
Jasonsalex edited this page Aug 15, 2017
·
1 revision
- module KissRpc.RpcSocketBaseInterface, inherit RpcSocketBaseInterface,
- int getFd(); // get socket handle
- string getIp(); //get socket ip address
- string getPort(); // get socket port
- void disconnect(); //close socket
- module KissRpc.RpcSocketBaseInterface, inherit ClientSocketEventInterface,
- connectd(RpcSocketBaseInterface socket); //connect to server is ok
- disconnectd(RpcSocketBaseInterface socket); // disconnect from server
- writeFailed(RpcSocketBaseInterface socket); // write data to server is failed
- readFailed(RpcSocketBaseInterface socket); //read data from server is failed
- client socket demo
import KissRpc.RpcClient;
import KissRpc.RpcSocketBaseInterface;
import KissRpc.Logs;
import kiss.event.GroupPoll;
class ClientSocket : ClientSocketEventInterface
{
this()
{
rpClient = new RpcClient(this);
// rpClient.setSocketCompress(RPC_PACKAGE_COMPRESS_TYPE.RPCT_DYNAMIC); //bind socket compress
}
void connectToServer(GroupPoll!() poll)
{
rpClient.connect("0.0.0.0", 4444, poll);
}
void connectd(RpcSocketBaseInterface socket)
{
writefln("connect to server, %s:%s", socket.getIp, socket.getPort);
}
void disconnectd(RpcSocketBaseInterface socket)
{
writefln("client disconnect ....");
}
void writeFailed(RpcSocketBaseInterface socket)
{
deWritefln("client write failed , %s:%s", socket.getIp, socket.getPort);
}
void readFailed(RpcSocketBaseInterface socket)
{
deWritefln("client read failed , %s:%s", socket.getIp, socket.getPort);
}
private:
RpcClient rpClient;
}
- start client rpc
void main()
{
auto poll = new GroupPoll!();
auto client = new ClientSocket;
client.connectToServer(poll);
poll.start;
poll.wait;
}
- module KissRpc.RpcSocketBaseInterface, inherit ServerSocketEventInterface
- listenFailed(const string str); //start server is failed
- inconming(RpcSocketBaseInterface socket); // client socket imconming
- disconnectd(RpcSocketBaseInterface socket); //client socket disconnect
- writeFailed(RpcSocketBaseInterface socket); //client socket write failed
- readFailed(RpcSocketBaseInterface socket); //client socket read failed
- server socket demo
import KissRpc.Unit;
import KissRpc.Logs;
import KissRpc.RpcServer;
import KissRpc.RpcSocketBaseInterface;
import kiss.event.GroupPoll;
class ServerSocket : ServerSocketEventInterface
{
void listenFailed(const string str)
{
deWriteln("server listen failed", str);
}
void disconnectd(RpcSocketBaseInterface socket)
{
deWriteln("client is disconnect");
}
void inconming(RpcSocketBaseInterface socket)
{
logInfo("client inconming:%s:%s, connect num:%s", socket.getIp, socket.getPort, connect_num++);
}
void writeFailed(RpcSocketBaseInterface socket)
{
deWritefln("write buffer to client is failed, %s:%s", socket.getIp, socket.getPort);
}
void readFailed(RpcSocketBaseInterface socket)
{
deWritefln("read buffer from client is failed, %s:%s", socket.getIp, socket.getPort);
}
}
- start server demo
void main(string[] args)
{
import KissRpc.IDL.kissidlService;
auto rpServer = new RpcServer(new ServerSocket);
auto address_book_service = new RpcAddressBookService(rpServer);
auto poll = new GroupPoll!();
if(rpServer.listen("0.0.0.0", 4444, poll))
{
logInfo("start server is ok");
}
poll.start();
poll.wait();
}