Mercurial > repos > tabletprog
comparison 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 |
comparison
equal
deleted
inserted
replaced
146:d8f92ebf1ff6 | 147:4c96a393103e |
---|---|
44 | 44 |
45 new <- :domain type protocol { | 45 new <- :domain type protocol { |
46 sfd <- socket: domain type protocol | 46 sfd <- socket: domain type protocol |
47 #{ | 47 #{ |
48 fd <- {sfd} | 48 fd <- {sfd} |
49 | |
49 llMessage: close withVars: { | 50 llMessage: close withVars: { |
50 sfd <- obj_int32 ptr | 51 sfd <- obj_int32 ptr |
51 } andCode: { | 52 } andCode: { |
52 sfd <- mcall: fd 1 self | 53 sfd <- mcall: fd 1 self |
53 close: (sfd num) | 54 close: (sfd num) |
54 self | 55 self |
55 } | 56 } |
57 | |
56 llMessage: send:withFlags withVars: { | 58 llMessage: send:withFlags withVars: { |
57 odata <- object ptr | 59 odata <- object ptr |
58 flags <- obj_int32 ptr | 60 flags <- obj_int32 ptr |
59 sdata <- string ptr | 61 sdata <- string ptr |
60 sfd <- obj_int32 ptr | 62 sfd <- obj_int32 ptr |
66 res num!: (send: (sfd num) (sdata data) (sdata bytes) (flags num)) | 68 res num!: (send: (sfd num) (sdata data) (sdata bytes) (flags num)) |
67 res | 69 res |
68 } | 70 } |
69 send <- :data { | 71 send <- :data { |
70 send: data withFlags: 0 | 72 send: data withFlags: 0 |
73 } | |
74 | |
75 llMessage: recv:withFlags withVars: { | |
76 length <- obj_int32 ptr | |
77 flags <- obj_int32 ptr | |
78 sfd <- obj_int32 ptr | |
79 res <- int | |
80 buf <- char ptr | |
81 out <- string ptr | |
82 } andCode: :length :flags { | |
83 sfd <- mcall: fd 1 self | |
84 buf <- GC_MALLOC_ATOMIC: (length num) + 1 | |
85 res <- recv: (sfd num) buf (length num) (flags num) | |
86 if: res < 0 { | |
87 length <- make_object: (addr_of: obj_int32_meta) NULL 0 | |
88 length num!: res | |
89 length | |
90 } else: { | |
91 out <- make_object: (addr_of: string_meta) NULL 0 | |
92 out bytes!: res | |
93 out len!: res | |
94 out data!: buf | |
95 out | |
96 } | |
97 } | |
98 recv <- :length { | |
99 recv: length withFlags: 0 | |
100 } | |
101 recvAll <- :len { | |
102 received <- "" | |
103 error <- false | |
104 while: { (not: error) && (received length) < len} do: { | |
105 res <- recv: (len - (received length)) | |
106 if: (res isInteger?) || (res length) = 0 { | |
107 error <- true | |
108 } else: { | |
109 received <- received . res | |
110 } | |
111 } | |
112 received | |
71 } | 113 } |
72 } | 114 } |
73 } | 115 } |
74 | 116 |
75 llMessage: _connectTo:onPort withVars: { | 117 llMessage: _connectTo:onPort withVars: { |