Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] V0.1 zip filestore feature branch #1

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/server-config.example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"updateServerHost": "0.0.0.0",
"updateServerPort": 43592,
"cacheDir": "./cache"
"storeDir": "../store",
"clientVersion": 435
}
7,611 changes: 5,300 additions & 2,311 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@runejs/update-server",
"version": "1.2.2",
"version": "2.0.0-alpha.0",
"description": "RuneJS Game Update Server",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "rimraf lib && tsc",
"start": "ts-node-dev --respawn src/main.ts",
"start": "ts-node-dev --max-old-space-size=1024 --respawn src/main.ts",
"lint": "eslint --ext .ts src",
"lint:fix": "eslint --ext .ts src --fix",
"package": "rimraf lib && npm i && npm run build && npm publish --dry-run"
Expand All @@ -22,8 +22,8 @@
"author": "Tynarus",
"license": "GPL-3.0",
"dependencies": {
"@runejs/core": "^1.5.4",
"@runejs/filestore": "^0.15.2",
"@runejs/common": "2.0.0-rc.1",
"@runejs/filestore": "file:../filestore",
"crc-32": "^1.2.0",
"source-map-support": "^0.5.19",
"tslib": "^2.2.0"
Expand Down
14 changes: 14 additions & 0 deletions src/config/update-server-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ServerConfigOptions } from '@runejs/common/net';


export interface UpdateServerConfig extends ServerConfigOptions {
updateServerHost: string;
updateServerPort: number;
storeDir: string;
clientVersion: number;
}

export const defaultConfig: Partial<UpdateServerConfig> = {
storeDir: '../store',
clientVersion: 435
};
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { launchUpdateServer } from './update-server';
export * from './update-server';
export * from './net/update-server-connection';
export { UpdateServerConfig } from './config/update-server-config';
8 changes: 6 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { launchUpdateServer } from './update-server';
import { logger } from '@runejs/common';
import UpdateServer from './update-server';
import 'source-map-support/register';

launchUpdateServer();

UpdateServer.launch()
.then(() => logger.info(`Ready to accept connections.`))
.catch(error => logger.error(`Error launching Update Server.`, error));
4 changes: 4 additions & 0 deletions src/net/file-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface FileRequest {
archiveIndex: number;
fileIndex: number;
}
108 changes: 108 additions & 0 deletions src/net/update-server-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Socket } from 'net';
import { logger } from '@runejs/common';
import { SocketServer } from '@runejs/common/net';
import { ByteBuffer } from '@runejs/common/buffer';
import UpdateServer from '../update-server';
import { FileRequest } from './file-request';


export const CONNECTION_ACCEPTED = 0;
export const UNSUPPORTED_CLIENT_VERSION = 6;


export class UpdateServerConnection extends SocketServer {

private readonly updateServer: UpdateServer;
private fileRequests: FileRequest[] = [];

public constructor(updateServer: UpdateServer,
gameServerSocket: Socket) {
super(gameServerSocket);
this.updateServer = updateServer;
}

public initialHandshake(buffer: ByteBuffer): boolean {
const clientVersion: number = buffer.get('int');
const supportedVersion: number = this.updateServer.serverConfig.clientVersion;

const responseCode: number = clientVersion === supportedVersion ? CONNECTION_ACCEPTED : UNSUPPORTED_CLIENT_VERSION;
const success: boolean = responseCode === CONNECTION_ACCEPTED;

// send the handshake response to the client
this.socket.write(Buffer.from([ responseCode ]));

return success;
}

public decodeMessage(buffer: ByteBuffer): void {
while(buffer.readable >= 4) {
const requestMethod = buffer.get('byte', 'u'); // 0, 1, 2, 3, or 4
const indexId = buffer.get('byte', 'u');
const fileId = buffer.get('short', 'u');

if(requestMethod >= 4) {
// error
return;
}

if(requestMethod >= 2) {
// clear queue
this.fileRequests = [];
break;
}

const fileRequest: FileRequest = { archiveIndex: indexId, fileIndex: fileId };

if(requestMethod === 1) {
try {
this.sendFile(fileRequest);
} catch(error) {
logger.error(error);
}
} else if(requestMethod === 0) {
this.fileRequests.push(fileRequest);
}
}

this.sendQueuedFiles();
}

public connectionDestroyed(): void {
this.fileRequests = [];
}

protected sendQueuedFiles(): void {
if(!this.fileRequests.length) {
return;
}

const fileRequests = [ ...this.fileRequests ];
this.fileRequests = [];

for(const fileRequest of fileRequests) {
try {
this.sendFile(fileRequest);
} catch(error) {
logger.error(error);
}
}
}

protected sendFile(fileRequest: FileRequest): void {
if(!this.connectionAlive) {
return;
}

const requestedFile = this.updateServer.handleFileRequest(fileRequest);

if(requestedFile) {
this.socket.write(requestedFile);
} else {
throw new Error(`File ${fileRequest.fileIndex} in index ${fileRequest.archiveIndex} is missing.`);
// ^^^ this should have already been logged up the chain, no need to do it again here
// ^^^ just leaving for reference while testing
// this.socket.write(this.generateEmptyFile(indexId, fileId));
}
}

}
Loading