Exemple #1
0
 def __getitem__(self, item):
     self._reqs[('GET', item)] = '__waiting'
     self.send(sterilizer.dirty(['GET', item]))
     start = time()
     while self._reqs[('GET', item)] == '__waiting':
         if self.timeout is not None:
             if time() - start > self.timeout:
                 raise TimeoutError("Server took to long to respond")
     return self._reqs[('GET', item)]
Exemple #2
0
 def run(self):
     log.info("Worker loop started, looking for data.")
     while not self.closed:
         try:
             data = self.conn.recv(self.server.buffer_size if self.server.
                                   buffer_size else 2048)
         except (ConnectionResetError, OSError, ConnectionAbortedError):
             log.info(
                 "Client Disconnected (addr {addr})".format(addr=self.addr))
             self.kill()
             continue
         try:
             self._buffer.extend(data)
             if len(data) < 4 or data[-4:] != b'\x00\x00\xff\xff':
                 continue
             inflator = zlib.decompressobj()
             inflated_data = inflator.decompress(
                 self._buffer) + inflator.flush(zlib.Z_SYNC_FLUSH)
             self._buffer = bytearray()
             info = sterilizer.clean(inflated_data.decode())
         except zlib.error:
             log.info("Client sent us a malformed call.")
             self.emit("BAD_CALL", data)
         else:
             log.debug("Received data from client, {len} bytes.".format(
                 len=len(data)))
             if type(info) == tuple:
                 if len(info) < 3 or len(info) > 3:
                     log.info(
                         "Client sent us either to much or too little information."
                     )
                     self.emit("BAD_CALL", info)
                 elif info[0] in self.server.EVENTS:
                     log.info(
                         "Client sent event ({e}) only triggerable server side."
                         .format(e=info[0]))
                     self.emit("BAD_CALL", info)
                 else:
                     handler, args, kwargs = info
                     self.server.emit(self, handler, *args, **kwargs)
             elif type(info) == list:
                 self.server.emit(self, "SERVER_REQUEST", info)
                 try:
                     if info[0] == 'GET':
                         log.debug(
                             "Client asked for value of {key}.".format(
                                 key=info[1]))
                         resp = sterilizer.dirty(
                             ['FOUND', info[1],
                              self.shared.get(info[1])])
                         self.send(resp)
                     elif info[0] == 'SET':
                         if not self.lock_sharing:
                             self.shared[info[1]] = info[2]
                             resp = sterilizer.dirty(
                                 ['CHANGED', info[1], info[2]])
                             self.send(resp)
                             log.debug(
                                 "Client set shared data {key} to {val}".
                                 format(key=info[1], val=info[2]))
                     elif info[0] == 'DEL':
                         if info[1] in self.shared and not self.lock_sharing:
                             del self.shared[info[1]]
                             resp = sterilizer.dirty(['REMOVED', info[1]])
                             self.send(resp)
                             log.debug(
                                 "Client deleted shared data {key}".format(
                                     key=info[1]))
                     else:
                         log.info("Client sent us invalid information")
                         self.emit("BAD_CALL", info)
                 except IndexError:
                     log.info(
                         "Client sent us either to much or too little information."
                     )
                     self.emit("BAD_CALL", info)
             else:
                 log.info("Client sent us unrecognized information")
                 self.emit("BAD_CALL", info)
Exemple #3
0
 def emit(self, handler, *args, **kwargs):
     cmd = sterilizer.dirty((handler, args, kwargs))
     self.send(cmd)
Exemple #4
0
 def call(self, handler: str, *args, **kwargs):
     data = sterilizer.dirty((handler, args, kwargs))
     self.send(data)
Exemple #5
0
 def __delitem__(self, key):
     req = sterilizer.dirty(['DEL', key])
     self.send(req)
Exemple #6
0
 def __setitem__(self, key, value):
     req = sterilizer.dirty(['SET', key, value])
     self.send(req)