def Serialize(self): returnArray = bytearray() length = Params.MESSAGE_HEADER_LENGTH + 1 + 2 + (2 + 2 + len(self.JpgImageData)) returnArray.extend(ByteConverter.GetBytesFromInteger(length)) returnArray.extend(ByteConverter.GetBytesFromByteInteger(self.MessageType)) returnArray.extend(ByteConverter.GetBytesFromUshort(self.Width)) returnArray.extend(ByteConverter.GetBytesFromUshort(self.Height)) returnArray.extend(self.JpgImageData) return returnArray
def Serialize(self): returnArray = bytearray() returnArray.extend( ByteConverter.GetBytesFromInteger(self.MessageLength)) returnArray.extend( ByteConverter.GetBytesFromByteInteger(self.MessageType)) returnArray.extend( ByteConverter.GetBytesFromBool(self.SendGoodbyeMessage)) return returnArray
def run(self): __receiveEnabled = True while __receiveEnabled and self.SocketListener.ListenerEnabled: try: # print("Awaiting message") receiveObject = ReceiveObject() while not receiveObject.IsSameLength(): receiveObject.buffer.extend(self.Client.recv(Params.BUFFER_SIZE)) receiveObject.CashedMessage.extend(receiveObject.buffer) receiveObject.buffer.clear() # Определяем тип сообщения messageType = MessageTypes( ByteConverter.GetInteger( receiveObject.CashedMessage, MessageParams.MESSAGE_HEADER_LENGTH, 1)) # Получаем объект сообщения message = MessageDeserializer.Deserialize(messageType, receiveObject.CashedMessage) # Очищаем кэш receiveObject.CashedMessage.clear() # Выполняем действие, связанное с этим сообщением # Если сообщение окончания работы сокета, то перкращаем чтение и закрываем сокет __receiveEnabled = MessageResponser.SendResponse(self.Client, messageType, message) except SocketLib.timeout: continue print(f"Calling close client {self.Client}") message = CloseConnectionMessage(not __receiveEnabled).Serialize() self.Client.send(message) self.Client.shutdown(SocketLib.SHUT_RDWR) self.Client.close() if self.Client in self.SocketListener.Clients: self.SocketListener.Clients.remove(self.Client) else: print(f"Socket [{self.Client}] not registered in list")
def MessageSize(self): if len(self.CashedMessage) == 0: return self.__messageSize valueByteArray = bytearray() for i in range(4): valueByteArray.append(self.CashedMessage[i]) self.__messageSize = ByteConverter.GetInteger(valueByteArray) return self.__messageSize
def Serialize(self): returnArray = bytearray() returnArray.extend( ByteConverter.GetBytesFromInteger(self.MessageLength)) returnArray.extend( ByteConverter.GetBytesFromByteInteger(self.MessageType)) returnArray.extend( ByteConverter.GetBytesFromByteInteger(self.Probability)) returnArray.extend(ByteConverter.GetBytesFromShort(self.PositionX)) returnArray.extend(ByteConverter.GetBytesFromShort(self.PositionY)) returnArray.extend(ByteConverter.GetBytesFromUshort(self.SizeX)) returnArray.extend(ByteConverter.GetBytesFromUshort(self.SizeY)) return returnArray
def __init__(self, data: bytearray): self.MessageLength = len(data) self.MessageType = MessageTypes(ByteConverter.GetInteger(data, 4, 1)) self.Width = ByteConverter.GetInteger(data, 5, 2, False) self.Height = ByteConverter.GetInteger(data, 7, 2, False) self.JpgImageData = bytes(data[9:]) # ByteConverter.GetBytes(data, 9)
def Serialize(self): returnArray = bytearray() length = Params.MESSAGE_HEADER_LENGTH + 1 returnArray.extend(ByteConverter.GetBytesFromInteger(length)) returnArray.extend(ByteConverter.GetBytesFromByteInteger(self.MessageType)) return returnArray
def Deserialize(data: bytearray): offset = Params.MESSAGE_HEADER_LENGTH + 1 return CloseConnectionMessage( ByteConverter.GetBool(data, offset=offset))