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

target := xremoteview

+ 42
- 10
main.c View File

#include <errno.h> #include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h>


static void *imgbuf = NULL; static void *imgbuf = NULL;
static size_t imgbuflen = 0; static size_t imgbuflen = 0;


static void upload_jpeg(XImage *img, int fd) { static void upload_jpeg(XImage *img, int fd) {
write_jpeg(img); write_jpeg(img);
uint32_t len = (uint32_t)imgbuflen;
uint32_t len = htonl((uint32_t)imgbuflen);
write(fd, &len, sizeof(len)); 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) { if (sockfd < 0) {
perror("socket"); perror("socket");
return 1; 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"); perror("connect");
return 1; return 1;
} }


freeaddrinfo(info);

Display *display = XOpenDisplay(NULL); Display *display = XOpenDisplay(NULL);
Window root = XDefaultRootWindow(display); Window root = XDefaultRootWindow(display);
XWindowAttributes gwa; XWindowAttributes gwa;
} }


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


XShmDetach(display, &shminfo); XShmDetach(display, &shminfo);

+ 36
- 9
server.js View File

let http = require("http"); let http = require("http");
let net = require("net"); 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 = ` let html = `
<!DOCTYPE html> <!DOCTYPE html>
<script> <script>
let img = document.getElementById("image"); let img = document.getElementById("image");
let url = img.src; 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); let rand = Math.floor(Math.random() * 1000000);
img.src = url + "?" + rand; img.src = url + "?" + rand;
}, 1000);
}

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

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


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


net.createServer(conn => { net.createServer(conn => {
console.log("Connection", conn.remoteAddress); console.log("Connection", conn.remoteAddress);


while (true) { while (true) {
if (!hasLength && received.length >= 4) { if (!hasLength && received.length >= 4) {
expectedLen = received.readInt32LE(0);
expectedLen = received.readUInt32BE(0);
received = received.slice(4);
hasLength = true; 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; hasLength = false;

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

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

Loading…
Cancel
Save