Skip to content

Commit

Permalink
Added ProgressCallback; Added options to specify request range; Remov…
Browse files Browse the repository at this point in the history
…ed auto created CurlMultiUpdater since it would cause unexpected behaviours when hit stop button in Unity
  • Loading branch information
root authored and root committed Nov 9, 2019
1 parent 7e2fb7b commit a156f1f
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 166 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes.meta → Assets/Samples.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions Assets/Samples/SampleResumeDownload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using CurlUnity;
using System;
using System.IO;
using UnityEngine;

public class SampleResumeDownload : MonoBehaviour
{
public string m_link;
public string m_localPath;

private long m_downloadedBytes;
private long m_nowBytes;
private long m_totalBytes;

private CurlMulti m_multi;

void Start()
{
m_multi = new CurlMulti();
ResumeDownload();
}

async void ResumeDownload()
{
var easy = new CurlEasy();
easy.uri = new Uri(m_link);
easy.method = "HEAD";
easy.debug = true;

if (await easy.PerformAsync() == CURLE.OK)
{
if (easy.GetResponseHeader("Accept-Ranges") == "bytes")
{
m_downloadedBytes = 0;
if (File.Exists(m_localPath)) m_downloadedBytes = (new FileInfo(m_localPath)).Length;
int.TryParse(easy.GetResponseHeader("Content-Length"), out var contentLength);
if (contentLength != 0 && contentLength <= m_downloadedBytes)
{
Debug.LogWarning($"Link {m_link} already downloaded at: {m_localPath}");
}
else
{
Debug.Log($"Start to download {m_link} in range {m_downloadedBytes}-{contentLength}");
easy.method = "GET";
easy.outputPath = m_localPath;
easy.rangeStart = m_downloadedBytes;
easy.rangeEnd = contentLength;
easy.progressCallback = (dltotal, dlnow, ultotal, ulnow, _) =>
{
m_nowBytes = dlnow;
m_totalBytes = dltotal;
return 0;
};
easy.performCallback = (result, _) =>
{
if (result == CURLE.OK)
{
Debug.Log($"Link {m_link} downloaded");
}
else
{
Debug.LogWarning($"Failed to download link {m_link}");
}
};
// Use multi perform so we could interrupt the download
easy.MultiPerform(m_multi);
}
}
else
{
Debug.LogWarning($"Link {m_link} doesn't support Accept-Ranges");
}
}
else
{
Debug.LogWarning($"Failed to get info of link {m_link}");
}
}

private void OnGUI()
{
GUILayout.Label($"Download progress {m_nowBytes}-{m_totalBytes} ({m_downloadedBytes} already downloaded)");
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RenderSettings:
m_AmbientIntensity: 1
m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
m_SkyboxMaterial: {fileID: 0}
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
Expand All @@ -37,8 +37,8 @@ RenderSettings:
m_ReflectionBounces: 1
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 170076734}
m_IndirectSpecularColor: {r: 0.44657868, g: 0.49641263, b: 0.5748171, a: 1}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -112,75 +112,6 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &170076733
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 170076735}
- component: {fileID: 170076734}
m_Layer: 0
m_Name: Directional Light
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &170076734
Light:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 170076733}
m_Enabled: 1
serializedVersion: 8
m_Type: 1
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 1
m_Range: 10
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 2
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 1
m_LightShadowCasterMode: 0
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &170076735
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 170076733}
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &534669902
GameObject:
m_ObjectHideFlags: 0
Expand All @@ -191,23 +122,13 @@ GameObject:
m_Component:
- component: {fileID: 534669905}
- component: {fileID: 534669904}
- component: {fileID: 534669903}
- component: {fileID: 534669906}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!81 &534669903
AudioListener:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 534669902}
m_Enabled: 1
--- !u!20 &534669904
Camera:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -264,15 +185,65 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &534669906
--- !u!1 &1550258503
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1550258506}
- component: {fileID: 1550258504}
- component: {fileID: 1550258505}
m_Layer: 0
m_Name: CurlTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1550258504
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 534669902}
m_GameObject: {fileID: 1550258503}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c28edb20c545c4ba39554ce237ca7da9, type: 3}
m_Script: {fileID: 11500000, guid: 946a1cbad290d13488d518a442eb30ce, type: 3}
m_Name:
m_EditorClassIdentifier:
autoStart: 1
multiThread: 1
busyInterval: 1
idleInterval: 50
--- !u!114 &1550258505
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1550258503}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b3e9b55d2b8cc1f478aaa78238c05e4e, type: 3}
m_Name:
m_EditorClassIdentifier:
m_link: https://public-cdn.cloud.unitychina.cn/hub/prod/UnityHubSetup.exe
m_localPath: UnityHubSetup.exe
--- !u!4 &1550258506
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1550258503}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions Assets/curl-unity/CurlTest.cs

This file was deleted.

10 changes: 7 additions & 3 deletions Assets/curl-unity/Native/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ namespace CurlUnity
public static class Delegates
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int WriteFunction(IntPtr ptr, int size, int nmemb, IntPtr userdata);
public delegate long WriteFunction(IntPtr ptr, long size, long nmemb, IntPtr userdata);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int HeaderFunction(IntPtr ptr, int size, int nmemb, IntPtr userdata);
public delegate long HeaderFunction(IntPtr ptr, long size, long nmemb, IntPtr userdata);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int DebugFunction(IntPtr ptr, CURLINFODEBUG type, IntPtr data, int size, IntPtr userdata);
public delegate long ProgressFunction(IntPtr clientp, long dltotal, long dlnow, long ultotal, long ulnow);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate long DebugFunction(IntPtr ptr, CURLINFODEBUG type, IntPtr data, long size, IntPtr userdata);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void LockFunction(IntPtr ptr, CURLLOCKDATA data, CURLLOCKACCESS access, IntPtr userdata);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
Expand Down Expand Up @@ -66,6 +68,8 @@ public static class Lib
public static extern CURLE curl_easy_setopt_ptr(IntPtr easyPtr, CURLOPT option, Delegates.HeaderFunction arg);
[DllImport(LIB_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern CURLE curl_easy_setopt_ptr(IntPtr easyPtr, CURLOPT option, Delegates.DebugFunction arg);
[DllImport(LIB_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern CURLE curl_easy_setopt_ptr(IntPtr easyPtr, CURLOPT option, Delegates.ProgressFunction arg);

[DllImport(LIB_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern CURLE curl_easy_perform(IntPtr easyPtr);
Expand Down
Loading

0 comments on commit a156f1f

Please sign in to comment.