comparison net.rhope @ 152:e9a8269384bb

Add support for opening a new TCP connection and fix bug in Read Delim
author Mike Pavone <pavone@retrodev.com>
date Sun, 05 Dec 2010 18:04:54 -0500
parents f3686f60985d
children 429afd920a23 47ab97730865
comparison
equal deleted inserted replaced
151:b75ec364cecc 152:e9a8269384bb
16 //Note: port numbers would more properly be UInt16, think about changing later 16 //Note: port numbers would more properly be UInt16, think about changing later
17 Foreign C:runtime 17 Foreign C:runtime
18 { 18 {
19 _internal_bindnewsocket[port(Int32,Naked),setreuse(Int32,Naked):socket(Int32,Naked)] 19 _internal_bindnewsocket[port(Int32,Naked),setreuse(Int32,Naked):socket(Int32,Naked)]
20 _internal_accept[sockfd(Int32,Naked),addrbuf(Array,Raw Pointer,Mutable),buflen(Int32,Naked):consock(Int32,Naked),addrbuf] 20 _internal_accept[sockfd(Int32,Naked),addrbuf(Array,Raw Pointer,Mutable),buflen(Int32,Naked):consock(Int32,Naked),addrbuf]
21 _internal_connectnewsocket[addr(Array,Raw Pointer),port(Int32,Naked):sockfd(Int32,Naked)]
21 } 22 }
22 23
23 Blueprint epoll_event 24 Blueprint epoll_event
24 { 25 {
25 events(UInt32,Naked) 26 events(UInt32,Naked)
252 TCP Connection[fd:out] 253 TCP Connection[fd:out]
253 { 254 {
254 out <- [[Build[TCP Connection()] 255 out <- [[Build[TCP Connection()]
255 ]Filedes <<[fd] 256 ]Filedes <<[fd]
256 ]Read Buffer <<[Array[]] 257 ]Read Buffer <<[Array[]]
258 }
259
260 TCP Connect[address,port:out,err]
261 {
262 addrbuf <- [[Flatten[address]]Buffer >>]Append[0u8]
263 fd <- _internal_connectnewsocket[addrbuf, port]
264 err <- If[[fd]=[-1]] {}
265 {
266 fcntl[fd, 4i32, 2048i64]
267 out <- TCP Connection[fd]
268 }
257 } 269 }
258 270
259 _Write@TCP Connection[con,buffer,wrote:out,err] 271 _Write@TCP Connection[con,buffer,wrote:out,err]
260 { 272 {
261 273
481 [_internal_array_copychunk[outbuf, [~]+[[delim]Length], 493 [_internal_array_copychunk[outbuf, [~]+[[delim]Length],
482 _internal_array_allocnaked[rbufferlen, UInt8()], 0, rbufferlen] 494 _internal_array_allocnaked[rbufferlen, UInt8()], 0, rbufferlen]
483 ]Length <<[rbufferlen] 495 ]Length <<[rbufferlen]
484 ] 496 ]
485 }{ 497 }{
486
487 If[[[con]Read Buffer >>]Length] 498 If[[[con]Read Buffer >>]Length]
488 { 499 {
489 nbuflist <- [buflist]Append[[con]Read Buffer >>] 500 nbuflist <- [buflist]Append[[con]Read Buffer >>]
490 }{ 501 }{
491 nbuflist <- Val[buflist] 502 nbuflist <- Val[buflist]
492 } 503 }
493 ncon <- [con]Read Buffer <<[outbuf] 504 ncon <- [con]Read Buffer <<[outbuf]
494 }{ 505 }{
506 npoffset <- -1
495 If[[[con]Read Buffer >>]Length] 507 If[[[con]Read Buffer >>]Length]
496 { 508 {
497 nbuflist <- [[buflist]Append[[con]Read Buffer >>]]Append[outbuf] 509 nbuflist <- [[buflist]Append[[con]Read Buffer >>]]Append[outbuf]
498 ncon <- [con]Read Buffer <<[Array[]] 510 ncon <- [con]Read Buffer <<[Array[]]
499 }{ 511 }{
538 Close@TCP Connection[con:out] 550 Close@TCP Connection[con:out]
539 { 551 {
540 out <- close[[con]Filedes >>] 552 out <- close[[con]Filedes >>]
541 } 553 }
542 554
555 Globals Forever
556 {
557 Wait <- No
558 }
559
560 _Wait Forever[context:out] uses Forever
561 {
562 Forever::Wait <- context
563 out <- Yes
564 }
565
543 //This effectively leaks a context and thus any data on the stack of that context 566 //This effectively leaks a context and thus any data on the stack of that context
544 //Need either handle cleanup of contexts or find a better way to accomplish this 567 //Need either handle cleanup of contexts or find a better way to accomplish this
545 Wait Forever[] 568 Wait Forever[]
546 { 569 {
547 Pause[Val[?]] 570 Pause[_Wait Forever[?]]
548 } 571 }
549 572
573 Stop Waiting[] uses Forever
574 {
575 Resume[Forever::Wait, Yes]
576 }
577