Frage

I have a simple Asyncore client in Linux interacting with a server developed in C on a Windows machine.

The setup works great for data rates of about once per second being written to the asyncore buffer, but whenever my data rate exceeds that (I have tested once per 0.1 seconds), Asyncore does not send all writes. Some buffer values get lost and never make it to the server.

I suspect this is because asyncore goes through a loop and only checks to see if there is data in the buffer to send once per loop, and if data rates are higher than the loop time, then they get lost. Is this true? If so, is there a workaround to this or a different socket module other than Asyncore that doesnt pose this problem?

Here is the code I am using:

class DETClient(asyncore.dispatcher):

    buffer = ""
    t = None

    def __init__(self, host, port):

        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host,port))
        self.host=host
        self.port=port
        self.t = ReceiverBoard(self)
        self.t.start()

    def initiate_connection_with_server(self):
        print("trying to initialize connection with server...")
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((self.host,self.port))

    def sendCommand(self, command):
        self.buffer = command +'\0'

    def handle_connect(self):
        pass

    def handle_error(self):
        print("problem reaching server.")
        self.initiate_connection_with_server()

    def handle_close(self):
        self.t.stop()
        self.close()

    def handle_read(self):
        message = str(self.recv(20)) #how many bytes should be read?

        if (message=="b'system_bias_ON'"):
            self.t.enable_sys_bias()

        if (message=="b'system_bias_OFF'"):
            self.t.disable_sys_bias()

        if (message[2:5]=="DET"):
            self.t.DET_bias_command_received = True
            self.t.DET_bias_command_text = message

        if (message[2:7]=="CURVE"):
            self.t.DET_bias_command_received = True
            self.t.DET_bias_command_text = message

    def handle_write(self):
        sent=self.send(self.buffer.encode())
        self.buffer=""
War es hilfreich?

Lösung

I found the answer to this question. Asyncore is not to blame in this case. The code provided in the question is making bad use of the buffer interface. All I had to do was replace the last line in the handle_write method with the following line:

self.buffer = self.buffer[sent:]

This solves the problem

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top