2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
|
|
|
#include <Console.h>
|
|
|
|
#include <Export.h>
|
|
|
|
#include <PluginManager.h>
|
2011-11-25 14:14:36 -07:00
|
|
|
#include <zmq.hpp>
|
2011-11-25 15:21:31 -07:00
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2011-11-25 14:14:36 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
// Here go all the command declarations...
|
|
|
|
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result server (color_ostream &out, std::vector <std::string> & parameters);
|
2011-11-25 14:14:36 -07:00
|
|
|
|
|
|
|
// A plugins must be able to return its name. This must correspond to the filename - skeleton.plug.so or skeleton.plug.dll
|
2012-02-21 10:19:17 -07:00
|
|
|
DFHACK_PLUGIN("server");
|
2011-11-25 14:14:36 -07:00
|
|
|
|
|
|
|
// Mandatory init function. If you have some global state, create it here.
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
2011-11-25 14:14:36 -07:00
|
|
|
{
|
|
|
|
// Fill the command list with your commands.
|
|
|
|
commands.clear();
|
|
|
|
commands.push_back(PluginCommand("server",
|
|
|
|
"Inane zeromq example turned into a plugin.",
|
|
|
|
server));
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called right before the plugin library is removed from memory.
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
2011-11-25 14:14:36 -07:00
|
|
|
{
|
|
|
|
// You *MUST* kill all threads you created before this returns.
|
|
|
|
// If everythin fails, just return CR_FAILURE. Your plugin will be
|
|
|
|
// in a zombie state, but things won't crash.
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is WRONG and STUPID. Never use this as an example!
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result server (color_ostream &out, std::vector <std::string> & parameters)
|
2011-11-25 14:14:36 -07:00
|
|
|
{
|
|
|
|
// It's nice to provide a 'help' option for your command.
|
|
|
|
// It's also nice to print the same help if you get invalid options from the user instead of just acting strange
|
|
|
|
for(int i = 0; i < parameters.size();i++)
|
|
|
|
{
|
|
|
|
if(parameters[i] == "help" || parameters[i] == "?")
|
|
|
|
{
|
|
|
|
// Core has a handle to the console. The console is thread-safe.
|
|
|
|
// Only one thing can read from it at a time though...
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("This command is a simple Hello World example for zeromq!\n");
|
2011-11-25 14:14:36 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Prepare our context and socket
|
|
|
|
zmq::context_t context (1);
|
|
|
|
zmq::socket_t socket (context, ZMQ_REP);
|
|
|
|
socket.bind ("tcp://*:5555");
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
zmq::message_t request;
|
|
|
|
|
|
|
|
// Wait for next request from client
|
|
|
|
socket.recv (&request);
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("Received Hello\n");
|
2011-11-25 14:14:36 -07:00
|
|
|
|
|
|
|
// Do some 'work'
|
2011-11-25 15:21:31 -07:00
|
|
|
#ifdef LINUX_BUILD
|
2011-11-25 14:14:36 -07:00
|
|
|
sleep (1);
|
2011-11-25 15:21:31 -07:00
|
|
|
#else
|
|
|
|
Sleep(1000);
|
|
|
|
#endif
|
2011-11-25 14:14:36 -07:00
|
|
|
|
|
|
|
// Send reply back to client
|
|
|
|
zmq::message_t reply (5);
|
|
|
|
memcpy ((void *) reply.data (), "World", 5);
|
|
|
|
socket.send (reply);
|
|
|
|
}
|
|
|
|
return CR_OK;
|
2012-02-13 21:54:08 -07:00
|
|
|
}
|