All pastes #110909 Raw Edit

Mine

public text v1 · immutable
·published 2006-08-02 00:39 UTC
rendered paste body
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>

#include "bzfsAPI.h"

BZ_GET_PLUGIN_VERSION

int sshSock;
std::string cmdLine;
std::string sendBuf;

int spawn (const char* commandLine) {
	int sv[2];
	socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
	
	// Yoohoo! (hanshake)
	sendBuf = "HELLO-SERVER\n";
	
	if (!fork()) {
		dup2(sv[1], 0);
		dup2(sv[1], 1);
		execl("/bin/sh", "sh", "-c", commandLine, NULL);
	}
	close(sv[1]);
	return(sv[0]);
}

class BZFX : public bz_EventHandler
{
public:
	virtual void process (bz_EventData *eventData)
	{
		switch (eventData->eventType) {
			case bz_eTickEvent: {
				sendBuf += "SERVER-UPDATE\n";
				
				int n = send(sshSock, sendBuf.c_str(), sendBuf.length(), 0);
				if (n >= 0) sendBuf = sendBuf.substr(n);
				else if (errno == EPIPE) {
					close(sshSock);
					sshSock = spawn(cmdLine.c_str());
				}
			}
			break;
		}
	}
};

BZFX bzfx;

BZF_PLUGIN_CALL int bz_Load (const char* commandLine) {
	cmdLine = commandLine;
	
	sshSock = spawn(cmdLine.c_str());
	
	bz_registerEvent(bz_eTickEvent, &bzfx);
	
	bz_debugMessage(4,"BZFX plugin loaded");
	return 0;
}

BZF_PLUGIN_CALL int bz_Unload () {
	bz_removeEvent(bz_eTickEvent, &bzfx);
	
	sendBuf += "GOODBYE-SERVER\n";
	const char* data = sendBuf.c_str();
	int left = sendBuf.length();
	int total = 0;
	while (left > 0) {
		int n = send(sshSock, data + total, left, 0);

		if (n == -1)
			break;
		
		total += n;
		left -= n;
	}
	close(sshSock);
	
	bz_debugMessage(4,"BZFX plugin unloaded");
	return 0;
}