1 /** 2 * dplugin 3 * 4 * Represents a dplugin 5 * 6 * On initialization it connects over a UNIX domain socket 7 * to the server and then the plugin can be used whenver it 8 * needs to be (send and receive) 9 */ 10 11 module dnetd.dplugin; 12 13 import dnetd.dserver; 14 import std.socket; 15 import bmessage; 16 17 public final class DPlugin 18 { 19 /* The associated server */ 20 private DServer server; 21 22 /* The UNIX domaion socket path */ 23 private string unixDomainSocketPath; 24 25 /** 26 * Constructs a new DPugin associated with the 27 * given server and with the intent to connect 28 * to the UNIX domain socket at the path given 29 */ 30 this(DServer server, string unixDomainSocketPath) 31 { 32 this.server = server; 33 this.unixDomainSocketPath = unixDomainSocketPath; 34 } 35 36 /** 37 * Opens a new session to the plugin server 38 * then sends the data, awaits a reply, then 39 * closes the session (connection) 40 */ 41 public byte[] sendPlugin(byte[] data) 42 { 43 /* The response */ 44 byte[] response; 45 46 /* The status */ 47 bool status; 48 49 /* Open a connection to the plugin server */ 50 Socket socket = new Socket(AddressFamily.UNIX, SocketType.STREAM, ProtocolType.RAW); 51 socket.connect(new UnixAddress(unixDomainSocketPath)); 52 53 /* Send the data */ 54 /* TODO: Error handling */ 55 status = sendMessage(socket, data); 56 57 /* Encode the status in the reply */ 58 response ~= [status]; 59 60 /* If the send succeeded */ 61 if(status) 62 { 63 /* Get the reply */ 64 /* TODO: Error handling */ 65 byte[] reply; 66 receiveMessage(socket, reply); 67 68 /* Close the connetion to the plugin server */ 69 socket.close(); 70 71 /* Encode the response */ 72 response ~= reply; 73 } 74 75 return response; 76 } 77 78 79 80 }