Skip to content
This repository was archived by the owner on Nov 25, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 21 additions & 41 deletions src/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Mapbox.Map {
using System.Linq;
using System.Threading;
using System.IO;
using System.ComponentModel;

/// <summary>
/// The Mapbox Map abstraction will take care of fetching and decoding
Expand Down Expand Up @@ -58,7 +59,7 @@ private void OnQueueEmpty() {
#endregion


private int _MainThreadId;
private readonly SynchronizationContext syncContext;
private bool _IsDisposed = false;
private bool _PauseTileUpdates = false;
private TileFetcher _TileFetcher;
Expand All @@ -79,14 +80,13 @@ private void OnQueueEmpty() {
/// <param name="memoryTileCacheMax">Maximum number of tiles to cache in memory.</param>
/// <param name="numberOfThreads">Size of threadpool for paralell tile fetching.</param>
public Map(
int mainThreadId
, IFileSource fileSource
IFileSource fileSource
, uint memoryTileCacheMin = 9
, uint memoryTileCacheMax = 256
, uint numberOfThreads = 4
) {

_MainThreadId = mainThreadId;
syncContext = AsyncOperationManager.SynchronizationContext;
if(null == fileSource) {
throw new ArgumentNullException("fileSource");
}
Expand All @@ -100,8 +100,7 @@ int mainThreadId
_Zoom = 0;

_TileFetcher = new TileFetcher(
_MainThreadId
, fileSource
fileSource
, (int)memoryTileCacheMin
, (int)memoryTileCacheMax
, null
Expand Down Expand Up @@ -314,20 +313,7 @@ public void DownloadTiles() {


private void TileFetcher_QueueEmpty(object sender, EventArgs e) {
//if (UnityToolbag.Dispatcher.isMainThread)
//{
// OnQueueEmpty();
//}
//else
//{
// UnityToolbag.Dispatcher.Invoke(() =>
// {
// OnQueueEmpty();
// });
//}
Mapbox.Threading.Dispatcher.Invoke(_MainThreadId, () => {
OnQueueEmpty();
});
syncContext.Post(delegate { OnQueueEmpty(); }, null);
}


Expand All @@ -336,34 +322,28 @@ private void TileFetcher_TileReceived(object sender, TileFetcherTileReceivedEven
}

private void addTile(byte[] tileData, CanonicalTileId tileId, bool hasError, string errorMessage) {

T tile = new T();
tile.Id = tileId;

//clone byte array to get rid of references
//TODO: profile if this really helps
byte[] localTileData = null;
using(MemoryStream ms = new MemoryStream(tileData)) {
localTileData = ms.ToArray();
if(null != tileData) {
byte[] localTileData = null;
using(MemoryStream ms = new MemoryStream(tileData)) {
localTileData = ms.ToArray();
}
tile.ParseTileData(localTileData);
}
T tile = new T();
tile.Id = tileId;
tile.ParseTileData(localTileData);

tile.SetState(Tile.State.Loaded);
if(hasError) { tile.SetError(errorMessage); }
if(hasError) {
tile.SetError(errorMessage);
}
lock(_TilesLock) {
_Tiles.Add(tile);
}
//if (UnityToolbag.Dispatcher.isMainThread)
//{
// OnTileReceived(tile);
//}
//else
//{
// UnityToolbag.Dispatcher.Invoke(() =>
// {
// OnTileReceived(tile);
// });
//}
Mapbox.Threading.Dispatcher.Invoke(_MainThreadId, () => {
OnTileReceived(tile);
});
syncContext.Post(delegate { OnTileReceived(tile); }, null);
}


Expand Down
84 changes: 33 additions & 51 deletions src/Map/TileFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public byte[] Get(CanonicalTileId index) {
}
}

private int _MainThreadId;
private IFileSource _FileSource;
private MemoryCache<byte[]> _volatileCache;
private ITileCache<byte[]> _permaCache;
Expand All @@ -41,8 +40,8 @@ public byte[] Get(CanonicalTileId index) {
/// <param name="minTiles">min. number of tiles in memory cache</param>
/// <param name="maxTiles">max. number of tiles in memory cache</param>
/// <param name="permaCache">The perma cache</param>
internal TileFetcher(int mainThreadId, IFileSource fileSource, Tile tile, int minTiles, int maxTiles, ITileCache<byte[]> permaCache)
: this(mainThreadId, fileSource, minTiles, maxTiles, permaCache, 4) {
internal TileFetcher(IFileSource fileSource, Tile tile, int minTiles, int maxTiles, ITileCache<byte[]> permaCache)
: this(fileSource, minTiles, maxTiles, permaCache, 4) {
}

/// <summary>
Expand All @@ -54,13 +53,12 @@ internal TileFetcher(int mainThreadId, IFileSource fileSource, Tile tile, int mi
/// <param name="permaCache">The perma cache</param>
/// <param name="maxNumberOfThreads">The maximum number of threads used to get the tiles</param>
internal TileFetcher(
int mainThreadId
, IFileSource fileSource
IFileSource fileSource
, int minTiles
, int maxTiles
, ITileCache<byte[]> permaCache,
int maxNumberOfThreads) {
_MainThreadId = mainThreadId;
int maxNumberOfThreads
) {
_FileSource = fileSource;
_volatileCache = new MemoryCache<byte[]>(minTiles, maxTiles);
_permaCache = permaCache ?? NoopCache.Instance;
Expand Down Expand Up @@ -149,57 +147,42 @@ private object GetTileOnThread(object parameter) {
try {

_openTileRequests.TryAdd(tileId, 1);
Mapbox.Threading.Dispatcher.Invoke(_MainThreadId, () => {
try {
_FileSource.Request(tileUrl, (Response response) => {
if(!string.IsNullOrEmpty(response.Error)) {
//TODO: evaluate headers sent by server, or do this in IFileSource
//if (null != response.Headers)
//{
// string hdrs = "";
// foreach (var hdr in response.Headers)
// {
// hdrs += string.Format("{0}: {1}\n", hdr.Key, hdr.Value);
// }
// UnityEngine.Debug.LogErrorFormat("+++++ TileFetcher.GetTileOnThread(), _FileSource response.Error: \n[{0}]\n[{1}]\nheaders:\n{2}", tileUrl, response.Error, hdrs);
//}
}
result = response.Data;
if(null == result) {
errorMessage = "+++++ TileFetcher.GetTileOnThread(), no data receiced, " + response.Error;
} else {
try {
result = Compression.Decompress(result);
}
catch(Exception exDecompress) {
string msg = string.Format("+++++ TileFetcher.GetTileOnThread(), exception: [{0}], {1}", exDecompress, response.Error);
errorMessage = msg;
#if UNITY_EDITOR
UnityEngine.Debug.LogError(msg);
#else
System.Diagnostics.Debug.WriteLine(msg, "ERROR");
#endif

}
}
fetched = true;
});
_FileSource.Request(tileUrl, (Response response) => {
if(!string.IsNullOrEmpty(response.Error)) {
//TODO: evaluate headers sent by server, or do this in IFileSource
//if (null != response.Headers)
//{
// string hdrs = "";
// foreach (var hdr in response.Headers)
// {
// hdrs += string.Format("{0}: {1}\n", hdr.Key, hdr.Value);
// }
// UnityEngine.Debug.LogErrorFormat("+++++ TileFetcher.GetTileOnThread(), _FileSource response.Error: \n[{0}]\n[{1}]\nheaders:\n{2}", tileUrl, response.Error, hdrs);
//}
}
catch(Exception e) {
PreserveStackTrace(e);
string msg = string.Format("+++++ TileFetcher.GetTileOnThread(), exception: [{0}]", e);
errorMessage = msg;
result = response.Data;
if(null == result) {
errorMessage = "+++++ TileFetcher.GetTileOnThread(), no data receiced, " + response.Error;
} else {
try {
result = Compression.Decompress(result);
}
catch(Exception exDecompress) {
string msg = string.Format("+++++ TileFetcher.GetTileOnThread(), exception: [{0}], {1}", exDecompress, response.Error);
errorMessage = msg;
#if UNITY_EDITOR
UnityEngine.Debug.LogError(msg);
UnityEngine.Debug.LogError(msg);
#else
System.Diagnostics.Debug.WriteLine(msg, "ERROR");
System.Diagnostics.Debug.WriteLine(msg, "ERROR");
#endif
fetched = true;
}
}
fetched = true;
});
}

catch(Exception ex) {
PreserveStackTrace(ex);
string msg = string.Format("+++++ TileFetcher.GetTileOnThread(), exception: [{0}]", ex);
errorMessage = msg;
#if UNITY_EDITOR
Expand All @@ -210,8 +193,7 @@ private object GetTileOnThread(object parameter) {
fetched = true;
}

//HACK: couldn't find a way to make UnityToolbag.Dispatcher.Invoke() work
//only InvokeAsync did the job
//HACK: wait till request has finish
while(!fetched) {
Thread.Sleep(5);
}
Expand Down
74 changes: 0 additions & 74 deletions src/Utils/Threading.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Utils/Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<Compile Include="JsonConverters\PolylineToGeoCoordinateListConverter.cs" />
<Compile Include="JsonConverters\JsonConverters.cs" />
<Compile Include="Conversions.cs" />
<Compile Include="Threading.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
Loading