private void ProcessSendFile(SocketAsyncEventArgs e)
        {
            var bytesRead = e.BytesTransferred;

            if (bytesRead > 0 && e.SocketError == SocketError.Success)
            {
                var receiveData = e.Buffer.Skip(e.Offset).Take(bytesRead).ToArray();
                var dataList    = PacketUtils.SplitBytes(receiveData, PacketUtils.ClientRequestFileTag());
                if (dataList != null)
                {
                    foreach (var request in dataList)
                    {
                        if (PacketUtils.IsPacketComplete(request))
                        {
                            int startPosition = PacketUtils.GetRequestFileStartPosition(request);

                            var packetSize = PacketUtils.GetPacketSize(_serverPath, _downloadChannelsCount);
                            if (packetSize != 0)
                            {
                                byte[] filedata     = FileUtils.GetFile(_serverPath, startPosition, packetSize);
                                byte[] packetNumber = BitConverter.GetBytes(startPosition / packetSize);

                                Console.WriteLine("Receive File Request PacketNumber: " + startPosition / packetSize);

                                if (filedata != null)
                                {
                                    //ready to send back to Client
                                    byte[] segmentedFileResponseData = PacketUtils.PacketData(PacketUtils.ServerResponseFileTag(), filedata, packetNumber);

                                    Array.Clear(e.Buffer, e.Offset, e.Count);
                                    Array.Copy(segmentedFileResponseData, 0, e.Buffer, e.Offset, segmentedFileResponseData.Length);

                                    e.Completed -= ProcessFilePosition_Completed;
                                    e.Completed += ProcessSendFile_Completed;

                                    if (!e.AcceptSocket.SendAsync(e))
                                    {
                                        CloseClientSocket(e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                CloseClientSocket(e);
            }
        }
 private bool CheckPackets(Dictionary <Socket, byte[]> dictionary, int downloadChannelsCount)
 {
     foreach (var socket in dictionary.Keys)
     {
         var response = dictionary[socket];
         var packets  = PacketUtils.SplitBytes(response, PacketUtils.ServerResponseFileTag());
         if (packets != null && packets.Any())
         {
             var result = packets.FirstOrDefault();
             if (Crc16Utils.CheckCrcCode(result) && PacketUtils.IsPacketComplete(result))
             {
                 var packetNumber = PacketUtils.GetResponsePacketNumber(result);
                 var data         = PacketUtils.GetData(PacketUtils.ServerResponseFileTag(), result);
                 if (!ResultPacketDict.ContainsKey(packetNumber))
                 {
                     ResultPacketDict.Add(packetNumber, data);
                 }
             }
         }
     }
     return(ResultPacketDict.Count == downloadChannelsCount);
 }
        private static void ReadFilePositionRequestCallback(IAsyncResult ar)
        {
            ComObject state     = (ComObject)ar.AsyncState;
            Socket    handler   = state.WorkSocket;
            int       bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                var receiveData = state.Buffer.Take(bytesRead).ToArray();
                var dataList    = PacketUtils.SplitBytes(receiveData, PacketUtils.ClientRequestFileTag());
                if (dataList != null)
                {
                    foreach (var request in dataList)
                    {
                        if (PacketUtils.IsPacketComplete(request))
                        {
                            int startPosition = PacketUtils.GetRequestFileStartPosition(request);
                            SendFileResponse(handler, startPosition);
                        }
                    }
                }
            }
        }