changeset 144:65ebd1ce2611

Add Read method to TCP connection
author Mike Pavone <pavone@retrodev.com>
date Sun, 21 Nov 2010 18:21:45 -0500
parents ff00538cd818
children 357f4ce3ca6d
files array.rhope net.rhope
diffstat 2 files changed, 102 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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 >>]