Commit b291d6df authored by Linshizhi's avatar Linshizhi

Add RBLKS, WBLKS field to channel.

parent 77d8fb3f
This diff is collapsed.
...@@ -44,19 +44,26 @@ export class Channel { ...@@ -44,19 +44,26 @@ export class Channel {
* 2.Read Position * 2.Read Position
* *
* Figure: * Figure:
* ---------------------------------------------------------------------------------------------------------------- * -------------------------------------------------------------------------------------------------------------------------------
* | ReadPointer (4 bytes) | WritePointer (4 Bytes) | Private (4 Bytes) | WBlocks (2 Bytes) | RBlocks (2 Bytes) | Data Area (N bytes) | * | ReadPointer (4 bytes) | WritePointer (4 Bytes) | RBlks (1 Byte) | WBlks (1 Bytes) | Private (2 Bytes) | Data Area (N bytes) |
* ---------------------------------------------------------------------------------------------------------------- * -------------------------------------------------------------------------------------------------------------------------------
* *
* where N >= 2 * where N >= 2
* *
* */ * */
#rFieldPosLen = 4; #rFieldPosLen = 4;
#wFieldPosLen = 4; #wFieldPosLen = 4;
#priFieldLen = 4; #rBlkFieldLen = 1;
#numOfMetaField = 3; #wBlkFieldLen = 1;
#priFieldLen = 2;
#fieldSize = 0; #fieldSize = 0;
#rPtrShift = 0;
#wPtrShift = this.#rPtrShift + this.#rFieldPosLen;
#rBlkShift = this.#wPtrShift + this.#wFieldPosLen;
#wBlkShift = this.#rBlkShift + this.#rBlkFieldLen;
#priShift = this.#wBlkShift + this.#wBlkFieldLen;
#metaSize = 0; #metaSize = 0;
#size = 0; #size = 0;
#totalSize = 0; #totalSize = 0;
...@@ -86,7 +93,8 @@ export class Channel { ...@@ -86,7 +93,8 @@ export class Channel {
this.#size = size+1; this.#size = size+1;
// Init shared memory // Init shared memory
this.#metaSize = this.#rFieldPosLen + this.#wFieldPosLen + this.#priFieldLen; this.#metaSize = this.#rFieldPosLen + this.#wFieldPosLen +
this.#rBlkFieldLen + this.#wBlkFieldLen + this.#priFieldLen;
this.#shMem = shMem == null ? new bufferType(this.#size + this.#metaSize) : shMem; this.#shMem = shMem == null ? new bufferType(this.#size + this.#metaSize) : shMem;
this.#view = new DataView(this.#shMem); this.#view = new DataView(this.#shMem);
...@@ -97,8 +105,8 @@ export class Channel { ...@@ -97,8 +105,8 @@ export class Channel {
// Init readPointer and writePointer to // Init readPointer and writePointer to
// the first bytes of data area. // the first bytes of data area.
this.#view.setUint32(0, this.#writePointerCache); this.#view.setUint32(this.#wPtrShift, this.#writePointerCache);
this.#view.setUint32(4, this.#readPointerCache); this.#view.setUint32(this.#rPtrShift, this.#readPointerCache);
this.#totalSize = this.#metaSize + this.#size; this.#totalSize = this.#metaSize + this.#size;
this.#endPos = this.#metaSize + this.#size; this.#endPos = this.#metaSize + this.#size;
...@@ -107,11 +115,11 @@ export class Channel { ...@@ -107,11 +115,11 @@ export class Channel {
} }
#getReadPointer() { #getReadPointer() {
return this.#view.getUint32(0); return this.#view.getUint32(this.#rPtrShift);
} }
#getWritePointer() { #getWritePointer() {
return this.#view.getUint32(4); return this.#view.getUint32(this.#wPtrShift);
} }
metaSize() { metaSize() {
...@@ -119,12 +127,12 @@ export class Channel { ...@@ -119,12 +127,12 @@ export class Channel {
} }
readPriv() { readPriv() {
return this.#view.getUint32(8); return this.#view.getUint16(this.#priShift);
} }
writePriv(privData) { writePriv(privData) {
try { try {
this.#view.setUint32(8, privData); this.#view.setUint16(this.#priShift, privData);
} catch (error) { } catch (error) {
if (error instanceof RangeError) if (error instanceof RangeError)
return false; return false;
...@@ -153,6 +161,22 @@ export class Channel { ...@@ -153,6 +161,22 @@ export class Channel {
this.#mode = CHANNEL_MODE.BYTE; this.#mode = CHANNEL_MODE.BYTE;
} }
getRBlks() {
return this.#view.getUint8(this.#rBlkShift);
}
setRBlks(num) {
this.#view.setUint8(this.#rBlkShift, num);
}
getWBlks() {
return this.#view.getUint8(this.#wBlkShift);
}
setWBlks(num) {
this.#view.setUint8(this.#wBlkShift, num);
}
isSetPriv(flag) { isSetPriv(flag) {
let flags = this.readPriv(); let flags = this.readPriv();
return flags & flag; return flags & flag;
...@@ -165,7 +189,7 @@ export class Channel { ...@@ -165,7 +189,7 @@ export class Channel {
unsetPriv(flag) { unsetPriv(flag) {
let old = this.readPriv(); let old = this.readPriv();
flag = flag ^ 0x11111111; flag = flag ^ 0x1111;
this.writePriv(flag & old); this.writePriv(flag & old);
} }
...@@ -277,7 +301,6 @@ export class Channel { ...@@ -277,7 +301,6 @@ export class Channel {
this.#writePointerCache = this.#getWritePointer(); this.#writePointerCache = this.#getWritePointer();
this.#readPointerCache = this.#getReadPointer(); this.#readPointerCache = this.#getReadPointer();
console.log(this.#readPointerCache, this.#writePointerCache);
if (!this.#isAbleToWrite(size)) { if (!this.#isAbleToWrite(size)) {
return []; return [];
...@@ -366,12 +389,12 @@ export class Channel { ...@@ -366,12 +389,12 @@ export class Channel {
#writePointerUpdate(pos) { #writePointerUpdate(pos) {
this.#writePointerCache = pos; this.#writePointerCache = pos;
this.#view.setUint32(4, pos); this.#view.setUint32(this.#wPtrShift, pos);
} }
#readPointerUpdate(pos) { #readPointerUpdate(pos) {
this.#readPointerCache = pos; this.#readPointerCache = pos;
this.#view.setUint32(0, pos); this.#view.setUint32(this.#rPtrShift, pos);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment