Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
JSTcpHub.hxx
Go to the documentation of this file.
1
36#ifndef _UTILS_JSTCPHUB_HXX_
37#define _UTILS_JSTCPHUB_HXX_
38
39#ifdef __EMSCRIPTEN__
40
41#include <emscripten.h>
42#include <emscripten/val.h>
43
44#include "utils/Hub.hxx"
45#include "utils/JSHubPort.hxx"
46
47class JSTcpHub
48{
49public:
50 JSTcpHub(CanHubFlow *hflow, int port)
51 : canHub_(hflow)
52 {
53 EM_ASM_(
54 {
55 var net = require('net');
56 var server = net.createServer(function(c)
57 {
58 console.log('client connected');
59 c.setEncoding('utf-8');
60 c.setTimeout(0);
61 c.setKeepAlive(true);
62 var client_port =
63 new Module.JSHubPort($1, function(data)
64 {
65 c.write(data);
66 });
67 c.on('close', function()
68 {
69 console.log('client disconnected');
70 client_port.abandon();
71 });
72 c.on('error', function()
73 {
74 console.log('client error -- disconnected');
75 client_port.abandon();
76 });
77 c.on('data', function(data)
78 {
79 client_port.recv(data);
80 });
81 });
82 server.listen($0, function()
83 {
84 console.log('listening on port ' + $0);
85 });
86 },
87 port, (unsigned long)canHub_);
88 }
89
90private:
91 CanHubFlow *canHub_;
92};
93
94#endif // __EMSCRIPTEN__
95#endif // _UTILS_JSTCPHUB_HXX_