mario has logged in

v
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-09-15 15:55:02 -05:00
parent 6026593287
commit 13d144d26f
8 changed files with 115 additions and 30 deletions

View file

@ -1,3 +1,23 @@
class SockContext {
private static didInit: boolean = false;
private static _masterSock: Connection;
public static get masterSock(): Connection {
return this._masterSock;
}
private static _slaveSock: Connection;
public static get slaveSock(): Connection {
return this._slaveSock;
}
public static init(): void {
if(this.didInit) return;
}
}
class Entrypoint { class Entrypoint {
private static initStatus = { private static initStatus = {
fileCache: false fileCache: false
@ -5,8 +25,8 @@ class Entrypoint {
private static initCheck(): void { private static initCheck(): void {
var done = true; var done = true;
for(var i in Entrypoint.initStatus) for(var i in this.initStatus)
done = done && Entrypoint.initStatus[i]; done = done && this.initStatus[i];
if(done) if(done)
Entrypoint.ready(); Entrypoint.ready();
@ -18,7 +38,7 @@ class Entrypoint {
FileCache.initCache( FileCache.initCache(
// SUCCESS // SUCCESS
() => { () => {
Entrypoint.initStatus.fileCache = true; this.initStatus.fileCache = true;
this.initCheck(); this.initCheck();
}, },

View file

@ -25,13 +25,13 @@ class FileCache {
}; };
request.onsuccess = (event: any) => { request.onsuccess = (event: any) => {
FileCache.dbHandle = request.result; this.dbHandle = request.result;
success(); success();
}; };
} }
public static getMeta(fileName: string, success: (meta: FileMeta)=>void, error: (error: string)=>void): void { public static getMeta(fileName: string, success: (meta: FileMeta)=>void, error: (error: string)=>void): void {
var query = FileCache.dbHandle.transaction("metadata"); var query = this.dbHandle.transaction("metadata");
var store = query.objectStore("metadata"); var store = query.objectStore("metadata");
var request = store.get(fileName); var request = store.get(fileName);
@ -45,13 +45,13 @@ class FileCache {
} }
public static setMeta(meta: FileMeta) { public static setMeta(meta: FileMeta) {
var query = FileCache.dbHandle.transaction("metadata", "readwrite"); var query = this.dbHandle.transaction("metadata", "readwrite");
var store = query.objectStore("metadata"); var store = query.objectStore("metadata");
store.put(meta); store.put(meta);
} }
public static getFile(fileName: string, success: (name: string, data: Uint8Array)=>void, error: (error: string)=>void): void { public static getFile(fileName: string, success: (name: string, data: Uint8Array)=>void, error: (error: string)=>void): void {
var query = FileCache.dbHandle.transaction("files"); var query = this.dbHandle.transaction("files");
var store = query.objectStore("files"); var store = query.objectStore("files");
var request = store.get(fileName); var request = store.get(fileName);
@ -65,13 +65,17 @@ class FileCache {
} }
public static setFile(fileName: string, data: Uint8Array) { public static setFile(fileName: string, data: Uint8Array) {
var query = FileCache.dbHandle.transaction("files", "readwrite"); var query = this.dbHandle.transaction("files", "readwrite");
var store = query.objectStore("files"); var store = query.objectStore("files");
store.put({name: fileName, data: data}); store.put({name: fileName, data: data});
} }
public static deleteFile(fileName: string) { public static deleteFile(fileName: string) {
var query = this.dbHandle.transaction("files", "readwrite");
var store = query.objectStore("files");
store.delete(fileName);
store = query.objectStore("metadata");
store.delete(fileName);
} }
} }

View file

@ -0,0 +1,20 @@
class MasterProtocol {
public static get packetHandlers(): PacketHandle[] {
return [
{ id: 1, event: this.keyExchange }
];
}
private static keyExchange(data: Packet, conn: Connection): void {
var response = Key.generateResponsePacket(data);
if(Key.succeeded) {
Cipher.init(Key.privateKey);
conn.send(response);
} else
CriticalStop.redirect("Could not establish an encrypted connection with the server.");
}
public static loginAttempt(username: string, password: string): void {
}
}

View file

@ -0,0 +1,11 @@
class SlaveProtocol {
public static get packetHandlers(): PacketHandle[] {
return [
{ id: 1, event: this.userLoginResponse }
];
}
private static userLoginResponse(data: Packet, conn: Connection): void {
console.log("mario has logged in");
}
}

View file

@ -1,23 +1,50 @@
type ConnEvent = (conn: Connection) => void;
type ConnErrorEvent = (event: any, conn: Connection) => void;
type PacketEvent = (data: Packet, conn: Connection) => void;
type PacketHandle = { id: number, event: PacketEvent };
class Connection { class Connection {
private sock: WebSocket = null; private sock: WebSocket = null;
private _isOpen: boolean = false; private _isOpen: boolean = false;
private onOpenFunc: () => void = null;
private onCloseFunc: () => void = null; private address: string;
private useCipher: boolean;
private handles: PacketEvent[] = [];
private onOpenFunc: ConnEvent = null;
private onCloseFunc: ConnEvent = null;
private onErrorFunc: ConnErrorEvent = null;
public get isOpen(): boolean { public get isOpen(): boolean {
return this._isOpen; return this._isOpen;
} }
public open(onOpen: () => void = null): void { public constructor(address: string, handles: PacketHandle[], useCipher: boolean = false,
onOpen: ConnEvent = null, onClose: ConnEvent = null, onError: ConnErrorEvent = null)
{
this.address = address;
this.useCipher = useCipher;
this.onOpenFunc = onOpen;
this.onCloseFunc = onClose;
this.onErrorFunc = onError;
handles.forEach(element => {
this.handles[element.id] = element.event;
});
}
public open(): void {
if(this._isOpen) if(this._isOpen)
return; return;
// FLAG replace hard coded url with one loaded from a config file // FLAG replace hard coded url with one loaded from a config file
this.sock = new WebSocket("ws://localhost:6770"); this.sock = new WebSocket(this.address);
this.sock.binaryType = "arraybuffer"; this.sock.binaryType = "arraybuffer";
this.onOpenFunc = onOpen;
this.sock.onopen = this.onOpen; this.sock.onopen = this.onOpen;
this.sock.onmessage = this.onMessage; this.sock.onmessage = this.onMessage;
this.sock.onerror = this.onError;
this.sock.onclose = this.onClose; this.sock.onclose = this.onClose;
} }
@ -29,14 +56,14 @@ class Connection {
this._isOpen = true; this._isOpen = true;
if(this.onOpenFunc) if(this.onOpenFunc)
this.onOpenFunc(); this.onOpenFunc(this);
} }
private onMessage(event: any): void { private onMessage(event: any): void {
var raw = new Uint8Array(event.data); var raw = new Uint8Array(event.data);
var msg: Packet; var msg: Packet;
try { try {
msg = !Cipher.ready ? Packet.fromBytes(raw) msg = !this.useCipher || !Cipher.ready ? Packet.fromBytes(raw)
: Packet.fromBytes(Cipher.parse(raw)); : Packet.fromBytes(Cipher.parse(raw));
} catch(e) { } catch(e) {
close(); close();
@ -44,14 +71,12 @@ class Connection {
} }
console.log(msg); console.log(msg);
if(msg.id < this.handles.length && this.handles[msg.id] !== undefined)
this.handles[msg.id](msg, this);
/*
switch(msg.id) { switch(msg.id) {
case kMasterId.KeyExchange: case kMasterId.KeyExchange:
var response = Key.generateResponsePacket(msg);
if(Key.succeeded) {
Cipher.init(Key.privateKey);
this.send(response);
} else
CriticalStop.redirect("Could not establish an encrypted connection with the server.");
break; break;
case kMasterId.LoginAttempt: case kMasterId.LoginAttempt:
@ -60,6 +85,12 @@ class Connection {
break; break;
} }
*/
}
private onError(event: any): void {
if(this.onErrorFunc)
this.onErrorFunc(event, this);
} }
private onClose(event: any): void { private onClose(event: any): void {
@ -67,14 +98,13 @@ class Connection {
Cipher.close(); Cipher.close();
if(this.onCloseFunc) if(this.onCloseFunc)
this.onCloseFunc(); this.onCloseFunc(this);
} }
public close(onClose: () => void = null): void { public close(): void {
if(!this._isOpen) if(!this._isOpen)
return; return;
this.onCloseFunc = onClose;
this.sock.close(); this.sock.close();
} }
} }

View file

@ -36,7 +36,7 @@ namespace SockScape {
// TODO figure out what this has to do with ICMP (in server too) // TODO figure out what this has to do with ICMP (in server too)
uint IOC_IN = 0x80000000, uint IOC_IN = 0x80000000,
IOC_VENDOR = 0x18000000, IOC_VENDOR = 0x18000000,
SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 0xC;
Sock.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] {0}, null); Sock.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] {0}, null);
Key = new Key(); Key = new Key();