diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e7cf786f27a44ee24bc96de2a1b81768a214607..86c61c03e1e1d78ab19b40aae0d8c893edcd1134 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ add_library(objlib OBJECT src/channel/blc_channel.cpp src/channel/channel_file.cpp src/core/blc_array.cpp src/core/blc_mem.cpp src/core/blc_realtime.cpp src/core/blc_text.cpp src/core/blc_tools.cpp src/image/blc_image.cpp src/image/jpeg_tools.cpp - src/network/blc_array_tcp4_client.cpp src/network/blc_array_tcp4_server.cpp src/network/blc_network.cpp + src/network/blc_array_tcp4_client.cpp src/network/blc_array_tcp4_server.cpp src/network/blc_array_network.cpp src/network/blc_network.cpp src/process/blc_process.cpp src/program/blc_command.cpp src/program/blc_loop.cpp src/program/blc_program.cpp ) diff --git a/include/blc_array.h b/include/blc_array.h index 55b2a44f3f790c4c44235676ddfa2533c21b1092..2d80a2d2b9d365f2b336519284dca728984f0b41 100644 --- a/include/blc_array.h +++ b/include/blc_array.h @@ -51,7 +51,7 @@ typedef struct blc_array /**Define and allocates the array */ blc_array(uint32_t type, uint32_t format, std::vector<size_t> const &lengths); - /**Copy the definition of the array, NOT the content */ + /**Copy the definition of the array, not the content, but data points toward the same memeory and the dims definitaions are copied */ blc_array(blc_array const &array); /**Move the array. The content of initial array is not valid anymore*/ diff --git a/include/blc_array_network.h b/include/blc_array_network.h new file mode 100644 index 0000000000000000000000000000000000000000..4bada8900cc41df0fc6183632b40d1afbcb57464 --- /dev/null +++ b/include/blc_array_network.h @@ -0,0 +1,26 @@ +// +// blc_array_network.h +// blc +// +// Created by Arnaud Blanchard on 10/11/2020. +// + +#ifndef blc_array_network_h +#define blc_array_network_h + +#include "blc_core.h" + +typedef struct blc_array_network +#ifdef __cplusplus +:blc_array{ + ~blc_array_network(); + + int recv_data(); + int send_data(); +#else + blc_array array; +#endif + int socket_fd; +}blc_array_network; + +#endif /* blc_array_network_h */ diff --git a/include/blc_array_tcp4_client.h b/include/blc_array_tcp4_client.h index f658a24753d481502928dc37ace068aacd5804dc..15592d55c431b746fcd9d10038f090e0b8d100ee 100644 --- a/include/blc_array_tcp4_client.h +++ b/include/blc_array_tcp4_client.h @@ -1,7 +1,7 @@ #ifndef BLC_ARRAY_TCP4_CLIENT_H #define BLC_ARRAY_TCP4_CLIENT_H -#include"blc_core.h" +#include"blc_array_network.h" #ifdef __cplusplus #include <string> @@ -10,13 +10,10 @@ typedef struct blc_array_tcp4_client #ifdef __cplusplus -:blc_array { +:blc_array_network { blc_array_tcp4_client(std::string const &address, std::string const &port_name); blc_array_tcp4_client(std::string const &address, std::string const &port_name, uint32_t type, uint32_t format, std::vector<size_t> const &lengths); - ~blc_array_tcp4_client(); - - void read_properties(); // void server_manager(); int recv_data(); @@ -25,12 +22,11 @@ typedef struct blc_array_tcp4_client #else { blc_array array;// Not beautiful but makes it easy to convert C++ heritage of "class" in C struct inclusion. #endif - int socket_fd; }blc_array_tcp4_client; START_EXTERN_C blc_array_tcp4_client *blc_array_tcp4_client_new_void(const char *address, const char* port); - blc_array_tcp4_client *blc_array_tcp4_client_new( char const *address, char const *port_name, uint32_t type, uint32_t format, int dims_nb, size_t length0, ...); +blc_array_tcp4_client *blc_array_tcp4_client_new( char const *address, char const *port_name, uint32_t type, uint32_t format, int dims_nb, size_t length0, ...); void blc_array_tcp4_client_send_data(blc_array_tcp4_client *client); int blc_array_tcp4_client_recv_data(blc_array_tcp4_client *client); diff --git a/include/blc_array_tcp4_server.h b/include/blc_array_tcp4_server.h index 5a444ff0d761447f84a6a01097018bfebc43ca06..f33283eaddca5c24c7b271d4f4acdd1acc3b351a 100644 --- a/include/blc_array_tcp4_server.h +++ b/include/blc_array_tcp4_server.h @@ -1,28 +1,26 @@ #ifndef BLC_ARRAY_TCP4_SERVER_HPP #define BLC_ARRAY_TCP4_SERVER_HPP -#include"blc_core.h" +#include"blc_array_network.h" #include <string> #include <thread> typedef struct blc_array_tcp4_server #ifdef __cplusplus -:blc_array { +:blc_array_network { blc_array_tcp4_server(std::string const &port_name); blc_array_tcp4_server(std::string const &port_name, uint32_t type, uint32_t format, std::vector<size_t> const &lengths); - - ~blc_array_tcp4_server(); - + + void read_properties(); void send_properties(); - void server_manager(); int recv_data(); int send_data(); - int bytes_to_send(); + void server_manager(); int wait_connection(); #else blc_array *array; #endif - int socket_fd, client_socket=-1; //We suppose only on client + int client_socket; //We suppose only on client }blc_array_tcp4_server; START_EXTERN_C diff --git a/include/blc_image.h b/include/blc_image.h index 5a690534e5738b6583ae4203579c501d80ff7595..51d1670dbe2556e0eeb4fef9e8ccda3f7dedf7e6 100644 --- a/include/blc_image.h +++ b/include/blc_image.h @@ -50,6 +50,8 @@ typedef struct blc_Y800_image:blc_array{ START_EXTERN_C + void blc_RGBA_from_Y800(uint8_t *dest, uint8_t const *src, int elements_nb); + /**Return the number of bytes for a pixel of the format. -1 is returned if this data is undefined (i.e. JPEG)*/ int blc_image_get_bytes_per_pixel(blc_array const *image); diff --git a/include/blc_mem.h b/include/blc_mem.h index 35a8476de25fd2467c663c22b12ebb67b0a9fd3b..276a18730e72a42799ee5c001869929c1f999ea4 100644 --- a/include/blc_mem.h +++ b/include/blc_mem.h @@ -54,7 +54,7 @@ typedef struct blc_mem { /** Create and allocate memory of size size */ blc_mem(size_t size); - ///Copy the defienition of blc_meme, NOT the content + ///Copy the defienition of blc_meme, not the content, but data points to the initial content blc_mem(blc_mem const &mem); ///Move the definition and move the content diff --git a/include/blc_network.h b/include/blc_network.h index 8668129275cea680fadc3e78d4674b3c01d4d2df..2164424263ab90235779bc30bc959ea07ed5d913 100644 --- a/include/blc_network.h +++ b/include/blc_network.h @@ -16,6 +16,5 @@ #include "blc_network_base.h" #include "blc_array_tcp4_client.h" #include "blc_array_tcp4_server.h" -#include "blc_net_array.h" #endif ///@} diff --git a/include/blc_network_base.h b/include/blc_network_base.h index d1e6dda47b3ea0b706b884c059e0b53a69058655..3afea85b3c7d6ec9d459d2a8e1325e4eeca6c870 100644 --- a/include/blc_network_base.h +++ b/include/blc_network_base.h @@ -46,6 +46,8 @@ typedef struct blc_network blc_network() = default; blc_network(char const *address_name, char const *port_name, int mode, ssize_t buffer_size); ~blc_network(); + + void publish(); void send(ssize_t send_size=-1); void send_buffer(void const* data, size_t size); // Return 1 if data has been read, 0 otherwise (timeout) diff --git a/python/notebook.ipynb b/python/notebook.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..530a9e118c24f99ca8f854fc0c7eab06a9936893 --- /dev/null +++ b/python/notebook.ipynb @@ -0,0 +1,97 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import matplotlib.image as mpimg\n", + "import PIL\n", + "\n", + "from blc_network import BlcArrayTCP4Client" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "client=BlcArrayTCP4Client(\"localhost\", \"31440\")\n", + "\n", + "print(client.type, client.format, client.dims)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'client' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-2-ca24b43a1928>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwhile\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbytes_to_recv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"drop\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mpil_image\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mPIL\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mImage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrombytes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"P\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdims\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdims\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'client' is not defined" + ] + } + ], + "source": [ + "\n", + "while( client.bytes_to_recv() > 0):\n", + " print(\"drop\")\n", + " client.recv_data()\n", + " \n", + "pil_image = PIL.Image.frombytes(\"P\", (client.dims[0], client.dims[1]), client.data)\n", + "plt.imshow(pil_image)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/src/core/blc_mem.cpp b/src/core/blc_mem.cpp index 1a1315496e81149226bad9fb621dbca885a3da3e..aa61fc8ea634b2ab41a55d52e245afe1e0aa27ed 100644 --- a/src/core/blc_mem.cpp +++ b/src/core/blc_mem.cpp @@ -28,7 +28,7 @@ blc_mem::blc_mem(size_t size) data = MANY_ALLOCATIONS(size, char); } -blc_mem::blc_mem(blc_mem const &mem):data(nullptr), size(mem.size){ +blc_mem::blc_mem(blc_mem const &mem):data(mem.data), size(mem.size){ }; blc_mem::blc_mem(blc_mem &&mem):size(mem.size){ diff --git a/src/core/blc_tools.cpp b/src/core/blc_tools.cpp index 4f9a9ca142d2daacc29a93fad439502604612183..2ab8e844e548b46f7f03e874aa19325a710f92f6 100644 --- a/src/core/blc_tools.cpp +++ b/src/core/blc_tools.cpp @@ -20,7 +20,6 @@ #include "blc_tools.h" - #include <limits.h> #include <signal.h> #include <stdio.h> diff --git a/src/image/blc_image.cpp b/src/image/blc_image.cpp index 0a840a7b87dd15cac98480d39f9b79947ca0b040..ac7655b16c8144a00e570c8d576e3008bbc2bd07 100644 --- a/src/image/blc_image.cpp +++ b/src/image/blc_image.cpp @@ -24,6 +24,8 @@ #include "blc_tools.h" #include "jpeg_tools.h" +#define Y800_to_RGBA(val) (val+(val<<8)+(val<<16)+(255<<24)) + typedef struct { uchar r, g, b; } type_RGB; @@ -173,6 +175,16 @@ void blc_Y800_from_BA81(blc_array *dest, blc_array const *src) } } +void blc_RGBA_from_Y800(uint8_t *dest, uint8_t const *src, int elements_nb){ + for (int i=0; i<elements_nb; i++) { + dest[4*i] = src[i]; + dest[4*i+1] = src[i]; + dest[4*i+2] = src[i]; + dest[4*i+3] = src[i]; + }; +} + + void blc_Y800_float_from_BA81(blc_array *dest, blc_array const *src) { int i, j; diff --git a/src/network/blc_array_network.cpp b/src/network/blc_array_network.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4b2ffe8efa0db3e103b1e13803e77ac2896f53b --- /dev/null +++ b/src/network/blc_array_network.cpp @@ -0,0 +1,9 @@ +#include "blc_array_network.h" + +#include <unistd.h> + +blc_array_network::~blc_array_network(){ + SYSTEM_ERROR_CHECK(close(socket_fd), -1, nullptr); +} + + diff --git a/src/network/blc_array_tcp4_client.cpp b/src/network/blc_array_tcp4_client.cpp index d11a9665153b1ae58c8c66a57f5deed235ef2c00..c801ad2f048ea6f30a42afa739cdd7beb2bd2d61 100644 --- a/src/network/blc_array_tcp4_client.cpp +++ b/src/network/blc_array_tcp4_client.cpp @@ -33,6 +33,8 @@ blc_array_tcp4_client::blc_array_tcp4_client(string const &address, string const freeaddrinfo(results); read_properties(); allocate(); + SYSTEM_ERROR_CHECK(setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)), -1, nullptr); + SYSTEM_ERROR_CHECK(setsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)), -1, nullptr); } blc_array_tcp4_client::blc_array_tcp4_client(string const &address, string const &port_name, uint32_t type, uint32_t format, vector<size_t> const &lengths):blc_array_tcp4_client(address, port_name){ @@ -44,9 +46,7 @@ void blc_array_tcp4_client::read_properties(){ ssize_t ret; vector<char> properties(1024); - for (size_t read_nb=0; read_nb < properties.size(); read_nb+=static_cast<size_t>(ret)){ - SYSTEM_ERROR_CHECK(ret=read(socket_fd, properties.data()+read_nb, properties.size()-read_nb), -1, nullptr); - } + SYSTEM_ERROR_CHECK(ret=recv(socket_fd, properties.data(), properties.size(), MSG_WAITALL), -1, nullptr); sscan_properties(properties.data()); // The array will need allocation } @@ -68,10 +68,6 @@ void blc_array_tcp4_client::send_data(){ SYSTEM_ERROR_CHECK(write(socket_fd, data, size), -1, nullptr); } -blc_array_tcp4_client::~blc_array_tcp4_client(){ - SYSTEM_ERROR_CHECK(close(socket_fd), -1, nullptr); -} - START_EXTERN_C blc_array_tcp4_client *blc_array_tcp4_client_new_void( char const *address, char const *port){ diff --git a/src/network/blc_array_tcp4_server.cpp b/src/network/blc_array_tcp4_server.cpp index 4d727947cf1f9bb700aea71ec4a7959f00fdc95b..9f1a576c20dc380fa7107693350e48cca425fe5d 100644 --- a/src/network/blc_array_tcp4_server.cpp +++ b/src/network/blc_array_tcp4_server.cpp @@ -47,7 +47,11 @@ int blc_array_tcp4_server::wait_connection(){ client_socket=accept(socket_fd, reinterpret_cast<struct sockaddr*>(&client_storage), &client_addr_len); if (client_socket==-1) return errno; - if (dims!=nullptr) send_properties(); + if (dims!=nullptr) { + SYSTEM_ERROR_CHECK(setsockopt(client_socket, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)), -1, nullptr); + SYSTEM_ERROR_CHECK(setsockopt(client_socket, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)), -1, nullptr); + send_properties(); + } return 0; } @@ -56,6 +60,7 @@ void blc_array_tcp4_server::send_properties(){ vector<char> properties(1024); length=static_cast<size_t>(sprint_properties(properties.data(), properties.size()-1)); + if (length>=1024) EXIT_ON_ERROR(" length (%zu) too longe that the max (%zu)", length, properties.size()); properties[length]=0; SYSTEM_ERROR_CHECK(write(client_socket, properties.data(), properties.size()), -1, nullptr); } @@ -83,16 +88,6 @@ int blc_array_tcp4_server::send_data(){ else return 0; } -int blc_array_tcp4_server::bytes_to_send(){ - int bytes_to_send; - SYSTEM_ERROR_CHECK(ioctl(client_socket, TIOCOUTQ, &bytes_to_send), -1, "client socket (%d)", client_socket); - return bytes_to_send; -} - -blc_array_tcp4_server::~blc_array_tcp4_server(){ - SYSTEM_ERROR_CHECK(close(socket_fd), -1, nullptr); -} - START_EXTERN_C blc_array_tcp4_server *blc_array_tcp4_server_new_void(char const*port_name){ return new blc_array_tcp4_server(port_name); diff --git a/src/network/blc_network.cpp b/src/network/blc_network.cpp index b6c779b1adc33cea8c8197303dbd01ad8ca25a8d..4e7b2c65cdf73961766720e647a9c53bdddcface 100644 --- a/src/network/blc_network.cpp +++ b/src/network/blc_network.cpp @@ -44,7 +44,6 @@ int blc_set_iovec(struct iovec *iovec, void *data, size_t size, ...) return len; } - void blc_set_msghdr(struct msghdr *msghdr, void *data, size_t size, ...) { va_list arguments; @@ -54,7 +53,6 @@ void blc_set_msghdr(struct msghdr *msghdr, void *data, size_t size, ...) va_end(arguments); } - void blc_network::create_ip4_socket(const char* address_name, const char *port_name, int socket_type){ struct addrinfo hints, *results; @@ -73,8 +71,6 @@ void blc_network::create_ip4_socket(const char* address_name, const char *port_n freeaddrinfo(results); } - - void blc_network::allocate_system_size_buffer(){ size_t system_buffer_size=0; socklen_t len=sizeof(system_buffer_size); @@ -88,7 +84,7 @@ void blc_network::get_remote_address(char *channel_name, socklen_t name_size, ch ret=getnameinfo((const struct sockaddr *)&remote_address, remote_address_length, channel_name, name_size, port_name, port_name_size, NI_NUMERICSERV); if (ret !=0) EXIT_ON_ERROR(gai_strerror(ret)); //, 0, "setting address: %s", address_name)); } - + void blc_network::recv_buffer_block(void *buffer, size_t buffer_size){ ssize_t ret; SYSTEM_ERROR_CHECK(ret=recvfrom(socket_fd, buffer, buffer_size, MSG_TRUNC, (struct sockaddr*)&remote_address, &remote_address_length), -1, "Socket '%d', data: '%p', size: '%lu'", socket_fd, data, size);