- fixed problem with non-blocking socket send function on WIN32

This commit is contained in:
Michael Zillgith 2015-02-13 11:22:50 +01:00
parent 6bc7b48e9f
commit fa8abf813f
3 changed files with 43 additions and 21 deletions

View file

@ -29,10 +29,10 @@ downloadHandler(void* parameter, uint8_t* buffer, uint32_t bytesRead)
if (bufferPosition + bytesRead < MAX_BUFFER_SIZE) {
memcpy(downloadBuffer + bufferPosition, buffer, bytesRead);
return false;
return true;
}
else
return true;
return false;
}
int main(int argc, char** argv) {

View file

@ -381,7 +381,18 @@ Socket_read(Socket self, uint8_t* buf, int size)
int
Socket_write(Socket self, uint8_t* buf, int size)
{
return send(self->fd, (char*) buf, size, 0);
int bytes_sent = send(self->fd, (char*) buf, size, 0);
if (bytes_sent == SOCKET_ERROR) {
int errorCode = WSAGetLastError();
if (errorCode == WSAEWOULDBLOCK)
bytes_sent = 0;
else
bytes_sent = -1;
}
return bytes_sent;
}
void

View file

@ -167,17 +167,29 @@ sendBuffer(CotpConnection* self)
bool retVal = false;
if (Socket_write(self->socket, ByteBuffer_getBuffer(self->writeBuffer), writeBufferPosition) == writeBufferPosition)
retVal = true;
int sentBytes;
do {
sentBytes = Socket_write(self->socket, ByteBuffer_getBuffer(self->writeBuffer), writeBufferPosition);
if (sentBytes == -1)
goto exit_function;
} while (sentBytes == 0);
retVal = true;
ByteBuffer_setSize(self->writeBuffer, 0);
exit_function:
return retVal;
}
CotpIndication
CotpConnection_sendDataMessage(CotpConnection* self, BufferChain payload)
{
CotpIndication retValue = COTP_OK;
int fragments = 1;
int fragmentPayloadSize = CotpConnection_getTpduSize(self) - COTP_DATA_HEADER_SIZE;
@ -227,9 +239,6 @@ CotpConnection_sendDataMessage(CotpConnection* self, BufferChain payload)
currentChainIndex = 0;
}
if (DEBUG_COTP)
printf("%02x ", currentChain->buffer[currentChainIndex]);
buffer[bufPos++] = currentChain->buffer[currentChainIndex];
currentChainIndex++;
@ -242,13 +251,25 @@ CotpConnection_sendDataMessage(CotpConnection* self, BufferChain payload)
if (DEBUG_COTP)
printf("COTP: Send COTP fragment %i bufpos: %i\n", fragments, currentBufPos);
if (!sendBuffer(self))
return COTP_ERROR;
if (!sendBuffer(self)) {
retValue = COTP_ERROR;
if (DEBUG_COTP)
printf("COTP: sending message failed!\n");
goto exit_function;
}
fragments--;
}
return COTP_OK;
exit_function:
if (DEBUG_COTP)
printf("COTP: message transmission finished (fragments=%i, return=%i)\n", fragments, retValue);
return retValue;
}
static void
@ -435,16 +456,6 @@ CotpConnection_init(CotpConnection* self, Socket socket,
self->packetSize = 0;
}
//void
//CotpConnection_destroy(CotpConnection* self)
//{
// if (self->writeBuffer != NULL)
// ByteBuffer_destroy(self->writeBuffer);
//
// if (self->readBuffer != NULL)
// ByteBuffer_destroy(self->readBuffer);
//}
int /* in byte */
CotpConnection_getTpduSize(CotpConnection* self)
{