2012-10-29 15:18:50 -06:00
|
|
|
#include "Core.h"
|
2012-10-29 15:56:22 -06:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
2012-10-29 15:18:50 -06:00
|
|
|
|
2012-10-29 15:56:22 -06:00
|
|
|
#include "DataDefs.h"
|
2012-10-29 15:18:50 -06:00
|
|
|
#include "df/graphic.h"
|
|
|
|
#include "df/enabler.h"
|
2012-10-29 15:56:22 -06:00
|
|
|
#include "df/renderer.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include "PassiveSocket.h"
|
|
|
|
#include "tinythread.h"
|
2012-10-29 15:18:50 -06:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
2012-10-29 15:56:22 -06:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2012-10-29 15:18:50 -06:00
|
|
|
using df::global::gps;
|
|
|
|
using df::global::enabler;
|
|
|
|
|
|
|
|
// The error messages are taken from the clsocket source code
|
|
|
|
const char * translate_socket_error(CSimpleSocket::CSocketError err) {
|
|
|
|
switch (err) {
|
|
|
|
case CSimpleSocket::SocketError:
|
|
|
|
return "Generic socket error translates to error below.";
|
|
|
|
case CSimpleSocket::SocketSuccess:
|
|
|
|
return "No socket error.";
|
|
|
|
case CSimpleSocket::SocketInvalidSocket:
|
|
|
|
return "Invalid socket handle.";
|
|
|
|
case CSimpleSocket::SocketInvalidAddress:
|
|
|
|
return "Invalid destination address specified.";
|
|
|
|
case CSimpleSocket::SocketInvalidPort:
|
|
|
|
return "Invalid destination port specified.";
|
|
|
|
case CSimpleSocket::SocketConnectionRefused:
|
|
|
|
return "No server is listening at remote address.";
|
|
|
|
case CSimpleSocket::SocketTimedout:
|
|
|
|
return "Timed out while attempting operation.";
|
|
|
|
case CSimpleSocket::SocketEwouldblock:
|
|
|
|
return "Operation would block if socket were blocking.";
|
|
|
|
case CSimpleSocket::SocketNotconnected:
|
|
|
|
return "Currently not connected.";
|
|
|
|
case CSimpleSocket::SocketEinprogress:
|
|
|
|
return "Socket is non-blocking and the connection cannot be completed immediately";
|
|
|
|
case CSimpleSocket::SocketInterrupted:
|
|
|
|
return "Call was interrupted by a signal that was caught before a valid connection arrived.";
|
|
|
|
case CSimpleSocket::SocketConnectionAborted:
|
|
|
|
return "The connection has been aborted.";
|
|
|
|
case CSimpleSocket::SocketProtocolError:
|
|
|
|
return "Invalid protocol for operation.";
|
|
|
|
case CSimpleSocket::SocketFirewallError:
|
|
|
|
return "Firewall rules forbid connection.";
|
|
|
|
case CSimpleSocket::SocketInvalidSocketBuffer:
|
|
|
|
return "The receive buffer point outside the process's address space.";
|
|
|
|
case CSimpleSocket::SocketConnectionReset:
|
|
|
|
return "Connection was forcibly closed by the remote host.";
|
|
|
|
case CSimpleSocket::SocketAddressInUse:
|
|
|
|
return "Address already in use.";
|
|
|
|
case CSimpleSocket::SocketInvalidPointer:
|
|
|
|
return "Pointer type supplied as argument is invalid.";
|
|
|
|
case CSimpleSocket::SocketEunknown:
|
|
|
|
return "Unknown error please report to mark@carrierlabs.com";
|
|
|
|
default:
|
|
|
|
return "No such CSimpleSocket error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Owns the thread that accepts TCP connections and forwards messages to clients;
|
|
|
|
// has a mutex
|
|
|
|
class client_pool {
|
|
|
|
typedef tthread::mutex mutex;
|
|
|
|
|
|
|
|
mutex clients_lock;
|
|
|
|
std::vector<CActiveSocket *> clients;
|
|
|
|
|
|
|
|
// TODO - delete this at some point
|
|
|
|
tthread::thread * accepter;
|
|
|
|
|
|
|
|
static void accept_clients(void * client_pool_pointer) {
|
|
|
|
client_pool * p = reinterpret_cast<client_pool *>(client_pool_pointer);
|
|
|
|
CPassiveSocket socket;
|
|
|
|
socket.Initialize();
|
2012-10-29 15:56:22 -06:00
|
|
|
if (socket.Listen((const uint8_t *)"0.0.0.0", 8008)) {
|
2012-10-29 15:18:50 -06:00
|
|
|
std::cout << "Listening on a socket" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "Not listening: " << socket.GetSocketError() << std::endl;
|
|
|
|
std::cout << translate_socket_error(socket.GetSocketError()) << std::endl;
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
CActiveSocket * client = socket.Accept();
|
|
|
|
if (client != 0) {
|
|
|
|
lock l(*p);
|
|
|
|
p->clients.push_back(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
class lock {
|
|
|
|
tthread::lock_guard<mutex> l;
|
|
|
|
public:
|
|
|
|
lock(client_pool & p)
|
|
|
|
: l(p.clients_lock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
friend class client_pool::lock;
|
|
|
|
|
|
|
|
client_pool() {
|
|
|
|
accepter = new tthread::thread(accept_clients, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// MUST have lock
|
|
|
|
bool has_clients() {
|
|
|
|
return !clients.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// MUST have lock
|
|
|
|
void add_client(CActiveSocket * sock) {
|
|
|
|
clients.push_back(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// MUST have lock
|
|
|
|
void broadcast(const std::string & message) {
|
|
|
|
unsigned int sz = htonl(message.size());
|
|
|
|
for (size_t i = 0; i < clients.size(); ++i) {
|
2012-10-29 15:56:22 -06:00
|
|
|
clients[i]->Send(reinterpret_cast<const uint8_t *>(&sz), sizeof(sz));
|
|
|
|
clients[i]->Send((const uint8_t *) message.c_str(), message.size());
|
2012-10-29 15:18:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A decorator (in the design pattern sense) of the DF renderer class.
|
|
|
|
// Sends the screen contents to a client_pool.
|
2012-10-29 15:56:22 -06:00
|
|
|
class renderer_decorator : public df::renderer {
|
2012-10-29 15:18:50 -06:00
|
|
|
// the renderer we're decorating
|
2012-10-29 15:56:22 -06:00
|
|
|
df::renderer * inner;
|
2012-10-29 15:18:50 -06:00
|
|
|
|
|
|
|
// how many frames have passed since we last sent a frame
|
|
|
|
int framesNotPrinted;
|
|
|
|
|
|
|
|
// set to false in the destructor
|
|
|
|
bool * alive;
|
|
|
|
|
|
|
|
// clients to which we send the frame
|
|
|
|
client_pool clients;
|
|
|
|
|
|
|
|
// The following three methods facilitate copying of state to the inner object
|
2012-10-30 00:38:32 -06:00
|
|
|
void set_to_null() {
|
|
|
|
screen = NULL;
|
|
|
|
screentexpos = NULL;
|
|
|
|
screentexpos_addcolor = NULL;
|
|
|
|
screentexpos_grayscale = NULL;
|
|
|
|
screentexpos_cf = NULL;
|
|
|
|
screentexpos_cbr = NULL;
|
|
|
|
screen_old = NULL;
|
|
|
|
screentexpos_old = NULL;
|
|
|
|
screentexpos_addcolor_old = NULL;
|
|
|
|
screentexpos_grayscale_old = NULL;
|
|
|
|
screentexpos_cf_old = NULL;
|
|
|
|
screentexpos_cbr_old = NULL;
|
2012-10-29 15:18:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void copy_from_inner() {
|
|
|
|
screen = inner->screen;
|
|
|
|
screentexpos = inner->screentexpos;
|
|
|
|
screentexpos_addcolor = inner->screentexpos_addcolor;
|
|
|
|
screentexpos_grayscale = inner->screentexpos_grayscale;
|
|
|
|
screentexpos_cf = inner->screentexpos_cf;
|
|
|
|
screentexpos_cbr = inner->screentexpos_cbr;
|
|
|
|
screen_old = inner->screen_old;
|
|
|
|
screentexpos_old = inner->screentexpos_old;
|
|
|
|
screentexpos_addcolor_old = inner->screentexpos_addcolor_old;
|
|
|
|
screentexpos_grayscale_old = inner->screentexpos_grayscale_old;
|
|
|
|
screentexpos_cf_old = inner->screentexpos_cf_old;
|
|
|
|
screentexpos_cbr_old = inner->screentexpos_cbr_old;
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_to_inner() {
|
|
|
|
inner->screen = screen;
|
|
|
|
inner->screentexpos = screentexpos;
|
|
|
|
inner->screentexpos_addcolor = screentexpos_addcolor;
|
|
|
|
inner->screentexpos_grayscale = screentexpos_grayscale;
|
|
|
|
inner->screentexpos_cf = screentexpos_cf;
|
|
|
|
inner->screentexpos_cbr = screentexpos_cbr;
|
|
|
|
inner->screen_old = screen_old;
|
|
|
|
inner->screentexpos_old = screentexpos_old;
|
|
|
|
inner->screentexpos_addcolor_old = screentexpos_addcolor_old;
|
|
|
|
inner->screentexpos_grayscale_old = screentexpos_grayscale_old;
|
|
|
|
inner->screentexpos_cf_old = screentexpos_cf_old;
|
|
|
|
inner->screentexpos_cbr_old = screentexpos_cbr_old;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2012-10-29 15:56:22 -06:00
|
|
|
renderer_decorator(df::renderer * inner, bool * alive)
|
2012-10-29 15:18:50 -06:00
|
|
|
: inner(inner)
|
|
|
|
, framesNotPrinted(0)
|
|
|
|
, alive(alive)
|
|
|
|
{
|
|
|
|
copy_from_inner();
|
|
|
|
}
|
|
|
|
virtual void update_tile(int x, int y) {
|
|
|
|
copy_to_inner();
|
|
|
|
inner->update_tile(x, y);
|
|
|
|
}
|
|
|
|
virtual void update_all() {
|
|
|
|
copy_to_inner();
|
|
|
|
inner->update_all();
|
|
|
|
}
|
|
|
|
virtual void render() {
|
|
|
|
copy_to_inner();
|
|
|
|
inner->render();
|
|
|
|
|
|
|
|
++framesNotPrinted;
|
|
|
|
int gfps = enabler->calculated_gfps;
|
|
|
|
if (gfps == 0) gfps = 1;
|
|
|
|
// send a frame roughly every 128 mibiseconds (1 second = 1024 mibiseconds)
|
|
|
|
if ((framesNotPrinted * 1024) / gfps <= 128) return;
|
|
|
|
|
|
|
|
client_pool::lock lock(clients);
|
|
|
|
if (!clients.has_clients()) return;
|
|
|
|
framesNotPrinted = 0;
|
|
|
|
std::stringstream frame;
|
|
|
|
frame << gps->dimx << ' ' << gps->dimy << " 0 0 " << gps->dimx << ' ' << gps->dimy << '\n';
|
|
|
|
unsigned char * sc_ = gps->screen;
|
|
|
|
for (int y = 0; y < gps->dimy; ++y) {
|
|
|
|
unsigned char * sc = sc_;
|
|
|
|
for (int x = 0; x < gps->dimx; ++x) {
|
|
|
|
unsigned char ch = sc[0];
|
|
|
|
unsigned char bold = (sc[3] != 0) * 8;
|
|
|
|
unsigned char translate[] =
|
|
|
|
{ 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15 };
|
|
|
|
unsigned char fg = translate[(sc[1] + bold) % 16];
|
|
|
|
unsigned char bg = translate[sc[2] % 16]*16;
|
|
|
|
frame.put(ch);
|
|
|
|
frame.put(fg+bg);
|
|
|
|
sc += 4*gps->dimy;
|
|
|
|
}
|
|
|
|
sc_ += 4;
|
|
|
|
}
|
|
|
|
clients.broadcast(frame.str());
|
|
|
|
}
|
|
|
|
virtual void set_fullscreen() { inner->set_fullscreen(); }
|
2012-10-29 15:56:22 -06:00
|
|
|
virtual void zoom(df::zoom_commands cmd) {
|
2012-10-29 15:18:50 -06:00
|
|
|
copy_to_inner();
|
|
|
|
inner->zoom(cmd);
|
|
|
|
}
|
|
|
|
virtual void resize(int w, int h) {
|
|
|
|
copy_to_inner();
|
|
|
|
inner->resize(w, h);
|
|
|
|
copy_from_inner();
|
|
|
|
}
|
|
|
|
virtual void grid_resize(int w, int h) {
|
|
|
|
copy_to_inner();
|
|
|
|
inner->grid_resize(w, h);
|
|
|
|
copy_from_inner();
|
|
|
|
}
|
|
|
|
virtual ~renderer_decorator() {
|
|
|
|
*alive = false;
|
2012-10-30 00:38:32 -06:00
|
|
|
if (inner) {
|
|
|
|
copy_to_inner();
|
|
|
|
delete inner;
|
|
|
|
inner = 0;
|
|
|
|
}
|
|
|
|
set_to_null();
|
2012-10-29 15:18:50 -06:00
|
|
|
}
|
2012-10-29 15:56:22 -06:00
|
|
|
virtual bool get_mouse_coords(int *x, int *y) { return inner->get_mouse_coords(x, y); }
|
2012-10-29 15:18:50 -06:00
|
|
|
virtual bool uses_opengl() { return inner->uses_opengl(); }
|
|
|
|
|
2012-10-29 15:56:22 -06:00
|
|
|
static renderer_decorator * hook(df::renderer *& ptr, bool * alive) {
|
2012-10-29 15:18:50 -06:00
|
|
|
renderer_decorator * r = new renderer_decorator(ptr, alive);
|
|
|
|
ptr = r;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-10-29 15:56:22 -06:00
|
|
|
static void unhook(df::renderer *& ptr, renderer_decorator * dec, color_ostream & out) {
|
2012-10-29 15:18:50 -06:00
|
|
|
dec->copy_to_inner();
|
|
|
|
ptr = dec->inner;
|
|
|
|
dec->inner = 0;
|
|
|
|
delete dec;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DFHACK_PLUGIN("dfstream");
|
|
|
|
|
2012-10-29 15:56:22 -06:00
|
|
|
inline df::renderer *& active_renderer() {
|
|
|
|
return enabler->renderer;
|
2012-10-29 15:18:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// This class is a smart pointer around a renderer_decorator.
|
|
|
|
// It should only be assigned r_d pointers that use the alive-pointer of this
|
|
|
|
// instance.
|
|
|
|
// If the r_d has been deleted by an external force, this smart pointer doesn't
|
|
|
|
// redelete it.
|
|
|
|
class auto_renderer_decorator {
|
|
|
|
renderer_decorator * p;
|
|
|
|
public:
|
|
|
|
// pass this member to the ctor of renderer_decorator
|
|
|
|
bool alive;
|
|
|
|
|
|
|
|
auto_renderer_decorator()
|
|
|
|
: p(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~auto_renderer_decorator() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
if (*this) {
|
|
|
|
delete p;
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() {
|
|
|
|
return (p != 0) && alive;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_renderer_decorator & operator=(renderer_decorator *p) {
|
|
|
|
reset();
|
|
|
|
this->p = p;
|
2013-01-02 09:26:30 -07:00
|
|
|
return *this;
|
2012-10-29 15:18:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
renderer_decorator * get() {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer_decorator * operator->() {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto_renderer_decorator decorator;
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
|
|
|
|
{
|
2012-10-30 10:48:28 -06:00
|
|
|
if (!df::renderer::_identity.can_instantiate())
|
2012-10-30 00:38:32 -06:00
|
|
|
{
|
|
|
|
out.printerr("Cannot allocate a renderer\n");
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-10-29 15:18:50 -06:00
|
|
|
if (!decorator) {
|
|
|
|
decorator = renderer_decorator::hook(active_renderer(), &decorator.alive);
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
{
|
2012-10-30 00:38:32 -06:00
|
|
|
if (decorator && active_renderer() == decorator.get())
|
|
|
|
{
|
|
|
|
renderer_decorator::unhook(active_renderer(), decorator.get(), out);
|
|
|
|
}
|
2012-10-29 15:18:50 -06:00
|
|
|
decorator.reset();
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
// vim:set sw=4 sts=4 et:
|