Ejemplo n.º 1
0
        private PeerMessage BuildMessage <TArgs>(string handlerName, TArgs args)
        {
            PeerMessage msg = new PeerMessage();

            msg.Message = handlerName;
            msg.Data    = Serialize(args);

            return(msg);
        }
        public async Task <Block[]> RequestBlocks(int blockHeight)
        {
            PeerMessage msg = this.BuildMessage(this.BlocksRequested, blockHeight);

            TaskCompletionSource <PeerMessage> task = new TaskCompletionSource <PeerMessage>();

            this.tasks.Add(msg.Id, task);
            this.SubmitMessage(msg);

            PeerMessage result = await task.Task;

            return(ProtoBuf.Serializer.Deserialize <Block[]>(result.DataStream));
        }
Ejemplo n.º 3
0
        private void TcpStreamHandler() => Task.Run(() =>
        {
            while (!this.cancellationToken.IsCancellationRequested)
            {
                if (!this.Tcp.Connected)
                {
                    break;
                }

                NetworkStream stream = this.Tcp.GetStream();

                while (!this.cancellationToken.IsCancellationRequested)
                {
                    if (!this.Tcp.Connected)
                    {
                        break;
                    }

                    if (!stream.DataAvailable)
                    {
                        Task.Delay(100).Wait();
                        continue;
                    }

                    PeerMessage msg = ProtoBuf.Serializer.DeserializeWithLengthPrefix <PeerMessage>(stream, ProtoBuf.PrefixStyle.Base128);

                    TaskCompletionSource <PeerMessage> waitingTask;
                    if (this.tasks.TryGetValue(msg.Id, out waitingTask))
                    {
                        // Is a result message returned from another peer

                        this.tasks.Remove(msg.Id);
                        waitingTask.SetResult(msg);
                    }
                    else
                    {
                        // Is a request message from another peer

                        MethodInfo messageMethod  = typeof(Peer).GetMethod(msg.Message, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        Type messageMethodArgType = messageMethod.GetParameters().FirstOrDefault()?.ParameterType;

                        object result;
                        if (messageMethodArgType != null)
                        {
                            object arg = ProtoBuf.Serializer.Deserialize(messageMethodArgType, msg.DataStream);

                            result = messageMethod.Invoke(this, new object[] { arg });
                        }
                        else
                        {
                            result = messageMethod.Invoke(this, null);
                        }

                        if (messageMethod.ReturnType != null)
                        {
                            // the message has a return type. Write the result back to the stream.
                            PeerMessage resultMsg = new PeerMessage();
                            resultMsg.Id          = msg.Id;
                            resultMsg.Data        = this.Serialize(result);

                            this.SubmitMessage(resultMsg);
                        }
                    }
                }
            }
        });
Ejemplo n.º 4
0
        private void SubmitMessage(PeerMessage msg)
        {
            NetworkStream stream = this.Tcp.GetStream();

            ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, msg, ProtoBuf.PrefixStyle.Base128);
        }