private void SendFileStartPositionInfo(Socket client, int packetNumber, long packSize) { ComObject state = new ComObject { WorkSocket = client }; byte[] byteData = PacketUtils.PacketData(PacketUtils.ClientRequestFileTag(), BitConverter.GetBytes(packetNumber * packSize)); client.BeginSend(byteData, 0, byteData.Length, 0, SendFileRequestCallback, state); }
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 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); } } } } }