Skip to content

Commit

Permalink
Converting to new communication protocol part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal Web committed Jun 29, 2023
1 parent bc7a08a commit 35ff2b7
Show file tree
Hide file tree
Showing 22 changed files with 84 additions and 71 deletions.
7 changes: 3 additions & 4 deletions packetDesign.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
}
]

#### PACKET HEADERS (MSGPack Object)(OPTIONAL ENCRYPTION)
#### UDSP HEADERS (MSGPack Object)(OPTIONAL ENCRYPTION)

- Nonce
- Identity Certificate (OPTIONAL)(REQUIRED AT START)
- Socket ID (CLIENT/SERVER)
- May indicate which server to send to for load balancing
- May indicate which domain to send to for virtual hosts
Expand Down Expand Up @@ -52,8 +51,8 @@
| :------------: |
| UDP |
| UDSP HEADERS |
| UDSP BODY |
| UDSP FOOTER |
| UDSP MESSAGE |
| UDSP FOOTER |

The UDSP Header, Body, & Footer are all part of one array structure encoded with msgpack.
##### IPv6 HEADERS
Expand Down
Binary file modified profiles/default-Ephemeral.cert
Binary file not shown.
Binary file modified profiles/default-EphemeralPublic.cert
Binary file not shown.
Binary file modified profiles/default-Master.cert
Binary file not shown.
Binary file modified profiles/default-MasterPublic.cert
Binary file not shown.
Binary file modified profiles/default-Profile.cert
Binary file not shown.
4 changes: 4 additions & 0 deletions requestHeaders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# UDSP Request Headers

- dataSize (HTTP Content-Length)
- serialization
6 changes: 3 additions & 3 deletions scripts/certificates.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const domainProfile = await createProfile({
// encryptConnectionId: 'sealedbox', Applies to both client and server connection ID
// encryptClientConnectionId: 'sealedbox',
// encryptServerConnectionId: 'sealedbox',
connectionIdKeypair: true,
// connectionIdKeypair: true,
// Encrypt public key sent in the packet
encryptClientKey: 'sealedbox',
encryptServerKey: 'sealedbox',
// encryptClientKey: 'sealedbox',
// encryptServerKey: 'sealedbox',
encryptKeypair: true
},
// Max connection id size in bytes
Expand Down
Binary file modified services/universal.web-Ephemeral.cert
Binary file not shown.
Binary file modified services/universal.web-EphemeralPublic.cert
Binary file not shown.
Binary file modified services/universal.web-Master.cert
Binary file not shown.
Binary file modified services/universal.web-MasterPublic.cert
Binary file not shown.
Binary file modified services/universal.web-Profile.cert
Binary file not shown.
6 changes: 3 additions & 3 deletions udsp/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class Ask extends Base {
} = source;
const {
data,
head,
method
head = {},
method = 'get'
} = requestObject;
console.log('Ask', requestObject);
const streamId = packetIdGenerator.get();
Expand All @@ -39,7 +39,7 @@ export class Ask extends Base {
if (method) {
this.request.method = method;
} else {
this.method = 'get';
this.request.method = 'get';
}
queue.set(streamId, this);
}
Expand Down
6 changes: 3 additions & 3 deletions udsp/client/connect.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { connected } from '#logs';
import { decode } from 'msgpackr';
export async function connect(message = {}) {
import { randomBuffer } from '#utilities/crypto';
export async function connect(message) {
console.log('-------CLIENT CONNECTING-------\n');
const thisClient = this;
// opn stands for open meaning connect to a server
message.intro = 'Hello Server!';
const connectRequest = thisClient.request({
message
message: randomBuffer()
}, 'connect');
console.log('Connect request', connectRequest);
const connectResponse = await connectRequest.send();
Expand Down
4 changes: 2 additions & 2 deletions udsp/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Reply extends Base {
isReply = true;
async assemble() {
const thisReply = this;
const { contentType } = thisReply;
const { serialization } = thisReply;
if (thisReply.totalIncomingPackets === 1) {
thisReply.request = thisReply.incomingPackets[0];
}
Expand All @@ -56,7 +56,7 @@ export class Reply extends Base {
Buffer.concat([packet.data, item.data]);
}
});
if (contentType === 'struct' || !contentType) {
if (serialization === 'struct' || !serialization) {
msgReceived(thisReply.request);
if (thisReply.request.data) {
thisReply.request.data = decode(thisReply.request.data);
Expand Down
8 changes: 4 additions & 4 deletions udsp/request/assembleData.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { hasLength, jsonParse } from '@universalweb/acid';
import { decode } from 'msgpackr';
import { destroy } from './destory.js';
export async function assembleData(data, contentType) {
export async function assembleData(data, serialization) {
if (hasLength(data)) {
let compiledData = Buffer.concat(data);
if (contentType) {
if (contentType === 'obj' || contentType === 1) {
if (serialization) {
if (serialization === 'obj' || serialization === 1) {
try {
compiledData = decode(data);
} catch (err) {
return this.destroy('Failed to decode incoming data as message pack');
}
} else if (contentType === 'json' || contentType === 2) {
} else if (serialization === 'json' || serialization === 2) {
try {
compiledData = jsonParse(data);
} catch (err) {
Expand Down
71 changes: 49 additions & 22 deletions udsp/request/base.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { sendPacket } from './sendPacket.js';
import { destroy } from './destory.js';
import { bufferPacketization } from './bufferPacketization.js';
import { dataPacketization } from './dataPacketization.js';
import { sendEnd } from './sendEnd.js';
import { on } from './on.js';
import { flushOutgoing, flushIncoming, flush } from './flush.js';
import { sendPacketsById } from './sendPacketsById.js';
import { sendAll } from './sendAll.js';
import { onPacket } from './onPacket.js';
import {
isBuffer, isPlainObject, isString, promise, assign
isBuffer, isPlainObject, isString, promise, assign,
objectSize
} from '@universalweb/acid';
import { encode } from 'msgpackr';
import { request } from '#udsp/request';
import { assembleData } from './assembleData.js';
const singlePacketMethods = /^(connect|open)$/i;
export class Base {
constructor(options = {}, source) {
const { events, } = options;
Expand Down Expand Up @@ -53,42 +55,74 @@ export class Base {
return data;
}
if (isPlainObject(data)) {
this.contentType = 1;
this.serialization = 1;
return encode(data);
}
return Buffer.from(data);
}
async assemble() {
const { contentType } = this;
const { serialization } = this;
if (this.data) {
this.data = await assembleData(this.data, this.response, contentType);
this.data = await assembleData(this.data, this.response, serialization);
console.log('Assembled', this.data);
}
this.destroy();
await this.accept(this);
}
async send() {
const thisSource = this;
buildSetupPacket() {
const {
packetTemplate,
maxPacketSize,
sid,
isAsk,
isReply
} = this;
const message = (isAsk) ? this.request : this.response;
const method = message.method;
const packet = assign({
method
}, packetTemplate);
if (objectSize(message.head)) {
packet.headerSize = message.head.length;
}
this.outgoingPackets[0] = packet;
}
async packetization() {
const {
packetTemplate,
contentType,
maxPacketSize,
sid,
isAsk,
isReply
} = this;
const message = (isAsk) ? this.request : this.response;
console.log(`${this.type}.send`, message);
if (message.data) {
if (!isBuffer(message.data)) {
this.writeHeader('serialization', 'struct');
message.data = this.dataToBuffer(message.data);
}
this.totalReplyDataSize = request.data?.length;
this.dataSize = request.data?.length;
}
if (this.contentType) {
message.head.contentType = this.contentType;
await dataPacketization(this);
}
async send() {
const thisSource = this;
const {
packetTemplate,
maxPacketSize,
sid,
isAsk,
isReply
} = this;
const message = (isAsk) ? this.request : this.response;
console.log(`${this.type}.send`, message);
if (singlePacketMethods.test(message.method)) {
message.end = true;
message.pid = 0;
this.outgoingPackets[0] = message;
} else {
this.packetization();
}
// await bufferPacketization(this);
const awaitingResult = promise((accept) => {
thisSource.accept = accept;
});
Expand All @@ -102,20 +136,13 @@ export class Base {
sendAll = sendAll;
onPacket = onPacket;
sendPacket = sendPacket;
bufferPacketization = bufferPacketization;
on = on;
currentPayloadSize = 0;
totalReceivedUniquePackets = 0;
totalIncomingUniquePackets = 0;
progress = 0;
request = {
head: {},
body: {}
};
response = {
head: {},
body: {}
};
request = {};
response = {};
// this is the data in order may have missing packets at times but will remain in order
data = [];
// This is as the data came in over the wire out of order
Expand Down
6 changes: 3 additions & 3 deletions udsp/request/buildMessage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export function buildMessage({
method,
contentType,
serialization,
dataSize,
packet
}) {
if (contentType) {
packet.head.contentType = contentType;
if (serialization) {
packet.head.serialization = serialization;
}
if (dataSize) {
packet.head.dataSize = dataSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { assign } from '@universalweb/acid';
import { buildMessage } from './buildMessage.js';
import { request } from '#udsp/request';
export async function bufferPacketization(source) {
export async function dataPacketization(source) {
const {
maxPacketSize,
contentType,
method,
id: sid,
isAsk,
outgoingPackets
} = source;
const message = (isAsk) ? this.request : this.response;
const message = (isAsk) ? source.request : source.response;
const data = message.data;
const dataSize = data?.length;
let currentBytePosition = 0;
let packetId = 0;
let packetId = outgoingPackets.length;
if (dataSize > maxPacketSize) {
console.log('Body size', data.length);
console.log('data size', data.length);
while (currentBytePosition < dataSize) {
const endIndex = currentBytePosition + maxPacketSize;
const safeEndIndex = endIndex > dataSize ? dataSize : endIndex;
Expand All @@ -25,17 +23,8 @@ export async function bufferPacketization(source) {
const packet = {
pid: packetId,
endIndex: safeEndIndex,
sid,
head: {}
sid
};
if (packetId === 0) {
buildMessage({
method,
contentType,
dataSize,
packet
});
}
packet.data = chunk;
outgoingPackets[packetId] = outgoingPackets;
if (endIndex >= dataSize) {
Expand All @@ -51,12 +40,6 @@ export async function bufferPacketization(source) {
end: true,
data
};
buildMessage({
method,
contentType,
dataSize,
packet
});
console.log(source);
outgoingPackets[0] = packet;
}
Expand Down
8 changes: 4 additions & 4 deletions udsp/request/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export async function onPacket(packet) {
pt: totalIncomingUniquePackets,
// Dat payload size
tps: totalIncomingPayloadSize,
// contentType
ct: contentType,
// serialization
ct: serialization,
// Complete
done,
// Finale Packet
Expand All @@ -37,8 +37,8 @@ export async function onPacket(packet) {
if (hasValue(totalIncomingPayloadSize)) {
this.totalIncomingPayloadSize = totalIncomingPayloadSize;
}
if (contentType) {
this.contentType = contentType;
if (serialization) {
this.serialization = serialization;
}
this.totalIncomingPackets++;
if (hasValue(packetId)) {
Expand Down
2 changes: 1 addition & 1 deletion udsp/request/sendAll.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eachArray } from '@universalweb/acid';
export async function sendAll() {
const thisReply = this;
console.log('outgoingPackets', thisReply.outgoingPackets);
console.log('outgoingPackets', thisReply.outgoingPackets[0]);
eachArray(thisReply.outgoingPackets, (packet) => {
thisReply.sendPacket({
message: packet
Expand Down

0 comments on commit 35ff2b7

Please sign in to comment.