Skip to content

RPC socket usage

Jasonsalex edited this page Aug 15, 2017 · 1 revision

socket base interface

  • module KissRpc.RpcSocketBaseInterface, inherit RpcSocketBaseInterface,
  1. int getFd(); // get socket handle
  2. string getIp(); //get socket ip address
  3. string getPort(); // get socket port
  4. void disconnect(); //close socket

client socket event

  • module KissRpc.RpcSocketBaseInterface, inherit ClientSocketEventInterface,
  1. connectd(RpcSocketBaseInterface socket); //connect to server is ok
  2. disconnectd(RpcSocketBaseInterface socket); // disconnect from server
  3. writeFailed(RpcSocketBaseInterface socket); // write data to server is failed
  4. 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;
}

server socket event

  • module KissRpc.RpcSocketBaseInterface, inherit ServerSocketEventInterface
  1. listenFailed(const string str); //start server is failed
  2. inconming(RpcSocketBaseInterface socket); // client socket imconming
  3. disconnectd(RpcSocketBaseInterface socket); //client socket disconnect
  4. writeFailed(RpcSocketBaseInterface socket); //client socket write failed
  5. 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();
}
Clone this wiki locally