1 module kissrpc.RpcClientSocket; 2 3 import kissrpc.RpcRecvPackageManage; 4 import kissrpc.RpcEventInterface; 5 import kissrpc.RpcSocketBaseInterface; 6 import kissrpc.Unit; 7 import kissrpc.Logs; 8 9 // import kiss.aio.AsyncTcpBase; 10 // import kiss.event.Poll; 11 // import kiss.time.Timer; 12 // import kiss.aio.AsyncTcpClient; 13 14 import std.stdio; 15 import std.socket; 16 import std.conv; 17 18 import core.thread; 19 20 21 import kiss.aio.AsynchronousChannelSelector; 22 import kiss.aio.ByteBuffer; 23 import kiss.net.TcpClient; 24 25 class RpcClientSocket: TcpClient, RpcSocketBaseInterface 26 { 27 public: 28 this(string ip, ushort port, AsynchronousChannelSelector sel, RpcEventInterface rpcEventDelegate) { 29 super(ip, port, sel, RPC_PACKAGE_MAX); 30 _packageManage = new RpcRecvPackageManage(this, rpcEventDelegate); 31 _socketEventDelegate = rpcEventDelegate; 32 } 33 bool write(byte[] buf) { 34 // writeln("write index ",index++); 35 super.doWrite(buf); 36 return true; 37 } 38 override void onConnectCompleted(void* attachment) { 39 doRead(); 40 _socketEventDelegate.socketEvent(this, SOCKET_STATUS.SE_CONNECTD, "connect to server is ok!"); 41 } 42 override void onConnectFailed(void* attachment) { 43 //writeln("onConnectFailed"); 44 } 45 override void onWriteCompleted(void* attachment, size_t count , ByteBuffer buffer) { 46 // writeln("write success index ",index); 47 } 48 override void onWriteFailed(void* attachment) { 49 //writeln("onWriteFailed"); 50 _socketEventDelegate.socketEvent(this, SOCKET_STATUS.SE_WRITE_FAILED, "write data to server is failed"); 51 } 52 override void onReadCompleted(void* attachment, size_t count , ByteBuffer buffer) { 53 54 _packageManage.add(cast(ubyte[])(buffer.getCurBuffer())); 55 } 56 override void onReadFailed(void* attachment) { 57 //writeln("onReadFailed"); 58 } 59 override void onClose() { 60 _socketEventDelegate.socketEvent(this, SOCKET_STATUS.SE_DISCONNECTD, "disconnect from server!"); 61 } 62 override void reConnect() { 63 //writeln("RpcClientSocket reConnect"); 64 super.reConnect(); 65 } 66 67 void disconnect() 68 { 69 close(); 70 } 71 int getFd() { return cast(int)(fd()); } 72 string getIp() { return ip(); } 73 string getPort() { return port(); } 74 75 76 private: 77 RpcEventInterface _socketEventDelegate; 78 RpcRecvPackageManage _packageManage; 79 long index = 1; 80 } 81