1 module cassandra.internal.tcpconnection; 2 3 version(Have_vibe_d) { 4 pragma(msg, "build cassandra-d with vibe"); 5 public import vibe.core.net : TCPConnection, connectTCP; 6 } else { 7 pragma(msg, "build cassandra-d no vibe"); 8 import std.socket; 9 10 interface TCPConnection { 11 @property bool connected(); 12 void read(ref ubyte[] buf); 13 void write(ubyte[] buf); 14 void close(); 15 void flush(); 16 } 17 18 class TCPConnectionImpl :TCPConnection { 19 TcpSocket _socket; 20 21 this(string host, short port) { 22 _socket = new TcpSocket(); 23 assert(_socket, "Socket not created"); 24 _socket.connect(new InternetAddress(host, port)); 25 } 26 27 @property 28 bool connected() { 29 return _socket.isAlive; 30 } 31 32 void read(ref ubyte[] buf) { 33 assert(_socket, "Socket not created"); 34 auto n = _socket.receive(buf); 35 assert(n==buf.length, "receive didn't full buffer!"); 36 } 37 38 void write(ubyte[] buf) { 39 assert(_socket, "Socket not created"); 40 auto n = _socket.send(buf); 41 assert(n==buf.length, "didn't send full buffer!"); 42 } 43 44 void close() { 45 assert(_socket, "Socket not created"); 46 _socket.shutdown(SocketShutdown.BOTH); 47 _socket.close(); 48 } 49 50 void flush() {} 51 } 52 53 TCPConnection connectTCP(string host, short port) { 54 auto ret = new TCPConnectionImpl(host, port); 55 return ret; 56 } 57 }