Ejemplo n.º 1
0
 public void WriteSequenceToSocket(SequenceContext seqCtx, IChannel senderChannel, Guid guid,
                                   BufferObject buffObj,
                                   Socket sock, AsyncCallback sendCallback)
 {
     WritePacketToSocket(seqCtx.InitPacket, senderChannel, guid, buffObj, sock, sendCallback, null);
     for (var i = 0; i < seqCtx.SequencePackets.Length; i++)
     {
         WritePacketToSocket(seqCtx.SequencePackets[i], senderChannel, guid, buffObj, sock, sendCallback, null);
     }
 }
Ejemplo n.º 2
0
 public void ReadLength(AsyncCallback callback, BufferObject buffObj, Socket _sock, int readLen)
 {
     if (readLen >= 4)
     {
         buffObj.PacketSize = BitConverter.ToInt32(buffObj.RecBuff, 0);
         if (buffObj.PacketSize < buffObj.BufferSize)
         {
             _sock.BeginReceive(buffObj.RecBuff, 0, buffObj.PacketSize, 0, callback, buffObj);
         }
         else
         {
             _sock.BeginReceive(buffObj.RecBuff, 0, buffObj.BufferSize, 0, callback, buffObj);
         }
     }
 }
Ejemplo n.º 3
0
        public void WritePacketToSocket(IDataPacket data, IChannel senderChannel, Guid guid, BufferObject buffObj,
                                        Socket sock, AsyncCallback sendCallback, SocketOperationContext operationCtx)
        {
            var ms = new MemoryStream();

            data.SerializeTo(ms);

            sbyte integrityHashSize = 0;

            if (senderChannel != null)
            {
                switch (senderChannel.IntegrityHash)
                {
                case Security.PacketIntegrityHash.Sha256:
                    integrityHashSize = 32;
                    break;

                case Security.PacketIntegrityHash.Crc32:
                    integrityHashSize = 4;
                    break;

                case Security.PacketIntegrityHash.Elf32:
                    integrityHashSize = 4;
                    break;
                }
            }

            var headerSize = 1 + (operationCtx != null ? 32 : 0) +
                             (senderChannel != null && senderChannel.EnsurePacketIntegrity ? integrityHashSize + 2 : 1);
            var sendBuff = new byte[ms.Length + headerSize + 4];

            FastBuffer.MemCpy(BitConverter.GetBytes(headerSize + ms.Length), 0, sendBuff, 0, 4);
            sendBuff[4] = operationCtx == null ? ((byte)0x0) : ((byte)0x1);
            sendBuff[5] = senderChannel != null && senderChannel.EnsurePacketIntegrity ? (byte)0x1 : (byte)0x0;
            if (senderChannel != null && senderChannel.EnsurePacketIntegrity)
            {
                sendBuff[6] = (byte)senderChannel.IntegrityHash;
            }

            if (operationCtx != null)
            {
                FastBuffer.MemCpy(operationCtx.OperationGuid.ToByteArray(), 0, sendBuff, 6, 16);
                FastBuffer.MemCpy(guid.ToByteArray(), 0, sendBuff, 22, 16);
            }

            var tmpBuff = new byte[ms.Length];

            ms.Seek(0, SeekOrigin.Begin);
            ms.Read(tmpBuff, 0, tmpBuff.Length);

            FastBuffer.MemCpy(tmpBuff, 0, sendBuff, operationCtx != null ? 38 : 6, tmpBuff.Length);

            var sendObj = new Peer.Peer.SendObject {
                Channel = senderChannel, Packet = data
            };

            if (sock.Connected)
            {
                sock.BeginSend(sendBuff, 0, sendBuff.Length, 0, sendCallback, sendObj);
            }
        }