2022-11-04 16:11:30 -05:00
|
|
|
import { BufReader } from "https://deno.land/std@0.157.0/io/buffer.ts";
|
|
|
|
|
|
|
|
const PORT = 5588;
|
|
|
|
|
2022-11-23 13:19:44 -06:00
|
|
|
type Road = number;
|
|
|
|
type Dispatcher = Road[];
|
|
|
|
type Location = number;
|
|
|
|
type SpeedLimit = number;
|
|
|
|
type Timestamp = number;
|
|
|
|
type Plate = string;
|
|
|
|
type Car = Plate;
|
|
|
|
|
|
|
|
type U8 = number;
|
|
|
|
type U16 = number;
|
|
|
|
type U32 = number;
|
|
|
|
|
|
|
|
interface Camera {
|
|
|
|
road: Road;
|
|
|
|
location: Location;
|
|
|
|
speedLimit: SpeedLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Report {
|
|
|
|
camera: Camera;
|
|
|
|
car: Car;
|
|
|
|
timestamp: Timestamp;
|
|
|
|
}
|
|
|
|
|
2022-11-04 16:11:30 -05:00
|
|
|
// const tEnc = new TextEncoder();
|
|
|
|
|
|
|
|
console.log(`Starting TCP listener on on 0.0.0.0:${PORT}`);
|
|
|
|
|
|
|
|
function chatSession(conn: Deno.Conn) {
|
|
|
|
const closeOnDisconnect = () => {
|
|
|
|
console.warn("Disconnected client");
|
|
|
|
try {
|
|
|
|
conn.close();
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error closing client connection:", err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
serve(conn).catch((e) => e).finally(closeOnDisconnect);
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:19:44 -06:00
|
|
|
enum MessageType {
|
|
|
|
ERROR = 0x10, // Server -> Client
|
|
|
|
PLATE = 0x20, // Client -> Server
|
|
|
|
TICKET = 0x21, // Server -> Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorMessage = [
|
|
|
|
string, // msg
|
|
|
|
];
|
|
|
|
|
|
|
|
type PlateMessage = [
|
|
|
|
string, // plate
|
|
|
|
U32, // timestamp
|
|
|
|
];
|
|
|
|
|
|
|
|
interface TicketMessage {
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message = ErrorMessage | PlateMessage;
|
|
|
|
|
2022-11-04 16:11:30 -05:00
|
|
|
async function serve(conn: Deno.Conn) {
|
|
|
|
const reader = new BufReader(conn);
|
2022-11-19 20:43:14 -06:00
|
|
|
const type = await reader.peek(1);
|
|
|
|
if (!type) throw new Error("failed to peek start of packet");
|
2022-11-23 13:19:44 -06:00
|
|
|
console.log({ type }, 0x64);
|
|
|
|
// TODO: loop...
|
2022-11-04 16:11:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for await (const conn of Deno.listen({ port: PORT })) {
|
|
|
|
console.log("Connection established:", conn.remoteAddr);
|
|
|
|
chatSession(conn);
|
|
|
|
console.log("Connection closed:", conn.remoteAddr);
|
|
|
|
}
|