THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block |
---|
// Header socket.writeByte(103); // Binary object type code socket.writeByte(1); // Binary object format version socket.writeShort(1 + 2); // Flags, user type + has schema socket.writeInt(1512523596); // Type id, Java-style hash code of "MyType" socket.writeInt(0); // Hash code, 0 for simplicity socket.writeInt(24 + 5 + 8); // Object length, header + int field (byte type + 4 bytes value) + schema (4 bytes id + 4 bytes offset) socket.writeInt(0); // Schema id, for simplicity socket.writeInt(24 + 5); // Schema offset, header size + one int field // Fields socket.writeByte(3); // Field type, int socket.writeInt(42); // Field value // Schema socket.writeInt(1515208398); // Field id, Java hash of "myfield" socket.writeInt(24); // Field position from object start, right after 24-byte header |
Refuncading Reading binary object and retrieving field value:
Code Block |
---|
// Header assert socket.readByte() == 103; assert socket.readByte() == 1; assert socket.readByte() == 1 + 2; var typeId = socket.ReadInt(); var hash = socket.readInt(); var len = socket.ReadInt(); var schemaId = socket.ReadInt(); var schemaOffset = socket.ReadInt(); // Read field data into array, everything between header and schema var fieldData = socket.readByteArray(schemaOffset - 24); // Read schema var fieldCount = (len - schemaOffset) / 8; var fieldIds = new int[fieldCount]; var fieldOffsets = new int[fieldCount]; for (int i = 0; i < fieldCount; i++) { fieldIds[i] = socket.readInt(); fieldOffsets[i] = socket.readInt(); } // Let's suppose that user says BinaryObject.GetField( wants a value of the field named "myfield") var fieldName = "myfield"; var fieldId = getJavaHash(fieldName); // 1515208398 var fieldIdx = fieldIds.indexOf(fieldId); // Can be missing, handle accordingly var fieldOffset = fieldOffsets[fieldsIdx]; // We got the field offset, now read binary object from fieldData var reader = new ArrayReader(fieldData); reader.seek(fieldOffset - 24); var fieldType = reader.readByte(); if (fieldType = 3) // int { var fieldVal = reader.readInt(); } else if (fieldVal = 103) { // Nested binary object ... } else ... |
...