arpadboda commented on a change in pull request #550: MINIFICPP-822 - Nanofi raw S2S implementation shouldn't depend on any… URL: https://github.com/apache/nifi-minifi-cpp/pull/550#discussion_r281643041
########## File path: nanofi/src/core/cstream.c ########## @@ -0,0 +1,203 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <sys/socket.h> // socket +#include <arpa/inet.h> // inet_addr +#include <netdb.h> // hostent +#include <errno.h> +#include <string.h> + +#include "api/nanofi.h" +#include "core/cstructs.h" +#include "core/cstream.h" +#include "core/log.h" + +int write_uint32_t(uint32_t value, cstream * stream) { + value = htonl(value); + return write_buffer((uint8_t*)(&value), sizeof(uint32_t), stream); +} + +int write_uint16_t(uint16_t value, cstream * stream) { + value = htons(value); + return write_buffer((uint8_t*)(&value), sizeof(uint16_t), stream); +} + +int write_buffer(const uint8_t *value, int len, cstream * stream) { + int ret = 0, bytes = 0; + + while (bytes < len) { + ret = send(stream->socket_, value + bytes, len - bytes, 0); + // check for errors + if (ret <= 0) { + logc(err, "Could not send to %d, error: %s", stream->socket_, strerror(errno)); + close_stream(stream); + return ret; + } + bytes += ret; + } + + if (ret) + logc(trace, "Send data size %d over socket %d", len, stream->socket_); + return bytes; +} + +int read_buffer(uint8_t *buf, int len, cstream * stream) { + int32_t total_read = 0; + while (len) { + int bytes_read = recv(stream->socket_, buf, len, 0); + logc(trace, "Recv call %d", bytes_read); + if (bytes_read <= 0) { + if (bytes_read == 0) { + logc(debug, "Other side hung up on %d",stream->socket_); + } else { + if (errno == EAGAIN || errno == EWOULDBLOCK) { Review comment: Nice catch, fixed! ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
