This commit is contained in:
Daniel Flanagan 2022-11-23 13:19:44 -06:00
parent c2b37f5645
commit 0ed40d35c2
Signed by untrusted user: lytedev-divvy
GPG key ID: 6D69CEEE4ABBCD82

47
6.ts
View file

@ -2,6 +2,30 @@ import { BufReader } from "https://deno.land/std@0.157.0/io/buffer.ts";
const PORT = 5588; const PORT = 5588;
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;
}
// const tEnc = new TextEncoder(); // const tEnc = new TextEncoder();
console.log(`Starting TCP listener on on 0.0.0.0:${PORT}`); console.log(`Starting TCP listener on on 0.0.0.0:${PORT}`);
@ -19,11 +43,32 @@ function chatSession(conn: Deno.Conn) {
serve(conn).catch((e) => e).finally(closeOnDisconnect); serve(conn).catch((e) => e).finally(closeOnDisconnect);
} }
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;
async function serve(conn: Deno.Conn) { async function serve(conn: Deno.Conn) {
const reader = new BufReader(conn); const reader = new BufReader(conn);
const type = await reader.peek(1); const type = await reader.peek(1);
if (!type) throw new Error("failed to peek start of packet"); if (!type) throw new Error("failed to peek start of packet");
console.log(type); console.log({ type }, 0x64);
// TODO: loop...
} }
for await (const conn of Deno.listen({ port: PORT })) { for await (const conn of Deno.listen({ port: PORT })) {