def SendRequest(self, reqId, q, arh, discarded=None, efs=None): """ Send a request onto a remote server for processing, and return immediately without blocking :param reqId: An unique request id within a service handler :param q: An instance of CScopeUQueue or CUQueue or None :param arh: A callback for tracking an instance of CAsyncResult containing an expected result :param discarded: A callback for tracking communication channel events, close and cancel :param efs: A callback for tracking an exception from server :return: True if communication channel is sendable, and False if communication channel is not sendable """ if reqId <= tagBaseRequestID.idReservedTwo: raise ValueError('Request id must be larger than 0x2001') delay = q if isinstance(q, CScopeUQueue): q = q.UQueue elif q is None: delay = CScopeUQueue() q = delay.UQueue elif not isinstance(q, CUQueue): raise ValueError('Bad input for parameter q') #http://stackoverflow.com/questions/21483482/efficient-way-to-convert-string-to-ctypes-c-ubyte-array-in-python bytes = (c_ubyte * q.GetSize()).from_buffer(q._m_bytes_, q._m_position_) h = self._m_ClientSocket_.Handle if h == 0: return False kv = None batching = False if arh or discarded or efs: kv = (reqId, CResultCb(arh, discarded, efs)) batching = ccl.IsBatching(h) with self._lock_Send_: with self._lock_: if batching: self._m_kvBatching_.append(kv) else: self._m_kvCallback_.append(kv) if ccl.SendRequest(h, reqId, bytes, q.GetSize()): return True else: if ccl.SendRequest(h, reqId, bytes, q.GetSize()): return True if kv: with self._lock_: if batching: self._m_kvBatching_.pop() else: self._m_kvCallback_.pop() return False
def SendRouteeResult(self, q, reqId=0): """ Send a result to the other routee :param q: An instance of CScopeUQueue or CUQueue or None :param reqId: An unique request id within a service handler :return: True if socket is connected, and False if socket is closed """ delay = q if isinstance(q, CScopeUQueue): q = q.UQueue elif q is None: delay = CScopeUQueue() q = delay.UQueue elif not isinstance(q, CUQueue): raise ValueError('Bad input for parameter q') if reqId == 0: reqId = self._m_ClientSocket_.CurrentRequestID h = self._m_ClientSocket_.Handle bytes = (c_ubyte * q.GetSize()).from_buffer(q._m_bytes_, q._m_position_) return ccl.SendRouteeResult(h, reqId, bytes, q.GetSize())
def arh(ar): # ar: an instance of CAsyncResult if f.done(): return sb = CScopeUQueue() sb.UQueue.Swap(ar.UQueue) f.set_result(sb)