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 {
private static initStatus = {
fileCache: false
@ -5,8 +25,8 @@ class Entrypoint {
private static initCheck(): void {
var done = true;
for(var i in Entrypoint.initStatus)
done = done && Entrypoint.initStatus[i];
for(var i in this.initStatus)
done = done && this.initStatus[i];
if(done)
Entrypoint.ready();
@ -18,7 +38,7 @@ class Entrypoint {
FileCache.initCache(
// SUCCESS
() => {
Entrypoint.initStatus.fileCache = true;
this.initStatus.fileCache = true;
this.initCheck();
},

View file

@ -25,13 +25,13 @@ class FileCache {
};
request.onsuccess = (event: any) => {
FileCache.dbHandle = request.result;
this.dbHandle = request.result;
success();
};
}
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 request = store.get(fileName);
@ -45,13 +45,13 @@ class FileCache {
}
public static setMeta(meta: FileMeta) {
var query = FileCache.dbHandle.transaction("metadata", "readwrite");
var query = this.dbHandle.transaction("metadata", "readwrite");
var store = query.objectStore("metadata");
store.put(meta);
}
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 request = store.get(fileName);
@ -65,13 +65,17 @@ class FileCache {
}
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");
store.put({name: fileName, data: data});
}
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 {
private sock: WebSocket = null;
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 {
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)
return;
// 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.onOpenFunc = onOpen;
this.sock.onopen = this.onOpen;
this.sock.onmessage = this.onMessage;
this.sock.onerror = this.onError;
this.sock.onclose = this.onClose;
}
@ -29,14 +56,14 @@ class Connection {
this._isOpen = true;
if(this.onOpenFunc)
this.onOpenFunc();
this.onOpenFunc(this);
}
private onMessage(event: any): void {
var raw = new Uint8Array(event.data);
var msg: Packet;
try {
msg = !Cipher.ready ? Packet.fromBytes(raw)
msg = !this.useCipher || !Cipher.ready ? Packet.fromBytes(raw)
: Packet.fromBytes(Cipher.parse(raw));
} catch(e) {
close();
@ -44,14 +71,12 @@ class Connection {
}
console.log(msg);
if(msg.id < this.handles.length && this.handles[msg.id] !== undefined)
this.handles[msg.id](msg, this);
/*
switch(msg.id) {
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;
case kMasterId.LoginAttempt:
@ -60,6 +85,12 @@ class Connection {
break;
}
*/
}
private onError(event: any): void {
if(this.onErrorFunc)
this.onErrorFunc(event, this);
}
private onClose(event: any): void {
@ -67,14 +98,13 @@ class Connection {
Cipher.close();
if(this.onCloseFunc)
this.onCloseFunc();
this.onCloseFunc(this);
}
public close(onClose: () => void = null): void {
public close(): void {
if(!this._isOpen)
return;
this.onCloseFunc = onClose;
this.sock.close();
}
}

View file

@ -36,7 +36,7 @@ namespace SockScape {
// TODO figure out what this has to do with ICMP (in server too)
uint IOC_IN = 0x80000000,
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);
Key = new Key();