Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Mosquitto bridge problem due to non blocking socket connect #478
I was creating a Mosquitto bridge on Windows 7 x64 system.
After checking the code I found that the statement in bridge.c at line number 368 as follows, makes a non blocking socket connect.
So the the next command on line 387 sometimes fails, since the connection has not been established, as the previous connect is non-blocking. In other word the command below is executed before the previous command has succeeded in making the socket connection.
As a quick workaround I had put a Sleep statement previous to line 387, so as to allow some time for the connection, however the the time taken for connect was depending on the speed of the network connection, as for a slower network it took more time and the duration for Sleep was not constant. So in some cases the bridge connection was failing where the network is slow.
Another solution which I made was to change line no. 368 as
where now the socket connection is a blocking connect. And this solution worked, even if the network was slow.
I wanted to know if the second solution which I have provided is a proper one, or is there a better method to solve the problem. I suppose that originally the socket connection may have be done as non-blocking for some specific purpose.
I have mosquitto_net_write for the CONNECT returning -1 and WSAGetLastError giving 10057 (WSAENOTCONN). This allows the CONNECT to proceed: