# HG changeset patch # User Mike Pavone # Date 1290381705 18000 # Node ID 65ebd1ce261187e6bf202529cbde909a1d35b768 # Parent ff00538cd818e067747f7f1a19b31a4a5f839cfc Add Read method to TCP connection diff -r ff00538cd818 -r 65ebd1ce2611 array.rhope --- a/array.rhope Sun Nov 21 17:10:49 2010 -0500 +++ b/array.rhope Sun Nov 21 18:21:45 2010 -0500 @@ -222,6 +222,32 @@ } } +Slice@Array[array,slicepoint:left,right] +{ + If[[slicepoint]<=[0]] + { + left <- Array[] + right <- array + }{ + arrlen <- [array]Length >> + If[[slicepoint]>=[arrlen]] + { + left <- array + right <- Array[] + }{ + left <- [_internal_array_copychunk[array, 0, _internal_array_allocnaked[slicepoint,[array]Eltype >>], 0, slicepoint]]Length <<[slicepoint] + rightlen <- [arrlen]-[slicepoint] + right <- [_internal_array_copychunk[array, slicepoint, _internal_array_allocnaked[rightlen,[array]Eltype >>], 0, rightlen]]Length <<[rightlen] + } + } +} + +Slice@Empty Array[array,slicepoint:left,right] +{ + left <- array + right <- array +} + Length@Empty Array[arr:out] { out <- 0 diff -r ff00538cd818 -r 65ebd1ce2611 net.rhope --- a/net.rhope Sun Nov 21 17:10:49 2010 -0500 +++ b/net.rhope Sun Nov 21 18:21:45 2010 -0500 @@ -247,15 +247,13 @@ { Filedes Read Buffer - Read Offset } TCP Connection[fd:out] { - out <- [[[Build[TCP Connection()] + out <- [[Build[TCP Connection()] ]Filedes <<[fd] ]Read Buffer <<[Array[]] - ]Read Offset <<[0] } _Write@TCP Connection[con,buffer,wrote:out,err] @@ -264,7 +262,7 @@ new wrote <- write[[con]Filedes >>, buffer, Int64[[buffer]Length]] If[[new wrote]=[-1]] { - If[Wait for IO[[con]Filedes >>, 2]] + If[Wait for IO[[con]Filedes >>, 4]] { out,err <- [con]_Write[buffer,wrote] }{ @@ -292,6 +290,80 @@ } } +_Read@TCP Connection[con,toread,buflist:data,out con,err] +{ + read[[con]Filedes >>, _internal_array_allocnaked[toread, UInt8()], Int64[toread]] + { numread <- Trunc Int32[~] } + { outbuf <- [~]Length <<[numread] } + If[[numread]=[-1]] + { + If[Wait for IO[[con]Filedes >>, 1]] + { + data,out con,err <- [con]_Read[toread,buflist] + }{ + err <- Fold[_Add Len[?], 0, buflist] + } + }{ + If[[numread]=[toread]] + { + data <- [buflist]Append[outbuf] + out con <- con + }{ + data,out con,err <- [con]_Read[[toread]-[numread], [buflist]Append[outbuf]] + } + } +} + +_Add Len[len,buf:out] +{ + out <- [len]+[[buf]Length] +} + +_Merge One Buf[inprog,src:out] +{ + oldlen <- [inprog]Length + srclen <- [src]Length + out <- [_internal_array_copychunk[src, 0, inprog, oldlen, srclen]]Length <<[[oldlen]+[srclen]] +} + +_Merge Buffers[buflist:out] +{ + If[[[buflist]Length]=[1]] + { + out <- [buflist]Index[0] + }{ + len <- Fold[_Add Len[?], 0, buflist] + out <- Fold[_Merge One Buf[?], _internal_array_allocnaked[len, [[buflist]Index[0]]Eltype >>], buflist] + } +} + +Read@TCP Connection[con,toread(Int32):data,out con,err] +{ + buflen <- [[con]Read Buffer >>]Length + If[[toread]=[buflen]] + { + data <- [con]Read Buffer >> + out con <- [con]Read Buffer <<[Array[]] + }{ + If[[toread]<[buflen]] + { + data <- [[con]Read Buffer >>]Slice[toread] {} + { out con <- [con]Read Buffer <<[~] } + }{ + If[buflen] + { + buflist <- [()]Append[[con]Read Buffer >>] + ntoread <- [toread]-[[[con]Read Buffer >>]Length] + }{ + ntoread <- Val[toread] + buflist <- () + } + ,out con,err <- [[con]Read Buffer <<[Array[]]]_Read[ntoread,buflist] + { data <- _Merge Buffers[~] } + } + } +} + Close@TCP Connection[con:out] { out <- close[[con]Filedes >>]