changeset 143:ff00538cd818

Add TCP Connection type and implement Write and Close methods
author Mike Pavone <pavone@retrodev.com>
date Sun, 21 Nov 2010 17:10:49 -0500
parents 7bbdc034e347
children 65ebd1ce2611
files net.rhope
diffstat 1 files changed, 57 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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[]