def getResponse(mySocket): #wait for server responses... while True: try: retBuffer = mySocket.recv( 1024 ) except: #close the client if the connection is down print "ERROR: connection down" sys.exit() if len(retBuffer) >= 4: # Get the GPB message length length = struct.unpack('!I',retBuffer[0:4])[0] if len(retBuffer) == 4: # Only received the length so far retBuffer += mySocket.recv( 1024 ) if (len(retBuffer) == (length + 4)): # Populate the message with the data received message = messages_pb2.ServerResponse() message.ParseFromString(retBuffer[4:4+length]) #only allow correct version numbers if (message.version== version) and verify_checksum(message): opcode = message.opcode #send packet to correct handler try: opcodes[opcode](mySocket,message) except KeyError: break #mySocket.send ('\x01\x01\x02\x03\x53\x10\x12\x34') break return
def general_failure(conn, err_type, reason): #find the appropriate opcode to send for particular errors if err_type == 'create': typebyte = '\x12' elif err_type == 'delete': typebyte = '\x22' elif err_type == 'deposit': typebyte = '\x32' elif err_type == 'withdraw': typebyte = '\x42' elif err_type == 'balance': typebyte = '\x52' # Create a message to be transmitted message = messages_pb2.ServerResponse() message.version = '\x01' message.opcode = typebyte message.error_message = reason.encode('utf-8') insert_checksum(message) # Send message_string = message.SerializeToString() length = len(message_string) conn.send(struct.pack('!I', length) + message_string) return
def delete_success(conn): # Create a message to be transmitted message = messages_pb2.ServerResponse() message.version = '\x01' message.opcode = '\x21' insert_checksum(message) # Send message_string = message.SerializeToString() length = len(message_string) conn.send(struct.pack('!I', length) + message_string) return