1+ #include < dfhack/Core.h>
2+ #include < dfhack/Console.h>
3+ #include < dfhack/Export.h>
4+ #include < dfhack/PluginManager.h>
5+ #include < zmq.hpp>
6+ using namespace DFHack ;
7+
8+ // Here go all the command declarations...
9+ // mostly to allow having the mandatory stuff on top of the file and commands on the bottom
10+ DFhackCExport command_result server (Core * c, std::vector <std::string> & parameters);
11+
12+ // A plugins must be able to return its name. This must correspond to the filename - skeleton.plug.so or skeleton.plug.dll
13+ DFhackCExport const char * plugin_name ( void )
14+ {
15+ return " server" ;
16+ }
17+
18+ // Mandatory init function. If you have some global state, create it here.
19+ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
20+ {
21+ // Fill the command list with your commands.
22+ commands.clear ();
23+ commands.push_back (PluginCommand (" server" ,
24+ " Inane zeromq example turned into a plugin." ,
25+ server));
26+ return CR_OK ;
27+ }
28+
29+ // This is called right before the plugin library is removed from memory.
30+ DFhackCExport command_result plugin_shutdown ( Core * c )
31+ {
32+ // You *MUST* kill all threads you created before this returns.
33+ // If everythin fails, just return CR_FAILURE. Your plugin will be
34+ // in a zombie state, but things won't crash.
35+ return CR_OK ;
36+ }
37+
38+ // This is WRONG and STUPID. Never use this as an example!
39+ DFhackCExport command_result server (Core * c, std::vector <std::string> & parameters)
40+ {
41+ // It's nice to provide a 'help' option for your command.
42+ // It's also nice to print the same help if you get invalid options from the user instead of just acting strange
43+ for (int i = 0 ; i < parameters.size ();i++)
44+ {
45+ if (parameters[i] == " help" || parameters[i] == " ?" )
46+ {
47+ // Core has a handle to the console. The console is thread-safe.
48+ // Only one thing can read from it at a time though...
49+ c->con .print (" This command is a simple Hello World example for zeromq!\n " );
50+ return CR_OK ;
51+ }
52+ }
53+ // Prepare our context and socket
54+ zmq::context_t context (1 );
55+ zmq::socket_t socket (context, ZMQ_REP );
56+ socket.bind (" tcp://*:5555" );
57+
58+ while (true )
59+ {
60+ zmq::message_t request;
61+
62+ // Wait for next request from client
63+ socket.recv (&request);
64+ c->con .print (" Received Hello\n " );
65+
66+ // Do some 'work'
67+ sleep (1 );
68+
69+ // Send reply back to client
70+ zmq::message_t reply (5 );
71+ memcpy ((void *) reply.data (), " World" , 5 );
72+ socket.send (reply);
73+ }
74+ return CR_OK ;
75+ }
0 commit comments