changeset 145:7db37f040a6f

Basic socket support
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Aug 2013 04:15:53 -0700
parents 547153211389
children d8f92ebf1ff6
files modules/socket.tp samples/sock.tp
diffstat 2 files changed, 64 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/modules/socket.tp	Fri Aug 09 03:58:42 2013 -0700
+++ b/modules/socket.tp	Fri Aug 09 04:15:53 2013 -0700
@@ -1,4 +1,8 @@
 #{
+	includeSystemHeader: "sys/types.h"
+	includeSystemHeader: "sys/socket.h"
+	includeSystemHeader: "netdb.h"
+
 	llMessage: AF_INET withVars: {
 		intret <- obj_int32 ptr
 	} andCode: {
@@ -37,12 +41,7 @@
 		fd num!: (socket: (domain num) (type num) (protocol num))
 		fd
 	}
-	
-	llMessage: getaddrinfo withVars: {
-		
-	} andCode: {
-	}
-	
+
 	new <- :domain type protocol {
 		sfd <- socket: domain type protocol
 		#{
@@ -54,13 +53,61 @@
 				close: (sfd num)
 				self
 			}
+			llMessage: send:withFlags withVars: {
+				odata <- object ptr
+				flags <- obj_int32 ptr
+				sdata <- string ptr
+				sfd <- obj_int32 ptr
+				res <- obj_int32 ptr
+			} andCode: :odata :flags {
+				sdata <- mcall: string 1 odata
+				sfd <- mcall: fd 1 self
+				res <- make_object: (addr_of: obj_int32_meta) NULL 0
+				res num!: (send: (sfd num) (sdata data) (sdata bytes) (flags num))
+				res
+			}
+			send <- :data {
+				send: data withFlags: 0
+			}
 		}
 	}
-	
+
+	llMessage: _connectTo:onPort withVars: {
+		host <- string ptr
+		port <- string ptr
+		hints <- struct: addrinfo
+		info <- (struct: addrinfo) ptr
+		domain <- obj_int32 ptr
+		type <- obj_int32 ptr
+		protocol <- obj_int32 ptr
+		sock <- object ptr
+		sfd <- obj_int32 ptr
+	} andCode: :host :port {
+		memset: (addr_of: hints) 0 (sizeof: hints)
+		hints ai_family!: AF_UNSPEC
+		hints ai_socktype!: SOCK_STREAM
+		getaddrinfo: (host data) (port data) (addr_of: hints) (addr_of: info)
+
+		domain <- make_object: (addr_of: obj_int32_meta) NULL 0
+		domain num!: (info ai_family)
+		type <- make_object: (addr_of: obj_int32_meta) NULL 0
+		type num!: (info ai_socktype)
+		protocol <- make_object: (addr_of: obj_int32_meta) NULL 0
+		protocol num!: (info ai_protocol)
+
+		sock <- mcall: new 4 self domain type protocol
+		sfd <- mcall: fd 1 sock
+
+		connect: (sfd num) (info ai_addr) (info ai_addrlen)
+
+		freeaddrinfo: info
+		sock
+	}
+
 	connectTo:onPort <- :host :port {
-		
+		_connectTo: host onPort: (string: port)
 	}
-	listenOn: <- :port {
+	listenOn <- :port {
 	}
 	listenAt:onPort <- :host :port {
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/samples/sock.tp	Fri Aug 09 04:15:53 2013 -0700
@@ -0,0 +1,8 @@
+#{
+	main <- {
+		sock <- socket connectTo: "127.0.0.1" onPort: 12345
+		sock send: "Hello, Socket!\n"
+		sock close
+		0
+	}
+}