mirror of https://github.com/deavminet/dnetd.git
WIP: Plugin system
This commit is contained in:
parent
6a679613cc
commit
a246ef1d73
1
dub.json
1
dub.json
|
@ -4,6 +4,7 @@
|
|||
],
|
||||
"copyright": "Copyright © 2020, Tristan B. Kildaire",
|
||||
"dependencies": {
|
||||
"bformat": "~>1.0.8",
|
||||
"gogga": "~>0.0.2",
|
||||
"tristanable": "~>0.0.33"
|
||||
},
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/**
|
||||
* dplugin
|
||||
*
|
||||
* Represents a dplugin
|
||||
*
|
||||
* On initialization it connects over a UNIX domain socket
|
||||
* to the server and then the plugin can be used whenver it
|
||||
* needs to be (send and receive)
|
||||
*/
|
||||
|
||||
module dnetd.dplugin;
|
||||
|
||||
import dnetd.dserver;
|
||||
import std.socket;
|
||||
import bmessage;
|
||||
|
||||
public final class DPlugin
|
||||
{
|
||||
/* The associated server */
|
||||
private DServer server;
|
||||
|
||||
/* The UNIX domaion socket path */
|
||||
private string unixDomainSocketPath;
|
||||
|
||||
/**
|
||||
* Constructs a new DPugin associated with the
|
||||
* given server and with the intent to connect
|
||||
* to the UNIX domain socket at the path given
|
||||
*/
|
||||
this(DServer server, string unixDomainSocketPath)
|
||||
{
|
||||
this.server = server;
|
||||
this.unixDomainSocketPath = unixDomainSocketPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a new session to the plugin server
|
||||
* then sends the data, awaits a reply, then
|
||||
* closes the session (connection)
|
||||
*/
|
||||
public byte[] sendPlugin(byte[] data)
|
||||
{
|
||||
/* The response */
|
||||
byte[] response;
|
||||
|
||||
/* The status */
|
||||
bool status;
|
||||
|
||||
/* Open a connection to the plugin server */
|
||||
Socket socket = new Socket(AddressFamily.UNIX, SocketType.STREAM, ProtocolType.RAW);
|
||||
socket.connect(new UnixAddress(unixDomainSocketPath));
|
||||
|
||||
/* Send the data */
|
||||
/* TODO: Error handling */
|
||||
status = sendMessage(socket, data);
|
||||
|
||||
/* Get the reply */
|
||||
/* TODO: Error handling */
|
||||
byte[] reply;
|
||||
receiveMessage(socket, reply);
|
||||
|
||||
/* Close the connetion to the plugin server */
|
||||
socket.close();
|
||||
|
||||
/* Encode the status in the reply */
|
||||
response ~= [status];
|
||||
|
||||
/* Encode the response */
|
||||
response ~= reply;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue