changeset 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 b75ec364cecc
children d86df83402f3
files net.rhope runtime/net.c
diffstat 2 files changed, 56 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/net.rhope	Sun Dec 05 18:04:19 2010 -0500
+++ b/net.rhope	Sun Dec 05 18:04:54 2010 -0500
@@ -18,6 +18,7 @@
 {
 	_internal_bindnewsocket[port(Int32,Naked),setreuse(Int32,Naked):socket(Int32,Naked)]
 	_internal_accept[sockfd(Int32,Naked),addrbuf(Array,Raw Pointer,Mutable),buflen(Int32,Naked):consock(Int32,Naked),addrbuf]
+	_internal_connectnewsocket[addr(Array,Raw Pointer),port(Int32,Naked):sockfd(Int32,Naked)]
 }
 
 Blueprint epoll_event
@@ -256,6 +257,17 @@
 		]Read Buffer <<[Array[]]
 }
 
+TCP Connect[address,port:out,err]
+{
+	addrbuf <- [[Flatten[address]]Buffer >>]Append[0u8]
+	fd <- _internal_connectnewsocket[addrbuf, port]
+	err <- If[[fd]=[-1]] {}
+	{
+		fcntl[fd, 4i32, 2048i64]
+		out <- TCP Connection[fd]
+	}
+}
+
 _Write@TCP Connection[con,buffer,wrote:out,err]
 {
 	
@@ -483,7 +495,6 @@
 					]Length <<[rbufferlen]
 				]
 			}{
-				
 				If[[[con]Read Buffer >>]Length]
 				{
 					nbuflist <- [buflist]Append[[con]Read Buffer >>]
@@ -492,6 +503,7 @@
 				}
 				ncon <- [con]Read Buffer <<[outbuf]
 			}{
+				npoffset <- -1
 				If[[[con]Read Buffer >>]Length]
 				{
 					nbuflist <- [[buflist]Append[[con]Read Buffer >>]]Append[outbuf]
@@ -540,10 +552,26 @@
 	out <- close[[con]Filedes >>]
 }
 
+Globals Forever
+{
+	Wait <- No
+}
+
+_Wait Forever[context:out] uses Forever
+{
+	Forever::Wait <- context
+	out <- Yes
+}
+
 //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[]
 {
-	Pause[Val[?]]
+	Pause[_Wait Forever[?]]
 }
 
+Stop Waiting[] uses Forever
+{
+	Resume[Forever::Wait, Yes]
+}
+
--- a/runtime/net.c	Sun Dec 05 18:04:19 2010 -0500
+++ b/runtime/net.c	Sun Dec 05 18:04:54 2010 -0500
@@ -37,7 +37,7 @@
 	int sockfd,flag=1;
 	struct addrinfo hints, *localaddr;
 	
-	snprintf(portstr, 6, "%d", port & 0XFFFF);
+	snprintf(portstr, 6, "%d", port & 0xFFFF);
 	
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_UNSPEC;
@@ -63,3 +63,28 @@
 	return sockfd;
 }
 
+int _internal_connectnewsocket(char * addr, int32_t port)
+{
+	char portstr[6];
+	struct addrinfo hints, *res;
+	int sock;
+	
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+	
+	snprintf(portstr, 6, "%d", port & 0xFFFF);
+	if(getaddrinfo(addr, portstr, &hints, &res))
+		return -1;
+	if(-1 == (sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)))
+		return -1;
+	
+	if(connect(sock, res->ai_addr, res->ai_addrlen))
+	{
+		close(sock);
+		sock = -1;
+	}
+	freeaddrinfo(res);	
+	return sock;
+}
+