private static void FoundUpdateFileResponse(Socket handler) { byte[] foundUpdateFileData = PacketUtils.PacketData(PacketUtils.ServerFoundFileInfoTag(), null); ComObject state = new ComObject { WorkSocket = handler }; handler.BeginSend(foundUpdateFileData, 0, foundUpdateFileData.Length, 0, HasFoundUpdateFileCallback, state); }
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 FindUpdateFileInfo(Socket client, int packetNumber) { ComObject state = new ComObject { WorkSocket = client, PacketNumber = packetNumber }; byte[] byteData = PacketUtils.PacketData(PacketUtils.ClientFindFileInfoTag(), Encoding.UTF8.GetBytes(_updateFileName)); client.BeginSend(byteData, 0, byteData.Length, 0, FindUpdateFileCallback, 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 void ProcessReceiveFindFileRequest(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.ClientFindFileInfoTag()); if (dataList != null && dataList.Any()) { var request = PacketUtils.GetData(PacketUtils.ClientFindFileInfoTag(), dataList.FirstOrDefault()); string str = System.Text.Encoding.UTF8.GetString(request); var infos = str.Split('_'); var productName = infos[0]; var revitVersion = infos[1]; var currentVersion = infos[2]; var mainFolder = AppDomain.CurrentDomain.BaseDirectory.Replace("bin", "TestFile"); var serverFileFolder = Path.Combine(mainFolder, "Server"); var serverFileFiles = new DirectoryInfo(serverFileFolder).GetFiles(); var updatefile = serverFileFiles.FirstOrDefault(x => x.Name.Contains(productName) && x.Name.Contains(revitVersion) && x.Name.Contains(currentVersion)); if (updatefile != null) { if (string.IsNullOrEmpty(updatefile.FullName) || !File.Exists(updatefile.FullName)) { return; } _serverPath = updatefile.FullName; //ready to send back to Client byte[] foundUpdateFileData = PacketUtils.PacketData(PacketUtils.ServerFoundFileInfoTag(), null); Array.Clear(e.Buffer, e.Offset, e.Count); Array.Copy(foundUpdateFileData, 0, e.Buffer, e.Offset, foundUpdateFileData.Length); e.Completed -= ProcessAccept_Completed; e.Completed += ProcessReceiveFindFileRequest_Completed; if (!e.AcceptSocket.SendAsync(e)) { ProcessFilePosition(e); } } } } }
private static void SendFileResponse(Socket handler, int startPosition) { var packetSize = PacketUtils.GetPacketSize(_serverPath, _downloadChannelsCount); if (packetSize != 0) { byte[] filedata = FileUtils.GetFile(_serverPath, startPosition, packetSize); byte[] packetNumber = BitConverter.GetBytes(startPosition / packetSize); if (filedata != null) { byte[] segmentedFileResponseData = PacketUtils.PacketData(PacketUtils.ServerResponseFileTag(), filedata, packetNumber); ComObject state = new ComObject { WorkSocket = handler }; handler.BeginSend(segmentedFileResponseData, 0, segmentedFileResponseData.Length, 0, SendFileResponseCallback, state); } } else { handler.Shutdown(SocketShutdown.Both); handler.Close(); } }