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

pull/6/head
Michael Zillgith 11 years ago
parent 6bc7b48e9f
commit fa8abf813f

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

@ -381,7 +381,18 @@ Socket_read(Socket self, uint8_t* buf, int size)
int int
Socket_write(Socket self, uint8_t* buf, int size) 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 void

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

Loading…
Cancel
Save