# HG changeset patch # User Mike Pavone # Date 1290377449 18000 # Node ID ff00538cd818e067747f7f1a19b31a4a5f839cfc # Parent 7bbdc034e347eab6af39868edd419907b81cbaa4 Add TCP Connection type and implement Write and Close methods diff -r 7bbdc034e347 -r ff00538cd818 net.rhope --- a/net.rhope Sun Nov 21 16:33:17 2010 -0500 +++ b/net.rhope Sun Nov 21 17:10:49 2010 -0500 @@ -223,10 +223,9 @@ Print["Error waiting for connection"] } }{ - Call Async[_Do Con Call[con,address,tocall,?]] - { - out <- _Port Wait[fd,tocall] - } + fcntl[con, 4i32, 2048i64] + { Call Async[_Do Con Call[TCP Connection[con],address,tocall,?]] + { out <- _Port Wait[fd,tocall] }} } } @@ -244,6 +243,60 @@ } } +Blueprint TCP Connection +{ + Filedes + Read Buffer + Read Offset +} + +TCP Connection[fd:out] +{ + out <- [[[Build[TCP Connection()] + ]Filedes <<[fd] + ]Read Buffer <<[Array[]] + ]Read Offset <<[0] +} + +_Write@TCP Connection[con,buffer,wrote:out,err] +{ + + new wrote <- write[[con]Filedes >>, buffer, Int64[[buffer]Length]] + If[[new wrote]=[-1]] + { + If[Wait for IO[[con]Filedes >>, 2]] + { + out,err <- [con]_Write[buffer,wrote] + }{ + err <- wrote + } + }{ + If[[new wrote]=[[buffer]Length]] + { + out <- con + }{ + remaining <- [[buffer]Length]-[new wrote] + next buf <- [_internal_array_copychunk[buffer, new wrote, _internal_array_allocnaked[remaining,UInt8()], 0, remaining]]Length <<[remaining] + out,err <- [con]_Write[next buf,[wrote]+[new wrote]] + } + } +} + +Write@TCP Connection[con,buffer(Array):out,err] +{ + If[[buffer]Length] + { + out,err <- [con]_Write[buffer,0] + }{ + out <- con + } +} + +Close@TCP Connection[con:out] +{ + out <- close[[con]Filedes >>] +} + //This effectively leaks a context and thus any data on the stack of that context //Need either handle cleanup of contexts or find a better way to accomplish this Wait Forever[]