Example #1
0
        internal RelationInfo CreateByName(IInternalObjectDBTransaction tr, string name, Type interfaceType)
        {
            name = string.Intern(name);
            uint id;

            if (!_name2Id.TryGetValue(name, out id))
            {
                id             = _freeId++;
                _name2Id[name] = id;
                tr.KeyValueDBTransaction.SetKeyPrefixUnsafe(ObjectDB.RelationNamesPrefix);
                var nameWriter = new ByteBufferWriter();
                nameWriter.WriteString(name);
                var idWriter = new ByteBufferWriter();
                idWriter.WriteVUInt32(id);
                tr.KeyValueDBTransaction.CreateOrUpdateKeyValue(nameWriter.Data, idWriter.Data);
            }
            RelationInfo relation;

            if (_id2Relation.TryGetValue(id, out relation))
            {
                throw new BTDBException($"Relation with name '{name}' was already initialized");
            }
            var clientType = FindClientType(interfaceType);

            relation         = new RelationInfo(id, name, _relationInfoResolver, interfaceType, clientType, tr);
            _id2Relation[id] = relation;
            return(relation);
        }
        public override void WriteData(ByteBufferWriter bbw)
        {
            var count = 0;

            for (var y = 0; y < _grid.GetLength(1); y++)
            {
                for (var x = 0; x < _grid.GetLength(0); x++)
                {
                    if (_grid[x, y] is ItemStack stack && !stack.IsEmpty)
                    {
                        count++;
                    }
                }
            }

            bbw.WriteInt32(count);

            for (var y = 0; y < _grid.GetLength(1); y++)
            {
                for (var x = 0; x < _grid.GetLength(0); x++)
                {
                    if (_grid[x, y] is ItemStack stack && !stack.IsEmpty)
                    {
                        bbw.WriteInt32(x);
                        bbw.WriteInt32(y);
                        bbw.WriteInt32(stack.Meta);
                        bbw.WriteString(stack.Item.UnlocalizedName);
                    }
                }
            }
        }
        public ServiceObjectFieldHandler(IServiceInternal service, Type type)
        {
            _service  = service;
            _type     = type;
            _typeName = _service.RegisterType(type);
            var writer = new ByteBufferWriter();

            writer.WriteString(_typeName);
            _configuration = writer.Data.ToByteArray();
        }
Example #4
0
        public DBObjectFieldHandler(IObjectDB objectDB, Type type)
        {
            _objectDB = objectDB;
            _type     = Unwrap(type);
            _indirect = _type != type;
            _typeName = _objectDB.RegisterType(_type);
            var writer = new ByteBufferWriter();

            writer.WriteString(_typeName);
            _configuration = writer.Data.ToByteArray();
        }
Example #5
0
        public void StoreEncryptedString(EncryptedString value)
        {
            var writer = new ByteBufferWriter();

            writer.WriteString(value);
            var plain   = writer.Data.AsSyncReadOnlySpan();
            var encSize = _symmetricCipher.CalcEncryptedSizeFor(plain);
            var enc     = new byte[encSize];

            _symmetricCipher.Encrypt(plain, enc);
            _writer.WriteByteArray(enc);
        }
Example #6
0
        public void WriteOrderedEncryptedString(EncryptedString value)
        {
            var writer = new ByteBufferWriter();

            writer.WriteString(value);
            var cipher  = _transaction.Owner.GetSymmetricCipher();
            var plain   = writer.Data.AsSyncReadOnlySpan();
            var encSize = cipher.CalcOrderedEncryptedSizeFor(plain);
            var enc     = new byte[encSize];

            cipher.OrderedEncrypt(plain, enc);
            _writer.WriteByteArray(enc);
        }
Example #7
0
        public void WriteEncryptedString(EncryptedString value)
        {
            var writer = new ByteBufferWriter();

            writer.WriteString(value);
            var cipher  = _serviceClient?.GetSymmetricCipher() ?? _serviceServer?.GetSymmetricCipher();
            var plain   = writer.Data.AsSyncReadOnlySpan();
            var encSize = cipher !.CalcEncryptedSizeFor(plain);
            var enc     = new byte[encSize];

            cipher.Encrypt(plain, enc);
            _writer.WriteByteArray(enc);
        }
Example #8
0
 public DBObjectFieldHandler(IObjectDB objectDb, Type type)
 {
     _objectDb = objectDb;
     _type     = Unwrap(type);
     _indirect = _type != type;
     if (_type.IsInterface || _type.IsAbstract)
     {
         _typeName      = null;
         _configuration = Array.Empty <byte>();
     }
     else
     {
         _typeName = (_objectDb as ObjectDB)?.RegisterType(_type, false);
         var writer = new ByteBufferWriter();
         writer.WriteString(_typeName);
         _configuration = writer.Data.ToByteArray();
     }
 }
Example #9
0
            public byte[] ToConfiguration()
            {
                var writer = new ByteBufferWriter();

                writer.WriteVUInt32((_signed ? 1u : 0) + (Flags ? 2u : 0) + 4u * (uint)Names.Length);
                foreach (var name in Names)
                {
                    writer.WriteString(name);
                }
                foreach (var value in Values)
                {
                    if (_signed)
                    {
                        writer.WriteVInt64((long)value);
                    }
                    else
                    {
                        writer.WriteVUInt64(value);
                    }
                }
                return(writer.Data.ToByteArray());
            }
Example #10
0
        private void writeDataByteBuffer(ByteBufferWriter writer, int index, object data)
        {
            Assert.IsNotNull(writer);
            Assert.IsNotNull(data);
            writer.WriteVarint32((uint)index);
            switch (index)
            {
            case 0:
                writer.WriteBool((bool)data);
                break;

            case 1:
                writer.WriteByte((byte)data);
                break;

            case 2:
                writer.WriteUint16((ushort)data);
                break;

            case 3:
                writer.WriteUint32((uint)data);
                break;

            case 4:
                writer.WriteUint64((ulong)data);
                break;

            case 5:
                writer.WriteInt16((short)data);
                break;

            case 6:
                writer.WriteInt32((int)data);
                break;

            case 7:
                writer.WriteInt64((long)data);
                break;

            case 8:
                writer.WriteVarint32((uint)data);
                break;

            case 9:
                writer.WriteVarint64((ulong)data);
                break;

            case 10:
                writer.WriteSVarint32((int)data);
                break;

            case 11:
                writer.WriteSVarint64((long)data);
                break;

            case 12:
                writer.WriteFloat((float)data);
                break;

            case 13:
                writer.WriteDouble((double)data);
                break;

            case 14:
                writer.WriteString((string)data);
                break;

            case 15: {
                var buffer = (byte[])data;
                writer.WriteVarint32((uint)buffer.Length);
                writer.WriteBuffer(buffer, 0, buffer.Length);
            }
            break;

            case 16: {
                writer.WriteVarint32((uint)(int)data);
                writer.Skip((int)data);
            }
            break;

            default:
                Assert.Fail();
                break;
            }
        }
Example #11
0
        public void TestSocketServerClientThread()
        {
            var  server = SocketBase.Create <SocketServer>();
            bool rc     = server.Bind("127.0.0.1:" + port);

            Assert.IsTrue(rc);

            List <SocketBase> clientSet = new List <SocketBase>();

            for (int i = 0; i < clientCount; i++)
            {
                var client = SocketBase.Create(SocketType.Client);
                rc = client.Connect("127.0.0.1:" + port);
                Assert.IsTrue(rc);
                clientSet.Add(client);
            }

            port++;

            var checkData = generateCheckData();

            rc = Thread.Wait(() => server.ConnectionCount == clientCount, 1000);
            Assert.IsTrue(rc);

            string hello  = "Hello";
            var    writer = new ByteBufferWriter(16);

            writer.WriteString(hello);
            var helloMessage = new Message(writer);

            for (int i = 0; i < clientCount; i++)
            {
                rc = clientSet[i].TrySend(ref helloMessage, 1000);
                Assert.IsTrue(rc);
            }

            List <uint> clientIdSet = new List <uint>(clientCount);

            for (int i = 0; i < clientCount; i++)
            {
                Message msg;
                rc = server.TryRecv(out msg, 1000);
                Assert.IsTrue(rc);
                Assert.Less(0, msg.RouteId);
                Assert.IsFalse(clientIdSet.Contains(msg.RouteId));
                clientIdSet.Add(msg.RouteId);
                var reader = msg.GetBufferReader();
                var value  = reader.ReadString();
                Assert.AreEqual(value, hello);
            }

            var threadPool = new ThreadPool(clientCount);

            threadPool.Send(() => {
                for (int c = 0; c < clientCount; c++)
                {
                    for (int i = 0; i < checkCount; i++)
                    {
                        var msg = generateMessage(checkData);
                        rc      = server.Send(clientIdSet[c], ref msg);
                        Assert.IsTrue(rc, "client id: " + clientIdSet[c] + ", message id: " + i);
                    }
                }
            });

            int result = 0;

            for (int c = 0; c < clientCount; c++)
            {
                var client   = clientSet[c];
                var clientId = clientIdSet[c];
                threadPool.Post(() => {
                    for (int i = 0; i < checkCount; i++)
                    {
                        Message msg;
                        rc = client.Recv(out msg);
                        Assert.IsTrue(rc, "client id: " + clientId + ", message id: " + i);
                        checkMessage(ref msg, checkData);
                        Atomic.Inc(ref result);
                    }
                });
            }

            rc = Thread.Wait(() => result == clientCount * checkCount, -1);
            Assert.IsTrue(rc);

            threadPool.Dispose();
            Assert.AreEqual(result, clientCount * checkCount);

            for (int i = 0; i < clientSet.Count; i++)
            {
                clientSet[i].Close();
                rc = clientSet[i].WaitClose(1000);
                Assert.IsTrue(rc);
            }

            server.Close();
            rc = server.WaitClose(1000);
            Assert.IsTrue(rc);
        }