Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* TMCMR Tiles Server for FastCGI
Copyright 2015-2020 Rivoreo
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
This is a FastCGI-based program that render and serve map tiles using
TOGoS's Minecraft Map Renderer (TMCMR). Nonexistent and out-of-date
tiles are rendered automatically by running TMCMR, based on requests.
The following macros must be defined in order to build this program:
REGION_PATH path to a Minecraft region directory.
REGION_FILE_SUFFIX you probably want to define this as "mca".
TILE_IMAGE_PATH path to a directory where tile images in PNG
format will be stored.
For example using gcc(1):
$ gcc -Wall -O1 tmcmr-tiles-server.c -D REGION_PATH=\"/export/home/minecraft/chemistry/world/region\" -D REGION_FILE_SUFFIX=\"mca\" -D TILE_IMAGE_PATH=\"/var/www/mc-maps/chemistry/tiles\" -o chemistry-tmcmr-tiles-server -l fcgi
TMCMR program must be wrapped into a single executable named 'tmcmr'
that available under PATH; you can wrap it using a simple shell
script that runs java(1) to start TMCMR from a Java archive.
Example script at '/usr/local/bin/tmcmr':
#!/bin/sh
exec java -jar /opt/TMCMR/TMCMR-2017.09.07b.jar "$@"
*/
#include "fcgiapp.h"
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
static int transfer(FCGX_Stream *ws, int rfd) {
char buffer[4096];
int s;
while(1) {
s = read(rfd, buffer, sizeof buffer);
if(s < 0) {
if(errno == EINTR) continue;
return -1;
}
if(!s) return 0;
FCGX_PutStr(buffer, s, ws);
}
}
int main() {
FCGX_Stream *in, *out, *err;
FCGX_ParamArray envp;
accept_loop:
while(FCGX_Accept(&in, &out, &err, &envp) >= 0) {
int head_only = 0;
const char *method = FCGX_GetParam("REQUEST_METHOD", envp);
if(!method) {
FCGX_PutS("Status: 400 Bad request\r\nContent-type: text/plain\r\n\r\nMissing REQUEST_METHOD\n", out);
FCGX_PutS("Missing REQUEST_METHOD\n", err);
continue;
}
if(strcmp(method, "HEAD") == 0) head_only = 1;
else if(strcmp(method, "GET")) {
FCGX_PutS("Status: 501 Not implemented\r\n\r\n", out);
continue;
}
const char *query = FCGX_GetParam("QUERY_STRING", envp);
if(!query || !*query) {
FCGX_PutS("Status: 400 Need argument\r\nContent-type: text/plain\r\n\r\nNeed query string: <x>,<z>\n", out);
continue;
}
char *end_p;
long int x = strtol(query, &end_p, 10);
if(*end_p != ',' || !*++end_p) {
FCGX_PutS("Status: 400 Malformed argument\r\nContent-type: text/plain\r\n\r\nNeed 2 numbers separated by ','\n", out);
continue;
}
long int z = strtol(end_p, &end_p, 10);
if(*end_p) {
FCGX_PutS("Status: 400 Malformed argument\r\nContent-type: text/plain\r\n\r\nInvalid query string\n", out);
continue;
}
struct stat st;
time_t region_file_mtime, tile_image_file_mtime;
int region_file_errno;
char region_file_path[sizeof REGION_PATH + 2 + 11 + 1 + 11 + 4 + 1];
snprintf(region_file_path, sizeof region_file_path, "%s/r.%ld.%ld.%s", REGION_PATH, x, z, REGION_FILE_SUFFIX);
if(stat(region_file_path, &st) < 0) {
region_file_mtime = -1;
region_file_errno = errno;
} else {
region_file_mtime = st.st_mtime;
region_file_errno = 0;
}
char tile_image_file_path[sizeof TILE_IMAGE_PATH + 5 + 11 + 1 + 11 + 4 + 1];
snprintf(tile_image_file_path, sizeof tile_image_file_path, "%s/tile.%ld.%ld.png", TILE_IMAGE_PATH, x, z);
if(stat(tile_image_file_path, &st) < 0) {
if(errno != ENOENT) {
FCGX_FPrintF(err, "stat: %s: %s\n", tile_image_file_path, strerror(errno));
continue;
}
tile_image_file_mtime = -1;
} else {
tile_image_file_mtime = st.st_mtime;
}
if(tile_image_file_mtime == (time_t)-1 && region_file_errno) {
if(region_file_errno == ENOENT) {
FCGX_PutS("Status: 404 region not found\r\nContent-type: text/plain\r\n\r\nRegion not found\n", out);
} else {
FCGX_FPrintF(err, "Region file: stat: %s: %s\n", region_file_path, strerror(region_file_errno));
}
continue;
}
if(tile_image_file_mtime == (time_t)-1 || (region_file_mtime != (time_t)-1 && region_file_mtime - tile_image_file_mtime > 60)) {
pid_t pid = fork();
if(pid == -1) {
FCGX_FPrintF(err, "fork: %s\n", strerror(errno));
continue;
}
if(pid) {
while(waitpid(pid, NULL, 0) < 0) {
if(errno == EINTR) continue;
FCGX_FPrintF(err, "waitpid: %d: %s\n", (int)pid, strerror(errno));
goto accept_loop;
}
} else {
execlp("tmcmr", "tmcmr", "-threads", "1", region_file_path, "-o", TILE_IMAGE_PATH, NULL);
perror("tmcmr");
_exit(127);
}
}
int fd = open(tile_image_file_path, O_RDONLY);
if(fd == -1) {
if(errno == EINTR) {
FCGX_PutS("Status: 404 region not found\r\n\r\n", out);
} else {
FCGX_FPrintF(err, "Tile image file: open: %s: %s\n", tile_image_file_path, strerror(errno));
}
continue;
}
if(fstat(fd, &st) < 0) {
FCGX_FPrintF(err, "Tile image file: %s fstat: %d: %s\n", tile_image_file_path, fd, strerror(errno));
} else if(st.st_size > 0) {
FCGX_FPrintF(out, "Content-Length: %u\r\n", (unsigned int)st.st_size);
}
FCGX_PutS("Content-Type: image/png\r\n\r\n", out);
if(!head_only && transfer(out, fd) < 0) {
FCGX_FPrintF(err, "error on transfering image: %s\n", strerror(errno));
}
close(fd);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.