Browse Source

better

master
Martin Dørum 3 years ago
parent
commit
b13f1b519d
3 changed files with 79 additions and 19 deletions
  1. 1
    0
      build.bx
  2. 42
    10
      main.c
  3. 36
    9
      server.js

+ 1
- 0
build.bx View File

@@ -0,0 +1 @@
target := xremoteview

+ 42
- 10
main.c View File

@@ -17,6 +17,7 @@
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

static void *imgbuf = NULL;
static size_t imgbuflen = 0;
@@ -78,27 +79,58 @@ static void write_jpeg(XImage *img) {

static void upload_jpeg(XImage *img, int fd) {
write_jpeg(img);
uint32_t len = (uint32_t)imgbuflen;
uint32_t len = htonl((uint32_t)imgbuflen);
write(fd, &len, sizeof(len));
write(fd, imgbuf, len);
write(fd, imgbuf, imgbuflen);
}

int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int main(int argc, char **argv) {
if (argc <= 1) {
printf("Usage: %s <host> [port] [sleep time]\n", argv[0]);
return 1;
}

char *host = argv[1];
char *port;
int sleeptime;
if (argc >= 3) {
port = argv[2];
} else {
port = "8099";
}

if (argc >= 4) {
sleeptime = atoi(argv[3]);
} else {
sleeptime = 200;
}

printf("Connect %s:%s...\n", host, port);
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_protocol = 0,
.ai_flags = AI_ADDRCONFIG,
};
struct addrinfo *info;
if (getaddrinfo(host, port, &hints, &info) < 0) {
perror("getaddrinfo");
return 1;
}

int sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
if (sockfd < 0) {
perror("socket");
return 1;
}

struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
inet_aton("127.0.0.1", &addr.sin_addr);
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
if (connect(sockfd, info->ai_addr, info->ai_addrlen) < 0) {
perror("connect");
return 1;
}

freeaddrinfo(info);

Display *display = XOpenDisplay(NULL);
Window root = XDefaultRootWindow(display);
XWindowAttributes gwa;
@@ -125,7 +157,7 @@ int main() {
}

upload_jpeg(image, sockfd);
sleep(1);
usleep(sleeptime * 1000);
}

XShmDetach(display, &shminfo);

+ 36
- 9
server.js View File

@@ -1,8 +1,8 @@
let http = require("http");
let net = require("net");

let tcpport = process.env["TCPPORT"] ? parseInt(process.env["TCPPORT"]) : 8080;
let httpport = process.env["HTTPPORT"] ? parseInt(process.env["HTTPPORT"]) : 8081;
let tcpport = process.env["TCPPORT"] ? parseInt(process.env["TCPPORT"]) : 8099;
let httpport = process.env["HTTPPORT"] ? parseInt(process.env["HTTPPORT"]) : 8098;

let html = `
<!DOCTYPE html>
@@ -16,16 +16,33 @@ let html = `
<script>
let img = document.getElementById("image");
let url = img.src;
setInterval(() => {

function refresh() {
img.onload = wait;
img.onerror = () => {
console.error("Load error");
setTimeout(refresh, 4000);
};

let rand = Math.floor(Math.random() * 1000000);
img.src = url + "?" + rand;
}, 1000);
}

function wait() {
fetch("/wait").then(refresh).catch(err => {
console.error("Wait error:", err);
wait();
});
}

wait();
</script>
</body>
</html>
`;

let imgdata = Buffer.alloc(0);
let waiters = [];

net.createServer(conn => {
console.log("Connection", conn.remoteAddress);
@@ -38,30 +55,40 @@ net.createServer(conn => {

while (true) {
if (!hasLength && received.length >= 4) {
expectedLen = received.readInt32LE(0);
expectedLen = received.readUInt32BE(0);
received = received.slice(4);
hasLength = true;
}

if (hasLength && received.length >= expectedLen + 4) {
imgdata = received.slice(4, expectedLen + 4);
received = received.slice(expectedLen + 4);
if (hasLength && received.length >= expectedLen) {
imgdata = received.slice(0, expectedLen);
received = received.slice(expectedLen);
hasLength = false;

for (let waiter of waiters) {
waiter.end();
}

waiters = [];
} else {
break;
}
}
});
}).on("error", err => console.error(err)).listen(tcpport, "0.0.0.0");
console.log(`TCP listening on 0.0.0.0:${tcpport}`);

http.createServer((req, res) => {
if (req.url.split("?")[0] == "/image") {
res.writeHead(200, {
"Content-Type": "image/jpeg",
"Refresh": "1",
});
res.end(imgdata);
} else if (req.url.split("?")[0] == "/wait") {
waiters.push(res);
} else {
res.writeHead(200);
res.end(html);
}
}).listen(httpport, "0.0.0.0");
console.log(`HTTP listening on 0.0.0.0:${httpport}`);

Loading…
Cancel
Save