Example #1
0
	public override BaseMessage DynamicCreate (byte[] data, MessageHead head)
	{
		if(MessageInfo.MessageTypeCheck(MessageDataType.ProtoBuf))
		{
			ProtobufMessage message = new ProtobufMessage();
			message.DataHead = head;
			message.DataBody.m_SecondValue = data;
			return message;
		}

		return null;

	}
Example #2
0
        public async Task Basic_Mapped1Type_Test()
        {
            var from  = Accounts[0].Address.ToBase58();
            var pairA = "ELF";
            var to    = Accounts[1].Address.ToBase58();
            var pairB = "USDT";

            var protobufMessage = new ProtobufMessage
            {
                Int64Value  = 1,
                StringValue = "string",
                BoolValue   = true
            };

            await TestBasicSecurityContractStub.TestMapped2State.SendAsync(new ProtobufInput()
            {
                ProtobufValue = protobufMessage
            });

            var queryResult = (await TestBasicSecurityContractStub.QueryMappedState2.CallAsync(new ProtobufInput
            {
                ProtobufValue = protobufMessage
            }));

            queryResult.ShouldBe(protobufMessage);

            queryResult = (await TestBasicSecurityContractStub.QueryMappedState2.CallAsync(new ProtobufInput
            {
                ProtobufValue = new ProtobufMessage
                {
                    Int64Value = 0,
                    StringValue = "string",
                    BoolValue = true
                }
            }));
            queryResult.ShouldBe(new ProtobufMessage());
        }
Example #3
0
        public async Task TestMappedStateSizeLimit()
        {
            var stateSizeLimit = SmartContractConstants.StateSizeLimit;

            {
                var str      = Encoding.UTF8.GetString(new byte[stateSizeLimit]);
                var txResult = await TestBasicSecurityContractStub.TestMappedState.SendWithExceptionAsync(
                    new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        StringValue = str
                    }
                });

                txResult.TransactionResult.Error.ShouldContain($"exceeds limit of {stateSizeLimit}");

                await TestBasicSecurityContractStub.TestMappedState.SendAsync(new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        Int64Value = 1
                    }
                });

                var queryResult = await TestBasicSecurityContractStub.QueryMappedState.CallAsync(new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        Int64Value = 1
                    }
                });

                queryResult.Int64Value.ShouldBe(1);

                (await TestBasicSecurityContractStub.QueryMappedState.CallAsync(new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        Int64Value = 2
                    }
                })).ShouldBe(new ProtobufMessage());
            }

            {
                var str      = Encoding.UTF8.GetString(new byte[stateSizeLimit]);
                var txResult = await TestBasicSecurityContractStub.TestMapped1State.SendWithExceptionAsync(
                    new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        StringValue = str
                    }
                });

                txResult.TransactionResult.Error.ShouldContain($"exceeds limit of {stateSizeLimit}");

                var str1    = Encoding.UTF8.GetString(new byte[10]);
                var message = new ProtobufMessage
                {
                    BoolValue   = true,
                    Int64Value  = 1,
                    StringValue = str1
                };
                await TestBasicSecurityContractStub.TestMapped1State.SendAsync(new ProtobufInput
                {
                    ProtobufValue = message
                });

                var queryResult = await TestBasicSecurityContractStub.QueryMappedState1.CallAsync(new ProtobufInput
                {
                    ProtobufValue = message
                });

                queryResult.ShouldBe(message);

                (await TestBasicSecurityContractStub.QueryMappedState1.CallAsync(new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        BoolValue = true,
                        Int64Value = 2,
                        StringValue = str1
                    }
                })).ShouldBe(new ProtobufMessage());
            }

            {
                var str      = Encoding.UTF8.GetString(new byte[stateSizeLimit]);
                var txResult = await TestBasicSecurityContractStub.TestMapped2State.SendWithExceptionAsync(
                    new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        StringValue = str
                    }
                });

                await TestBasicSecurityContractStub.TestMapped2State.SendAsync(new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                    }
                });

                txResult.TransactionResult.Error.ShouldContain($"exceeds limit of {stateSizeLimit}");

                var str1    = Encoding.UTF8.GetString(new byte[10]);
                var message = new ProtobufMessage
                {
                    BoolValue   = true,
                    Int64Value  = 1,
                    StringValue = str1
                };

                await TestBasicSecurityContractStub.TestMapped2State.SendAsync(new ProtobufInput
                {
                    ProtobufValue = message
                });

                var queryResult = await TestBasicSecurityContractStub.QueryMappedState2.CallAsync(new ProtobufInput
                {
                    ProtobufValue = message
                });

                queryResult.ShouldBe(message);

                (await TestBasicSecurityContractStub.QueryMappedState2.CallAsync(new ProtobufInput
                {
                    ProtobufValue = new ProtobufMessage
                    {
                        BoolValue = true,
                        Int64Value = 2,
                        StringValue = str1
                    }
                })).ShouldBe(new ProtobufMessage());
            }

            {
                var str     = Encoding.UTF8.GetString(new byte[stateSizeLimit]);
                var message = new TradeMessage
                {
                    FromAmount = 1024
                };

                var txResult = await TestBasicSecurityContractStub.TestMapped3State.SendWithExceptionAsync(
                    new Complex3Input
                {
                    TradeDetails = new TradeMessage
                    {
                        FromAmount = 1,
                        Memo       = str
                    }
                });

                txResult.TransactionResult.Error.ShouldContain($"exceeds limit of {stateSizeLimit}");

                var str1 = Encoding.UTF8.GetString(new byte[10]);

                var complex3Input = new Complex3Input
                {
                    From         = str1,
                    To           = str1,
                    TradeDetails = message
                };
                await TestBasicSecurityContractStub.TestMapped3State.SendAsync(complex3Input);

                var queryResult = await TestBasicSecurityContractStub.QueryMappedState3.CallAsync(complex3Input);

                queryResult.FromAmount.ShouldBe(message.FromAmount);

                (await TestBasicSecurityContractStub.QueryMappedState3.CallAsync(new Complex3Input
                {
                    From = str1,
                    To = str1,
                    PairA = str1,
                    TradeDetails = message
                })).ShouldBe(new TradeMessage());
            }
        }
    void Listen()
    {
        IPAddress  ip            = IPAddress.Parse(host);
        IPEndPoint localEndPoint = new IPEndPoint(ip, Convert.ToInt32(port));

        listener = new Socket(ip.AddressFamily,
                              SocketType.Stream, ProtocolType.Tcp);
        listener.LingerState = new LingerOption(true, 0);

        try {// Bind the socket to the local endpoint and listen for incoming connections.
            Debug.Log("Binding socket to: " + localEndPoint);
            listener.Bind(localEndPoint);
            listener.Listen(10);

            while (!requestDisconnect)       //Start listening for connections.
            {
                handler = listener.Accept(); // Program suspended while waiting for an incoming connection.

                if (requestDisconnect)
                {
                    return;
                }

                //read header
                byte[]        headerBytes   = new byte[MessageHeader.HEADER_SIZE];
                int           bytesReceived = handler.Receive(headerBytes);
                MessageHeader header        = MessageHeader.ParseFromBytes(headerBytes);

                //if 0 bytes received, return. No valid message.
                if (bytesReceived == 0)
                {
                    if (requestDisconnect)
                    {
                        return;
                    }
                    continue;
                }

                Debug.Log("Got message type <" + header.MessageType + ">, Parsing: " + header.BodySize + "bytes");

                System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
                stopWatch.Start();

                //read body
                int    bodySize  = header.BodySize;
                byte[] bodyBytes = new byte[bodySize];

                int messageChunkSize = 100 * 1024;
                int messageBytesRead = 0;

                while (messageBytesRead < bodySize)
                {
                    if (requestDisconnect)
                    {
                        return;
                    }

                    byte [] bytes = new byte[messageChunkSize];

                    int bytesRec = handler.Receive(bytes);
                    if (bytesRec <= 0)
                    {
                        handler.Disconnect(true);
                        break;
                    }
                    Array.Copy(bytes, 0, bodyBytes, messageBytesRead, bytesRec);

                    messageBytesRead += bytesRec;

                    System.Threading.Thread.Sleep(1);
                }
                handler.Close();

                stopWatch.Stop();
                Debug.Log("Received " + messageBytesRead + " bytes in " + stopWatch.Elapsed.Milliseconds + " ms");
                stopWatch.Reset();

                //create message
                Message message = Message.ParseFromBytes(headerBytes, bodyBytes);

                ProtobufMessage protobufMessage = MessageDeserializer.DeserializeMessage(message);
                messageQueue.Enqueue(protobufMessage);

                System.Threading.Thread.Sleep(1);
            }
        } catch (Exception e) {
            Debug.Log("ERROR: " + localEndPoint + ": Exeception caught: " + e.ToString());
        }
    }