diff modules/socket.tp @ 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 7db37f040a6f
children 57d78a0af132
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
+			}
 		}
 	}