def connectionStreamCB(uv_server, status): status = intmask(status) # If the connection failed to complete, then whatever; we're a server, not # a client, and this is a pretty boring do-nothing failure mode. # XXX we *really* should have some way to report failures, though; right? if status < 0: return try: with ruv.unstashingStream(uv_server) as (vat, handler): uv_client = ruv.rffi.cast(ruv.stream_tp, ruv.alloc_tcp(vat.uv_loop)) # Actually accept the connection. ruv.accept(uv_server, uv_client) # Incant the handler. from typhon.objects.collections.maps import EMPTY_MAP wrappedStream = ruv.wrapStream(uv_client, 2) vat.sendOnly(handler, RUN_2, [ StreamSource(wrappedStream, vat), StreamSink(wrappedStream, vat) ], EMPTY_MAP) except: if not we_are_translated(): raise
def listen(self, handler): vat = currentVat.get() uv_server = ruv.alloc_tcp(vat.uv_loop) try: ruv.tcpBind(uv_server, "0.0.0.0", self.port) except ruv.UVError as uve: raise userError(u"makeTCP4ServerEndpoint: Couldn't listen: %s" % uve.repr().decode("utf-8")) uv_stream = ruv.rffi.cast(ruv.stream_tp, uv_server) ruv.stashStream(uv_stream, (vat, handler)) # XXX hardcoded backlog of 42 ruv.listen(uv_stream, 42, connectionCB) return TCP4Server(uv_server)
def listenStream(self, handler): vat = currentVat.get() uv_server = ruv.alloc_tcp(vat.uv_loop) try: ruv.tcpBind(uv_server, "0.0.0.0", self.port) except ruv.UVError as uve: raise userError(u"listenStream/1: Couldn't listen: %s" % uve.repr().decode("utf-8")) uv_stream = ruv.rffi.cast(ruv.stream_tp, uv_server) ruv.stashStream(uv_stream, (vat, handler)) # XXX hardcoded backlog of 42 ruv.listen(uv_stream, 42, connectionStreamCB) return TCP4Server(uv_server)
def connect(self): vat = currentVat.get() stream = ruv.alloc_tcp(vat.uv_loop) fount, fountResolver = makePromise() drain, drainResolver = makePromise() # Ugh, the hax. resolvers = ConstList([fountResolver, drainResolver]) ruv.stashStream(ruv.rffi.cast(ruv.stream_tp, stream), (vat, resolvers)) # Make the actual connection. ruv.tcpConnect(stream, self.host, self.port, connectCB) # Return the promises. return ConstList([fount, drain])
def run(self): uv_server = ruv.alloc_tcp(self.vat.uv_loop) try: if self.inet_type == 4: ruv.tcp4Bind(uv_server, "0.0.0.0", self.port) elif self.inet_type == 6: ruv.tcp6Bind(uv_server, "::", self.port) except ruv.UVError as uve: raise userError(u"listenStream/1: Couldn't listen: %s" % uve.repr().decode("utf-8")) uv_stream = ruv.rffi.cast(ruv.stream_tp, uv_server) ruv.stashStream(uv_stream, (self.vat, self.handler)) # XXX hardcoded backlog of 42 ruv.listen(uv_stream, 42, connectionStreamCB) self.r.resolve(TCPServer(uv_server))
def connectStream(self): vat = currentVat.get() stream = ruv.alloc_tcp(vat.uv_loop) source, sourceResolver = makePromise() sink, sinkResolver = makePromise() # Ugh, the hax. resolvers = wrapList([sourceResolver, sinkResolver]) ruv.stashStream(ruv.rffi.cast(ruv.stream_tp, stream), (vat, resolvers)) # Make the actual connection. ruv.tcpConnect(stream, self.host, self.port, connectStreamCB) # Return the promises. return [source, sink]
def connect(self): vat = currentVat.get() stream = ruv.alloc_tcp(vat.uv_loop) fount, fountResolver = makePromise() drain, drainResolver = makePromise() # Ugh, the hax. resolvers = wrapList([fountResolver, drainResolver]) ruv.stashStream(ruv.rffi.cast(ruv.stream_tp, stream), (vat, resolvers)) # Make the actual connection. ruv.tcpConnect(stream, self.host, self.port, connectCB) # Return the promises. return [fount, drain]
def listenStream(self, handler): vat = currentVat.get() uv_server = ruv.alloc_tcp(vat.uv_loop) try: if self.inet_type == 4: ruv.tcp4Bind(uv_server, "0.0.0.0", self.port) elif self.inet_type == 6: ruv.tcp6Bind(uv_server, "::", self.port) except ruv.UVError as uve: raise userError(u"listenStream/1: Couldn't listen: %s" % uve.repr().decode("utf-8")) uv_stream = ruv.rffi.cast(ruv.stream_tp, uv_server) ruv.stashStream(uv_stream, (vat, handler)) # XXX hardcoded backlog of 42 ruv.listen(uv_stream, 42, connectionStreamCB) return TCPServer(uv_server)
def connectStream(self): """ Connect this endpoint, returning a `[source, sink]` pair.of vows. """ vat = currentVat.get() stream = ruv.alloc_tcp(vat.uv_loop) source, sourceResolver = makePromise() sink, sinkResolver = makePromise() # Ugh, the hax. resolvers = wrapList([sourceResolver, sinkResolver]) ruv.stashStream(ruv.rffi.cast(ruv.stream_tp, stream), (vat, resolvers)) vat.enqueueEvent(ConnectStreamIOEvent( vat, stream, self.host, self.port, self.inet_type)) # Return the promises. return [source, sink]
def connectionCB(uv_server, status): status = intmask(status) # If the connection failed to complete, then whatever; we're a server, not # a client, and this is a pretty boring do-nothing failure mode. # XXX we *really* should have some way to report failures, though; right? if status < 0: return try: with ruv.unstashingStream(uv_server) as (vat, handler): uv_client = ruv.rffi.cast(ruv.stream_tp, ruv.alloc_tcp(vat.uv_loop)) # Actually accept the connection. ruv.accept(uv_server, uv_client) # Incant the handler. from typhon.objects.collections.maps import EMPTY_MAP vat.sendOnly(handler, RUN_2, [StreamFount(uv_client, vat), StreamDrain(uv_client, vat)], EMPTY_MAP) except: if not we_are_translated(): raise
def connectStream(self): """ Connect this endpoint, returning a `[source, sink]` pair.of vows. """ vat = currentVat.get() stream = ruv.alloc_tcp(vat.uv_loop) source, sourceResolver = makePromise() sink, sinkResolver = makePromise() # Ugh, the hax. resolvers = wrapList([sourceResolver, sinkResolver]) ruv.stashStream(ruv.rffi.cast(ruv.stream_tp, stream), (vat, resolvers)) # Make the actual connection. if self.inet_type == 4: ruv.tcp4Connect(stream, self.host, self.port, connectStreamCB) elif self.inet_type == 6: ruv.tcp6Connect(stream, self.host, self.port, connectStreamCB) # Return the promises. return [source, sink]