@@ -5,7 +5,6 @@ import scala.scalanative.libc.stdlib._
55import scala .scalanative .libc .string ._
66import scala .concurrent ._
77import scala .scalanative .runtime .Boxes
8- import scala .scalanative .runtime .Intrinsics
98
109case class ResponseState (
1110 var code : Int = 200 ,
@@ -16,14 +15,11 @@ case class ResponseState(
1615object Curl {
1716 import LibCurl ._
1817 import LibCurlConstants ._
19- import LibUV ._
2018 import LibUVConstants ._
2119
2220 var serial = 0L
2321
24- val loop = EventLoop .loop
25- var multi : MultiCurl = null
26- val timerHandle : TimerHandle = malloc(uv_handle_size(UV_TIMER_T ))
22+ var multi : MultiCurl = null
2723
2824 val requestPromises = mutable.Map [Long , Promise [ResponseState ]]()
2925 val requests = mutable.Map [Long , ResponseState ]()
@@ -44,7 +40,6 @@ object Curl {
4440 multi_setopt_ptr(multi, TIMERFUNCTION , func_to_ptr(startTimerCB))
4541 println(s " timerCB: $startTimerCB" )
4642
47- check(uv_timer_init(loop, timerHandle), " uv_timer_init" )
4843 initialized = true
4944 println(" done" )
5045 }
@@ -164,60 +159,52 @@ object Curl {
164159 val socketCB = new CurlSocketCallback {
165160 def apply (
166161 curl : Curl ,
167- socket : Ptr [ Byte ] ,
162+ socket : Int ,
168163 action : Int ,
169164 data : Ptr [Byte ],
170165 socket_data : Ptr [Byte ]
171166 ): Int = {
172167 println(s " socketCB called with action ${action}" )
173168 val pollHandle = if (socket_data == null ) {
174169 println(s " initializing handle for socket ${socket}" )
175- val buf = malloc(uv_handle_size(UV_POLL_T )).asInstanceOf [Ptr [Ptr [Byte ]]]
176- ! buf = socket
177- check(uv_poll_init_socket(loop, buf, socket), " uv_poll_init_socket" )
170+ val poll = Poll (socket)
178171 check(
179- multi_assign(multi, socket, buf. asInstanceOf [ Ptr [ Byte ]] ),
172+ multi_assign(multi, socket, poll.ptr ),
180173 " multi_assign"
181174 )
182- buf
175+ poll
183176 } else {
184- socket_data. asInstanceOf [ Ptr [ Ptr [ Byte ]]]
177+ new Poll ( socket_data)
185178 }
186179
187- val events = action match {
188- case POLL_NONE => None
189- case POLL_IN => Some (UV_READABLE )
190- case POLL_OUT => Some (UV_WRITABLE )
191- case POLL_INOUT => Some (UV_READABLE | UV_WRITABLE )
192- case POLL_REMOVE => None
193- }
180+ val readable = action == POLL_IN || action == POLL_INOUT
181+ val writable = action == POLL_OUT || action == POLL_INOUT
194182
195- events match {
196- case Some (ev) =>
197- println(s " starting poll with events $ev" )
198- uv_poll_start(pollHandle, ev, pollCB)
199- case None =>
200- println(" stopping poll" )
201- uv_poll_stop(pollHandle)
202- startTimerCB(multi, 1 , null )
183+ if (readable || writable) {
184+ println(
185+ s " starting poll with readable = $readable and writable = $writable"
186+ )
187+ pollHandle.start(readable, writable) { (status, readable, writable) =>
188+ println(
189+ s " ready_for_curl fired with status $status and readable = $readable writable = $writable"
190+ )
191+ var actions = 0
192+ if (readable) actions |= 1
193+ if (writable) actions |= 2
194+ val running_handles = stackalloc[Int ]
195+ val result =
196+ multi_socket_action(multi, socket, actions, running_handles)
197+ println(" multi_socket_action" , result)
198+ }
199+ } else {
200+ println(" stopping poll" )
201+ pollHandle.stop()
202+ startTimerCB(multi, 1 , null )
203203 }
204204 0
205205 }
206206 }
207207
208- val pollCB = new PollCB {
209- def apply (pollHandle : PollHandle , status : Int , events : Int ): Unit = {
210- println(
211- s " ready_for_curl fired with status ${status} and events ${events}"
212- )
213- val socket = ! (pollHandle.asInstanceOf [Ptr [Ptr [Byte ]]])
214- val actions = (events & 1 ) | (events & 2 ) // Whoa, nelly!
215- val running_handles = stackalloc[Int ]
216- val result = multi_socket_action(multi, socket, actions, running_handles)
217- println(" multi_socket_action" , result)
218- }
219- }
220-
221208 val startTimerCB = new CurlTimerCallback {
222209 def apply (curl : MultiCurl , timeout_ms : Long , data : Ptr [Byte ]): Int = {
223210 println(s " start_timer called with timeout ${timeout_ms} ms " )
@@ -226,23 +213,19 @@ object Curl {
226213 1
227214 } else timeout_ms
228215 println(" starting timer" )
229- check(uv_timer_start(timerHandle, timeoutCB, time, 0 ), " uv_timer_start" )
216+ Timer .timeout(time) { () =>
217+ println(" in timeout callback" )
218+ val running_handles = stackalloc[Int ]
219+ multi_socket_action(multi, - 1 , 0 , running_handles)
220+ println(s " on_timer fired, ${! running_handles} sockets running " )
221+ }
230222 println(" cleaning up requests" )
231223 cleanup_requests()
232224 println(" done" )
233225 0
234226 }
235227 }
236228
237- val timeoutCB = new TimerCB {
238- def apply (handle : TimerHandle ): Unit = {
239- println(" in timeout callback" )
240- val running_handles = stackalloc[Int ]
241- multi_socket_action(multi, int_to_ptr(- 1 ), 0 , running_handles)
242- println(s " on_timer fired, ${! running_handles} sockets running " )
243- }
244- }
245-
246229 def cleanup_requests (): Unit = {
247230 val messages = stackalloc[Int ]
248231 val privateDataPtr = stackalloc[Ptr [Long ]]
@@ -299,14 +282,6 @@ object Curl {
299282 Boxes .boxToPtr[Byte ](Boxes .unboxToCFuncRawPtr(f))
300283 }
301284
302- def int_to_ptr (i : Int ): Ptr [Byte ] = {
303- Boxes .boxToPtr[Byte ](Intrinsics .castIntToRawPtr(i))
304- }
305-
306- def long_to_ptr (l : Long ): Ptr [Byte ] = {
307- Boxes .boxToPtr[Byte ](Intrinsics .castLongToRawPtr(l))
308- }
309-
310285}
311286
312287object LibCurlConstants {
@@ -349,7 +324,7 @@ object LibCurlConstants {
349324
350325 type CurlDataCallback = CFuncPtr4 [Ptr [Byte ], CSize , CSize , Ptr [Byte ], CSize ]
351326 type CurlSocketCallback =
352- CFuncPtr5 [Curl , Ptr [ Byte ] , CInt , Ptr [Byte ], Ptr [Byte ], CInt ]
327+ CFuncPtr5 [Curl , CInt , CInt , Ptr [Byte ], Ptr [Byte ], CInt ]
353328 type CurlTimerCallback = CFuncPtr3 [MultiCurl , Long , Ptr [Byte ], CInt ]
354329
355330 @ name(" curl_global_init" )
@@ -408,14 +383,14 @@ object LibCurlConstants {
408383 @ name(" curl_multi_assign" )
409384 def multi_assign (
410385 multi : MultiCurl ,
411- socket : Ptr [ Byte ] ,
386+ socket : Int ,
412387 socket_data : Ptr [Byte ]
413388 ): Int = extern
414389
415390 @ name(" curl_multi_socket_action" )
416391 def multi_socket_action (
417392 multi : MultiCurl ,
418- socket : Ptr [ Byte ] ,
393+ socket : Int ,
419394 events : Int ,
420395 numhandles : Ptr [Int ]
421396 ): Int = extern
0 commit comments