This commit is contained in:
djes
2016-08-02 14:25:31 +02:00
parent 8505e67974
commit 767472053d
2 changed files with 195 additions and 115 deletions

View File

@@ -1026,27 +1026,27 @@ CompilerIf #PB_Compiler_OS = #PB_OS_Windows
EndImport EndImport
CompilerEndIf CompilerEndIf
ProcedureC curlWriteData(*ptr, Size, NMemB, *Stream) ; ProcedureC curlWriteData(*ptr, Size, NMemB, *Stream)
;retreives utf-8/ascii encoded data ; ;retreives utf-8/ascii encoded data
Protected SizeProper.i = Size & 255 ; Protected SizeProper.i = Size & 255
Protected NMemBProper.i = NMemB ; Protected NMemBProper.i = NMemB
Protected MyDataS.s ; Protected MyDataS.s
;Shared ReceivedData.s ; ;Shared ReceivedData.s
;
MyDataS = PeekS(*ptr, SizeProper * NMemBProper, #PB_UTF8) ; MyDataS = PeekS(*ptr, SizeProper * NMemBProper, #PB_UTF8)
ReceivedData + MyDataS ; ReceivedData + MyDataS
ProcedureReturn SizeProper * NMemBProper ; ProcedureReturn SizeProper * NMemBProper
EndProcedure ; EndProcedure
Procedure.s curlGetData() ; Procedure.s curlGetData()
;Shared ReceivedData.s ; ;Shared ReceivedData.s
Protected ReturnData.s ; Protected ReturnData.s
;
ReturnData.s = ReceivedData.s ; ReturnData.s = ReceivedData.s
ReceivedData.s = "" ; ReceivedData.s = ""
;
ProcedureReturn ReturnData.s ; ProcedureReturn ReturnData.s
EndProcedure ; EndProcedure
Procedure.s str2curl(string.s) Procedure.s str2curl(string.s)
Protected *curlstring, newstring.s Protected *curlstring, newstring.s
@@ -1065,7 +1065,7 @@ EndProcedure
; EnableXP ; EnableXP
; EnableBuildCount = 0 ; EnableBuildCount = 0
; IDE Options = PureBasic 5.42 LTS (Windows - x86) ; IDE Options = PureBasic 5.42 LTS (Windows - x86)
; CursorPosition = 1057 ; CursorPosition = 1048
; FirstLine = 1008 ; FirstLine = 1008
; Folding = -- ; Folding = --
; EnableUnicode ; EnableUnicode

268
osm.pb
View File

@@ -16,6 +16,7 @@ CompilerEndIf
EnableExplicit EnableExplicit
InitNetwork() InitNetwork()
OpenConsole()
UsePNGImageDecoder() UsePNGImageDecoder()
UsePNGImageEncoder() UsePNGImageEncoder()
@@ -124,7 +125,7 @@ Module OSM
Moving.i Moving.i
Dirty.i ;To signal that drawing need a refresh Dirty.i ;To signal that drawing need a refresh
CurlMutex.i ;CurlMutex.i ;seems that I can't thread curl ! :((((( ;CurlMutex.i ;CurlMutex.i ;seems that I can't thread curl ! :(((((
MainDrawingThread.i MainDrawingThread.i
List TilesThreads.TileThread() List TilesThreads.TileThread()
@@ -142,55 +143,140 @@ Module OSM
;- *** CURL specific *** ;- *** CURL specific ***
Global *ReceiveHTTPToMemoryBuffer, ReceiveHTTPToMemoryBufferPtr.i, ReceivedData.s ; Global *ReceiveHTTPToMemoryBuffer, ReceiveHTTPToMemoryBufferPtr.i, ReceivedData.s
IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl
;
; ProcedureC ReceiveHTTPWriteToMemoryFunction(*ptr, Size.i, NMemB.i, *Stream)
;
; Protected SizeProper.i = Size & 255
; Protected NMemBProper.i = NMemB
;
; If *ReceiveHTTPToMemoryBuffer = 0
; *ReceiveHTTPToMemoryBuffer = AllocateMemory(SizeProper * NMemBProper)
; If *ReceiveHTTPToMemoryBuffer = 0
; Debug "Problem allocating memory"
; End
; EndIf
; Else
; *ReceiveHTTPToMemoryBuffer = ReAllocateMemory(*ReceiveHTTPToMemoryBuffer, MemorySize(*ReceiveHTTPToMemoryBuffer) + SizeProper * NMemBProper)
; If *ReceiveHTTPToMemoryBuffer = 0
; Debug "Problem reallocating memory"
; End
; EndIf
; EndIf
;
; CopyMemory(*ptr, *ReceiveHTTPToMemoryBuffer + ReceiveHTTPToMemoryBufferPtr, SizeProper * NMemBProper)
; ReceiveHTTPToMemoryBufferPtr + SizeProper * NMemBProper
;
; ProcedureReturn SizeProper * NMemBProper
;
; EndProcedure
;
; Procedure.i CurlReceiveHTTPToMemory(URL$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="")
;
; Protected *Buffer, curl.i, Timeout.i, res.i
;
; ;Debug "ReceiveHTTPToMemory" + URL$ + ProxyURL$ + ProxyPort$ + ProxyUser$ + ProxyPassword$
;
; If Len(URL$)
;
; curl = curl_easy_init()
;
; If curl
;
; Timeout = 3
;
; curl_easy_setopt(curl, #CURLOPT_URL, str2curl(URL$))
; curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
; curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
; curl_easy_setopt(curl, #CURLOPT_HEADER, 0)
; curl_easy_setopt(curl, #CURLOPT_FOLLOWLOCATION, 1)
; curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout)
;
; If Len(ProxyURL$)
; ;curl_easy_setopt(curl, #CURLOPT_HTTPPROXYTUNNEL, #True)
; If Len(ProxyPort$)
; ProxyURL$ + ":" + ProxyPort$
; EndIf
; ; Debug ProxyURL$
; curl_easy_setopt(curl, #CURLOPT_PROXY, str2curl(ProxyURL$))
; If Len(ProxyUser$)
; If Len(ProxyPassword$)
; ProxyUser$ + ":" + ProxyPassword$
; EndIf
; ;Debug ProxyUser$
; curl_easy_setopt(curl, #CURLOPT_PROXYUSERPWD, str2curl(ProxyUser$))
; EndIf
; EndIf
;
; ;TODO improve with https://github.com/Progi1984/RWrappers/tree/master/LibCurl
; curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToMemoryFunction())
; LockMutex(OSM\CurlMutex)
; res = curl_easy_perform(curl)
; UnlockMutex(OSM\CurlMutex)
;
; If res = #CURLE_OK
;
; *Buffer = AllocateMemory(ReceiveHTTPToMemoryBufferPtr)
; If *Buffer
; CopyMemory(*ReceiveHTTPToMemoryBuffer, *Buffer, ReceiveHTTPToMemoryBufferPtr)
; FreeMemory(*ReceiveHTTPToMemoryBuffer)
; *ReceiveHTTPToMemoryBuffer = #Null
; ReceiveHTTPToMemoryBufferPtr = 0
; Else
; ; Debug "Problem allocating buffer"
; EndIf
; ;curl_easy_cleanup(curl) ;Was its original place but moved below as it seems more logical to me.
; Else
; ; Debug "CURL NOT OK"
; EndIf
;
; curl_easy_cleanup(curl)
;
; Else
; ; Debug "Can't Init CURL"
; EndIf
;
; EndIf
;
; ; Debug "Curl Buffer : " + Str(*Buffer)
;
; ProcedureReturn *Buffer
;
; EndProcedure
ProcedureC ReceiveHTTPWriteToMemoryFunction(*ptr, Size.i, NMemB.i, *Stream) ProcedureC ReceiveHTTPWriteToFileFunction(*ptr, Size.i, NMemB.i, FileHandle.i)
Protected SizeProper.i = Size & 255 ProcedureReturn WriteData(FileHandle, *ptr, Size * NMemB)
Protected NMemBProper.i = NMemB
If *ReceiveHTTPToMemoryBuffer = 0
*ReceiveHTTPToMemoryBuffer = AllocateMemory(SizeProper * NMemBProper)
If *ReceiveHTTPToMemoryBuffer = 0
Debug "Problem allocating memory"
End
EndIf
Else
*ReceiveHTTPToMemoryBuffer = ReAllocateMemory(*ReceiveHTTPToMemoryBuffer, MemorySize(*ReceiveHTTPToMemoryBuffer) + SizeProper * NMemBProper)
If *ReceiveHTTPToMemoryBuffer = 0
Debug "Problem reallocating memory"
End
EndIf
EndIf
CopyMemory(*ptr, *ReceiveHTTPToMemoryBuffer + ReceiveHTTPToMemoryBufferPtr, SizeProper * NMemBProper)
ReceiveHTTPToMemoryBufferPtr + SizeProper * NMemBProper
ProcedureReturn SizeProper * NMemBProper
EndProcedure EndProcedure
Procedure.i CurlReceiveHTTPToMemory(URL$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="") Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="")
Protected *Buffer, curl.i, Timeout.i, res.i Protected *Buffer, curl.i, Timeout.i, res.i
Protected FileHandle.i
;Debug "ReceiveHTTPToMemory" + URL$ + ProxyURL$ + ProxyPort$ + ProxyUser$ + ProxyPassword$ Debug "ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$
Debug " to file : " + DestFileName$
If Len(URL$) FileHandle = CreateFile(#PB_Any, DestFileName$)
If FileHandle And Len(URL$)
curl = curl_easy_init() curl = curl_easy_init()
If curl If curl
Timeout = 3 Timeout = 120
curl_easy_setopt(curl, #CURLOPT_URL, str2curl(URL$)) curl_easy_setopt(curl, #CURLOPT_URL, str2curl(URL$))
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0) curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0) curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
curl_easy_setopt(curl, #CURLOPT_HEADER, 0) curl_easy_setopt(curl, #CURLOPT_HEADER, 0)
curl_easy_setopt(curl, #CURLOPT_FOLLOWLOCATION, 1) curl_easy_setopt(curl, #CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout) curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout)
curl_easy_setopt(curl, #CURLOPT_VERBOSE, 1)
curl_easy_setopt(curl, #CURLOPT_CONNECTTIMEOUT, 0)
If Len(ProxyURL$) If Len(ProxyURL$)
;curl_easy_setopt(curl, #CURLOPT_HTTPPROXYTUNNEL, #True) ;curl_easy_setopt(curl, #CURLOPT_HTTPPROXYTUNNEL, #True)
@@ -208,41 +294,30 @@ Module OSM
EndIf EndIf
EndIf EndIf
;TODO improve with https://github.com/Progi1984/RWrappers/tree/master/LibCurl curl_easy_setopt(curl, #CURLOPT_WRITEDATA, FileHandle)
curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToMemoryFunction()) curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction())
LockMutex(OSM\CurlMutex) ;LockMutex(OSM\CurlMutex) ;Not any more necessary
res = curl_easy_perform(curl) res = curl_easy_perform(curl)
UnlockMutex(OSM\CurlMutex) ;UnlockMutex(OSM\CurlMutex)
If res <> #CURLE_OK
If res = #CURLE_OK Debug "CURL NOT OK"
*Buffer = AllocateMemory(ReceiveHTTPToMemoryBufferPtr)
If *Buffer
CopyMemory(*ReceiveHTTPToMemoryBuffer, *Buffer, ReceiveHTTPToMemoryBufferPtr)
FreeMemory(*ReceiveHTTPToMemoryBuffer)
*ReceiveHTTPToMemoryBuffer = #Null
ReceiveHTTPToMemoryBufferPtr = 0
Else
; Debug "Problem allocating buffer"
EndIf
;curl_easy_cleanup(curl) ;Was its original place but moved below as it seems more logical to me.
Else
; Debug "CURL NOT OK"
EndIf EndIf
curl_easy_cleanup(curl) curl_easy_cleanup(curl)
Else Else
; Debug "Can't Init CURL" Debug "Can't Init CURL"
EndIf EndIf
CloseFile(FileHandle)
ProcedureReturn FileSize(DestFileName$)
EndIf EndIf
; Debug "Curl Buffer : " + Str(*Buffer) ProcedureReturn #False
ProcedureReturn *Buffer
EndProcedure EndProcedure
;- *** ;- ***
Procedure InitOSM() Procedure InitOSM()
@@ -255,7 +330,7 @@ Module OSM
OSM\ZoomMax = 18 OSM\ZoomMax = 18
OSM\MoveStartingPoint\x = - 1 OSM\MoveStartingPoint\x = - 1
OSM\TileSize = 256 OSM\TileSize = 256
OSM\CurlMutex = CreateMutex() ;OSM\CurlMutex = CreateMutex()
OSM\Dirty = #False OSM\Dirty = #False
OSM\Drawing\Mutex = CreateMutex() OSM\Drawing\Mutex = CreateMutex()
OSM\Drawing\Semaphore = CreateSemaphore() OSM\Drawing\Semaphore = CreateSemaphore()
@@ -384,7 +459,7 @@ Module OSM
Protected mapWidth.l = Pow(2,OSM\Zoom+8) Protected mapWidth.l = Pow(2,OSM\Zoom+8)
Protected mapHeight.l = Pow(2,OSM\Zoom+8) Protected mapHeight.l = Pow(2,OSM\Zoom+8)
Protected x1.l,y1.l Protected x1.l,y1.l
; get x value ; get x value
x1 = (*Location\Longitude+180)*(mapWidth/360) x1 = (*Location\Longitude+180)*(mapWidth/360)
; convert from degrees To radians ; convert from degrees To radians
@@ -452,61 +527,64 @@ Module OSM
EndProcedure EndProcedure
Procedure.i GetTileFromHDD(Zoom.i, XTile.i, YTile.i) Procedure.i GetTileFromHDD(CacheFile.s)
Protected nImage.i Protected nImage.i
Protected CacheFile.s = "OSM_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png"
If FileSize(CacheFile) > 0
; Debug "Check if we have this image on HDD"
If FileSize(OSM\HDDCachePath + cacheFile) > 0
nImage = LoadImage(#PB_Any, OSM\HDDCachePath + CacheFile) nImage = LoadImage(#PB_Any, CacheFile)
If IsImage(nImage) If IsImage(nImage)
; Debug "Load from HDD Tile " + CacheFile Debug "Loadimage " + CacheFile + " -> Success !"
ProcedureReturn nImage ProcedureReturn nImage
EndIf EndIf
EndIf EndIf
Debug "Loadimage " + CacheFile + " -> Failed !"
ProcedureReturn -1 ProcedureReturn -1
EndProcedure EndProcedure
Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i) Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s)
Protected *Buffer Protected *Buffer
Protected nImage.i = -1 Protected nImage.i = -1
Protected FileHandle.i
Protected TileURL.s = OSM\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" Protected TileURL.s = OSM\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png"
Protected CacheFile.s = "OSM_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png" ; Protected CacheFile.s = "OSM_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png"
Debug "Check if we have this image on Web" Debug "Check if we have this image on Web"
If Proxy If Proxy
;LockMutex(OSM\CurlMutex) ;*Buffer = CurlReceiveHTTPToMemory(TileURL, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$)
*Buffer = CurlReceiveHTTPToMemory(TileURL, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$)
;UnlockMutex(OSM\CurlMutex) If FileHandle
Else nImage = GetTileFromHDD(CacheFile)
*Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous
EndIf
Debug "Image buffer " + Str(*Buffer)
If *Buffer
nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer))
If IsImage(nImage)
Debug "Load from web " + TileURL + " as Tile nb " + nImage
SaveImage(nImage, OSM\HDDCachePath + CacheFile, #PB_ImagePlugin_PNG)
FreeMemory(*Buffer)
Else Else
Debug "Can't catch image " + TileURL Debug "File " + TileURL + " not correctly received with Curl and proxy"
nImage = -1
;ShowMemoryViewer(*Buffer, MemorySize(*Buffer))
EndIf EndIf
Else Else
Debug "Problem loading from web " + TileURL *Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous
EndIf
If *Buffer
nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer))
If IsImage(nImage)
Debug "Load from web " + TileURL + " as Tile nb " + nImage
SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG)
FreeMemory(*Buffer)
Else
Debug "Can't catch image " + TileURL
nImage = -1
;ShowMemoryViewer(*Buffer, MemorySize(*Buffer))
EndIf
Else
Debug "ReceiveHTTPMemory's buffer is empty"
EndIf
EndIf
ProcedureReturn nImage ProcedureReturn nImage
@@ -516,12 +594,13 @@ Module OSM
Protected nImage.i = -1 Protected nImage.i = -1
Protected key.s = "Z" + RSet(Str(*Tile\OSMZoom), 4, "0") + "X" + RSet(Str(*Tile\OSMTileX), 8, "0") + "Y" + RSet(Str(*Tile\OSMTileY), 8, "0") Protected key.s = "Z" + RSet(Str(*Tile\OSMZoom), 4, "0") + "X" + RSet(Str(*Tile\OSMTileX), 8, "0") + "Y" + RSet(Str(*Tile\OSMTileY), 8, "0")
Protected CacheFile.s = OSM\HDDCachePath + "OSM_" + Str(*Tile\OSMZoom) + "_" + Str(*Tile\OSMTileX) + "_" + Str(*Tile\OSMTileY) + ".png"
;Adding the image to the cache if possible ;Adding the image to the cache if possible
AddMapElement(OSM\MemCache\Images(), key) AddMapElement(OSM\MemCache\Images(), key)
nImage = GetTileFromHDD(*Tile\OSMZoom, *Tile\OSMTileX, *Tile\OSMTileY) nImage = GetTileFromHDD(CacheFile)
If nImage = -1 If nImage = -1
nImage = GetTileFromWeb(*Tile\OSMZoom, *Tile\OSMTileX, *Tile\OSMTileY) nImage = GetTileFromWeb(*Tile\OSMZoom, *Tile\OSMTileX, *Tile\OSMTileY, CacheFile)
EndIf EndIf
If nImage <> -1 If nImage <> -1
OSM\MemCache\Images(key)\nImage = nImage OSM\MemCache\Images(key)\nImage = nImage
@@ -712,7 +791,7 @@ Module OSM
EndIf EndIf
EndIf EndIf
Next Next
EndProcedure EndProcedure
Procedure DrawingThread(*SharedDrawing.DrawingParameters) Procedure DrawingThread(*SharedDrawing.DrawingParameters)
@@ -1090,8 +1169,9 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf CompilerEndIf
; IDE Options = PureBasic 5.42 LTS (Windows - x86) ; IDE Options = PureBasic 5.42 LTS (Windows - x86)
; CursorPosition = 211 ; ExecutableFormat = Console
; FirstLine = 188 ; CursorPosition = 146
; FirstLine = 117
; Folding = ------- ; Folding = -------
; EnableUnicode ; EnableUnicode
; EnableThread ; EnableThread