tuyau

Client/server for transfering files (like cp)
git clone https://noulin.net/git/tuyau.git
Log | Files | Refs | README

netFrame.h (6348B)


      1 #pragma once
      2 
      3 /* Libsheepy documentation: https://spartatek.se/libsheepy/ */
      4 
      5 /**
      6  * netFrame is client and server for sending and receiving data frames
      7  * the sockets can remain open all the time
      8  *
      9  * A data frame is a buffer and a size.
     10  *
     11  * Frame:
     12  *  -------------- ------
     13  * | size 8 bytes | data
     14  *  -------------- ------
     15  *
     16  * The client opens a socket and calls send with the buffer address and size:
     17  *
     18  *    cleanAllocateNetFrame(netframe);
     19  *    Loop until all frames are sent:
     20  *        o(netframe,send , sock, buffer, size);
     21  *    Send a frame with size 0 with no data to signal the end of the transfer
     22  *      when a frame size 0 is received in the server, the callback is not called
     23  *    The socket can be kept open
     24  *
     25  * The server sets a callback before receiving connections
     26  *
     27  *    cleanAllocateNetFrame(netframe);
     28  *    o(netframe, setCallback, cb, NULL context);
     29  *
     30  * The callback is executed after each frame is completely received.
     31  * The callback prototype is:
     32  *
     33  *    callBackNetFramet:
     34  *    bool cb(void *buf, size_t size, void *context);
     35  *
     36  * The provided buffer buf is freed in netFrame.
     37  *
     38  * In the event loop, the server accepts connections and call receive:
     39  *
     40  *    var r = o(netframe,receive, mysock);
     41  *
     42  * receive waits for frames until a frame size 0 is received
     43  */
     44 
     45 #include <openssl/ssl.h>
     46 #include "libsheepyObject.h"
     47 
     48 /* Class netFrame */
     49 typ struct netFrame netFramet;
     50 
     51 #define isONetFrame(obj) isOType(obj, "netFrame")
     52 #define isONetFrametG isONetFrame
     53 
     54 /* for object inheriting netFrame, cast to netFrame to be able to use this class functions and generics*/
     55 #define cNetFrame(self) ( (netFramet*) self )
     56 
     57 typ void            (*freeNetFrameFt)      (netFramet *self);
     58 typ void            (*terminateNetFrameFt) (netFramet **self);
     59 typ char*           (*toStringNetFrameFt)  (netFramet *self);
     60 typ netFramet*      (*duplicateNetFrameFt) (netFramet *self);
     61 typ void            (*smashNetFrameFt)     (netFramet **self);
     62 
     63 /**
     64  * free netFrame
     65  */
     66 typ void            (*finishNetFrameFt)    (netFramet **self);
     67 
     68 typ const char*     (*helpNetFrameFt)      (netFramet *self);
     69 
     70 /**
     71  * callback is a function called after each frame is received
     72  *
     73  * \param
     74  * buf data from the network, netFrame frees the buffer
     75  * \param
     76  * size data szie from the network
     77  * \param
     78  * context callback context variable
     79  */
     80 typ bool (*callBackNetFramet)(void *buf, size_t size, void *context);
     81 
     82 /**
     83  * set callback called after each received frame in the receive function
     84  * and context for the callback function
     85  */
     86 typ void (*setCallbackNetFrameFt)(netFramet *self, callBackNetFramet callback, void* context);
     87 
     88 /**
     89  * send buf, size 0 means end of transaction
     90  */
     91 typ bool (*sendNetFrameFt)(netFramet *self, SSL *ssl, void* buf, size_t size);
     92 
     93 /**
     94  * receive frames until the socket is closed by the client
     95  */
     96 typ bool (*receiveNetFrameFt)(netFramet *self, SSL *ssl);
     97 
     98 /**
     99  * receive one frame and return
    100  */
    101 typ bool (*receiveOneNetFrameFt)(netFramet *self, SSL *ssl);
    102 
    103 /**
    104  * end transaction or inform the server that the connection is going to be closed
    105  * this function sends a frame of size 0
    106  * when one frame is sent:
    107  *   - the function send sends the size and the buffer
    108  *   - sending a frame size 0 closes the connection
    109  * when multiple frames are sent:
    110  *   - call send as many times as needed to send buffer of size not 0
    111  *   - when finished send a frame size 0 meaning end of transaction
    112  *   - to close the connection, send a frame size 0 again
    113  */
    114 typ bool (*endNetFrameFt)(netFramet *self, SSL *ssl);
    115 
    116 /**
    117  * class functions
    118  * allocated once for all objects
    119  *
    120  * freed with finalizeNetFrame
    121  */
    122 
    123 /**
    124  * use this define in child classes and add the new function after this class functions
    125  *
    126  * in this define, add the methods after <finishNetFrameFt    finish;>
    127  *
    128  * Example:
    129  * #define RINGFUNCTIONST \n *   NETFRAMEFUNCTIONST; \n *   setSizeRingFt           setSize
    130  */
    131 #define NETFRAMEFUNCTIONST \
    132   helpNetFrameFt        help;\
    133   setCallbackNetFrameFt setCallback;\
    134   sendNetFrameFt        send;\
    135   receiveNetFrameFt     receive;\
    136   receiveOneNetFrameFt  receiveOne;\
    137   endNetFrameFt         end
    138 
    139 typ struct {
    140   freeNetFrameFt      free;
    141   terminateNetFrameFt terminate;
    142   toStringNetFrameFt  toString;
    143   duplicateNetFrameFt duplicate;
    144   smashNetFrameFt     smash;
    145   finishNetFrameFt    finish;
    146   NETFRAMEFUNCTIONST;
    147 } netFrameFunctionst;
    148 
    149 /**
    150  * class
    151  */
    152 struct netFrame {
    153   const char         *type;
    154   netFrameFunctionst *f;
    155 
    156   callBackNetFramet  callback;
    157   void               *context;
    158 };
    159 
    160 /* netFrame */
    161 
    162 /** return true when this object file was compiled with current libsheepy version */
    163 #define isNetFrameCompiledWithCurrentLisheepyVersion checkLibsheepyVersionNetFrame(LIBSHEEPY_VERSION)
    164 bool checkLibsheepyVersionNetFrame(const char *currentLibsheepyVersion);
    165 
    166 #define createNetFrame(obj) ;netFramet obj; initiateNetFrame(&obj)
    167 #define createAllocateNetFrame(obj) ;netFramet *obj; initiateAllocateNetFrame(&obj)
    168 
    169 void initiateNetFrame(netFramet *self);
    170 void initiateAllocateNetFrame(netFramet **self);
    171 void finalizeNetFrame(void);
    172 
    173 /* initialize class methods, call registerMethodsNetFrame from classes inheriting this class */
    174 void registerMethodsNetFrame(netFrameFunctionst *f);
    175 
    176 netFramet* allocNetFrame(void);
    177 
    178 /* terminate netFramet val when it is out of scope */
    179 void cleanUpNetFrameTerminateG(netFramet **val);
    180 
    181 /* free netFramet val when it is out of scope */
    182 void cleanUpNetFrameFreeG(netFramet *val);
    183 
    184 /* finish netFramet val when it is out of scope */
    185 void cleanUpNetFrameFinishG(netFramet *val);
    186 
    187 /**
    188  * declare pointer name with Type netFramet and terminate name when it is out of scope
    189  */
    190 #define cleanNetFrameP(name) netFramet *name CLEANUP(cleanUpNetFrameTerminateG)
    191 
    192 /**
    193  * allocate NetFrame (pointer) and clean up when it is out of scope
    194  */
    195 #define cleanAllocateNetFrame(obj) ;cleanNetFrameP(obj); initiateAllocateNetFrame(&obj)
    196 
    197 /**
    198  * declare local object name with Type netFramet and free name when it is out of scope
    199  */
    200 #define cleanNetFrame(name) netFramet name CLEANUP(cleanUpNetFrameFreeG); initiateNetFrame(&name)
    201 
    202 /**
    203  * declare pointer name with Type netFramet and finish name when it is out of scope
    204  */
    205 #define cleanFinishNetFrameP(name) netFramet *name CLEANUP(cleanUpNetFrameFinishG)
    206 
    207 /* end class netFrame*/
    208 // vim: set expandtab ts=2 sw=2: