changeset 147:4c96a393103e

Add support for receiving data from a socket
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Aug 2013 04:57:21 -0700
parents d8f92ebf1ff6
children 5071d601fe70
files modules/socket.tp modules/string.tp samples/sock.tp
diffstat 3 files changed, 56 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/modules/socket.tp	Fri Aug 09 04:29:37 2013 -0700
+++ b/modules/socket.tp	Fri Aug 09 04:57:21 2013 -0700
@@ -46,6 +46,7 @@
 		sfd <- socket: domain type protocol
 		#{
 			fd <- {sfd}
+
 			llMessage: close withVars: {
 				sfd <- obj_int32 ptr
 			} andCode: {
@@ -53,6 +54,7 @@
 				close: (sfd num)
 				self
 			}
+
 			llMessage: send:withFlags withVars: {
 				odata <- object ptr
 				flags <- obj_int32 ptr
@@ -69,6 +71,46 @@
 			send <- :data {
 				send: data withFlags: 0
 			}
+
+			llMessage: recv:withFlags withVars: {
+				length <- obj_int32 ptr
+				flags <- obj_int32 ptr
+				sfd <- obj_int32 ptr
+				res <- int
+				buf <- char ptr
+				out <- string ptr
+			} andCode: :length :flags {
+				sfd <- mcall: fd 1 self
+				buf <- GC_MALLOC_ATOMIC: (length num) + 1
+				res <- recv: (sfd num) buf (length num) (flags num)
+				if: res < 0 {
+					length <- make_object: (addr_of: obj_int32_meta) NULL 0
+					length num!: res
+					length
+				} else: {
+					out <- make_object: (addr_of: string_meta) NULL 0
+					out bytes!: res
+					out len!: res
+					out data!: buf
+					out
+				}
+			}
+			recv <- :length {
+				recv: length withFlags: 0
+			}
+			recvAll <- :len {
+				received <- ""
+				error <- false
+				while: { (not: error) && (received length) < len} do: {
+					res <- recv: (len - (received length))
+					if: (res isInteger?) || (res length) = 0 {
+						error <- true
+					} else: {
+						received <- received . res
+					}
+				}
+				received
+			}
 		}
 	}
 
--- a/modules/string.tp	Fri Aug 09 04:29:37 2013 -0700
+++ b/modules/string.tp	Fri Aug 09 04:57:21 2013 -0700
@@ -2,7 +2,7 @@
 	llProperty: len withType: uint32_t
 	llProperty: bytes withType: uint32_t
 	llProperty: data withType: (char ptr)
-	
+
 	llMessage: length withVars: {
 		intret <- (obj_int32 ptr)
 	} andCode: {
@@ -10,7 +10,7 @@
 		intret num!: len
 		intret
 	}
-	
+
 	llMessage: byte_length withVars: {
 		intret <- (obj_int32 ptr)
 	} andCode: {
@@ -18,7 +18,7 @@
 		intret num!: bytes
 		intret
 	}
-	
+
 	llMessage: EQ_ withVars: {
 		argb <- (string ptr)
 	} andCode: :argb {
@@ -26,7 +26,7 @@
 			true
 		}
 	}
-	
+
 	llMessage: NEQ_ withVars: {
 		argb <- (string ptr)
 	} andCode: :argb {
@@ -34,16 +34,16 @@
 			true
 		}
 	}
-	
+
 	llMessage: print withVars: {} andCode: {
 		fwrite: data 1 bytes stdout
 		self
 	}
-	
+
 	llMessage: string withVars: {} andCode: {
 		self
 	}
-	
+
 	llMessage: CAT_ withVars: {
 		argbo <- (object ptr)
 		argb <- (string ptr)
@@ -58,7 +58,7 @@
 		memcpy: (out data) + bytes (argb data) (argb bytes) + 1
 		out
 	}
-	
+
 	llMessage: byte withVars: {
 		index <- (obj_int32 ptr)
 		intret <- (obj_int32 ptr)
@@ -67,7 +67,7 @@
 		intret num!: (if: (index num) < bytes { data get: (index num) } else: {0})
 		intret
 	}
-	
+
 	llMessage: int32 withVars: {
 		intret <- (obj_int32 ptr)
 	} andCode: {
@@ -75,7 +75,7 @@
 		intret num!: (atoi: data)
 		intret
 	}
-	
+
 	llMessage: hash withVars: {
 		intret <- (obj_int32 ptr)
 		i <- uint32_t
@@ -93,4 +93,6 @@
 		}
 		intret
 	}
+
+	isInteger? <- { false }
 }
--- a/samples/sock.tp	Fri Aug 09 04:29:37 2013 -0700
+++ b/samples/sock.tp	Fri Aug 09 04:57:21 2013 -0700
@@ -2,6 +2,8 @@
 	main <- {
 		sock <- socket connectTo: "127.0.0.1" onPort: 12345
 		sock send: "Hello, Socket!\n"
+		response <- sock recv: 1024
+		print: (string: response)
 		sock close
 		0
 	}