Exemple #1
0
        public void SendMessage(IPeerWireClient peerWireClient, IPEndPoint[] addedEndPoints, byte[] flags, IPEndPoint[] droppedEndPoints)
        {
            if (addedEndPoints == null && droppedEndPoints == null)
            {
                return;
            }

            BDict d = new BDict();

            if (addedEndPoints != null)
            {
                byte[] added = new byte[addedEndPoints.Length * 6];
                for (int x = 0; x < addedEndPoints.Length; x++)
                {
                    addedEndPoints[x].Address.GetAddressBytes().CopyTo(added, x * 6);
                    BitConverter.GetBytes((ushort)addedEndPoints[x].Port).CopyTo(added, (x * 6) + 4);
                }

                d.Add("added", new BString {
                    ByteValue = added
                });
            }

            if (droppedEndPoints != null)
            {
                byte[] dropped = new byte[droppedEndPoints.Length * 6];
                for (int x = 0; x < droppedEndPoints.Length; x++)
                {
                    droppedEndPoints[x].Address.GetAddressBytes().CopyTo(dropped, x * 6);

                    dropped.SetValue((ushort)droppedEndPoints[x].Port, (x * 6) + 2);
                }

                d.Add("dropped", new BString {
                    ByteValue = dropped
                });
            }

            _parent.SendExtended(peerWireClient, _parent.GetOutgoingMessageID(peerWireClient, this), BencodingUtils.EncodeBytes(d));
        }
        public void RequestMetaData(IPeerWireClient peerWireClient)
        {
            byte[] sendBuffer = new byte[0];

            for (Int32 i = 0; i < _pieceCount; i++)
            {
                BDict masterBDict = new BDict
                {
                    { "msg_type", (BInt)0 },
                    { "piece", (BInt)i }
                };

                String encoded = BencodingUtils.EncodeString(masterBDict);

                byte[] buffer = Pack.Int32(2 + encoded.Length, Pack.Endianness.Big);
                buffer = buffer.Concat(new byte[] { 20 }).ToArray();
                buffer = buffer.Concat(new[] { _parent.GetOutgoingMessageID(peerWireClient, this) }).ToArray();
                buffer = buffer.Concat(Encoding.GetEncoding(1252).GetBytes(encoded)).ToArray();

                sendBuffer = sendBuffer.Concat(buffer).ToArray();
            }

            peerWireClient.SendBytes(sendBuffer);
        }