diff --git a/examples/iec61850_client_example_files/client_example_files.c b/examples/iec61850_client_example_files/client_example_files.c index 0b3f5f3..33c6750 100644 --- a/examples/iec61850_client_example_files/client_example_files.c +++ b/examples/iec61850_client_example_files/client_example_files.c @@ -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) { diff --git a/src/hal/socket/win32/socket_win32.c b/src/hal/socket/win32/socket_win32.c index 1d888e9..528237a 100644 --- a/src/hal/socket/win32/socket_win32.c +++ b/src/hal/socket/win32/socket_win32.c @@ -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 diff --git a/src/mms/iso_cotp/cotp.c b/src/mms/iso_cotp/cotp.c index 83089f0..daa8890 100644 --- a/src/mms/iso_cotp/cotp.c +++ b/src/mms/iso_cotp/cotp.c @@ -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) {