コード例 #1
0
        public GossipConnection(TcpClient client)
        {
            if (client == null) throw new ArgumentNullException("client");
            Client = client;
            RemoteEndPoint = client.Client.RemoteEndPoint;
            TimestampConnected = GossipTimestampProvider.CurrentTimestamp;
            TimestampLastActivity = TimestampConnected;

            Stream = new BinaryNetworkStream(client.GetStream());
            Stream.OnDisconnected += s => OnDisconnected(this);
        }
コード例 #2
0
        public async Task WriteToStreamAsync(BinaryNetworkStream stream)
        {
            var intBuf = BitConverter.GetBytes(MessageType);
            await stream.WriteAsync(intBuf, 0, intBuf.Length);

            var buf = Serialize();

            intBuf = BitConverter.GetBytes(buf.Length);
            await stream.WriteAsync(intBuf, 0, intBuf.Length);

            await stream.WriteAsync(buf, 0, buf.Length);
        }
コード例 #3
0
        public GossipConnection(TcpClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            Client                = client;
            RemoteEndPoint        = client.Client.RemoteEndPoint;
            TimestampConnected    = GossipTimestampProvider.CurrentTimestamp;
            TimestampLastActivity = TimestampConnected;

            Stream = new BinaryNetworkStream(client.GetStream());
            Stream.OnDisconnected += s => OnDisconnected(this);
        }
コード例 #4
0
        public static async Task <GossipMessage> ReadFromStreamAsync(BinaryNetworkStream stream)
        {
            var intBuf = new byte[4];

            var read = await stream.ReadAsync(intBuf, 0, intBuf.Length);

            if (read != 4)
            {
                return(null);
            }
            int messageType = BitConverter.ToInt32(intBuf, 0);

            read = await stream.ReadAsync(intBuf, 0, intBuf.Length);

            if (read != 4)
            {
                return(null);
            }
            int size = BitConverter.ToInt32(intBuf, 0);

            if (size < 0)
            {
                return(null);
            }
            if (size == 0)
            {
                return(GossipMessageFactory.CreateMessage(messageType, _blankArray));
            }

            var result = new byte[size];
            int offset = 0;

            while (true)
            {
                read = await stream.ReadAsync(result, offset, size - offset);

                if (read == 0)
                {
                    return(null);
                }
                offset += read;
                if (offset >= size)
                {
                    return(GossipMessageFactory.CreateMessage(messageType, result));
                }
            }
        }