"Fossies" - the Fresh Open Source Software Archive

Member "TeXmacs-2.1.2-src/src/Plugins/Qt/QTMSockets.hpp" (5 May 2022, 3224 Bytes) of package /linux/misc/TeXmacs-2.1.2-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "QTMSockets.hpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.1.1_vs_2.1.2.

    1 
    2 /******************************************************************************
    3 * MODULE     : QTMSockets.hpp
    4 * DESCRIPTION: QT TeXmacs sockets manager - Header
    5 * COPYRIGHT  : (C) 2015 Denis RAUX
    6 *******************************************************************************
    7 * This software falls under the GNU general public license version 3 or later.
    8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
    9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
   10 ******************************************************************************/
   11 
   12 #include <stdint.h>
   13 #include <QApplication>
   14 #include <QObject>
   15 #include <QThread>
   16 #include <QSocketNotifier>
   17 
   18 #include "hashset.hpp"
   19 #include "string.hpp"
   20 #include "tm_link.hpp"
   21 
   22 #ifndef OS_MINGW
   23 
   24 #include <sys/types.h>
   25 #include <sys/socket.h>
   26 #include <netinet/in.h>
   27 
   28 #define SOCKADDR_IN sockaddr_in
   29 #define SOCKADDR_IN6 sockaddr_in6
   30 #define SOCKADDR sockaddr
   31 #define SOCKADDR_STORAGE sockaddr_storage
   32 
   33 #else
   34 
   35 namespace wsoc {
   36 #include <winsock2.h>
   37 #include <ws2tcpip.h>
   38 }
   39 typedef uint32_t in_addr_t;
   40 typedef int socklen_t;
   41 #define SOCKADDR_IN wsoc::sockaddr_in
   42 #define SOCKADDR_IN6 wsoc::sockaddr_in6
   43 #define SOCKADDR wsoc::sockaddr
   44 #define SOCKADDR_STORAGE wsoc::sockaddr_storage
   45 
   46 #endif
   47 
   48 // Common structures fors sockets
   49 
   50 extern unsigned qtmsocket_debug_count;
   51 
   52 string debug_io_string (string s);
   53 
   54 #define DBG_IO(a) \
   55   if (DEBUG_IO) debug_io << "TeXmacs " \
   56     << qtmsocket_debug_count++ << "] " << a << "\n"
   57 
   58 #define DBG_IOS(a,s) \
   59    if(N(s)) DBG_IO (a << debug_io_string (s))
   60 
   61 enum state { ST_OK, ST_WSA, ST_SOCKET, ST_FCNTL, ST_BIND,
   62          ST_LISTEN, ST_CONNECTION, ST_GETHOST, ST_NOTIF,
   63          ST_VOID, ST_HALTED, ST_CLOSED };
   64 
   65 class socket_basic {
   66 public:
   67   bool alive () { return st == ST_OK; }
   68 protected:
   69   socket_basic(void);
   70   ~socket_basic();
   71   int sock;
   72   int err;
   73   enum state st;
   74 private:
   75   static int count;
   76 #ifdef OS_MINGW
   77   static wsoc::WSADATA wsadata;
   78 #endif
   79 };
   80 
   81 // Socket for clients
   82 
   83 class socket_link: public QObject, public socket_basic, public tm_link_rep {
   84   Q_OBJECT
   85 
   86 public:
   87   socket_link (int s, SOCKADDR_STORAGE* addr);
   88   socket_link (string host, unsigned short port=6561);
   89   ~socket_link ();
   90   string  start ();
   91   void    write (string s, int channel=LINK_OUT);
   92   string& watch (int channel);
   93   string  read (int channel);
   94   void    listen (int msecs);
   95   void    interrupt () {}
   96   void    stop ();
   97   bool    alive () { return socket_basic::alive(); }
   98   int     getid () { return id; }
   99 
  100 public slots:
  101   void data_set_ready (int);
  102   void ready_to_send (int);
  103 signals:
  104   void disconnection (socket_link* clt);
  105 private :
  106   int id;
  107   string inbuf;
  108   string outbuf;
  109   QSocketNotifier *qsnr,*qsnw;
  110   SOCKADDR_STORAGE add; 
  111 };
  112 
  113 // Socket for servers
  114 
  115 class socket_server: public QObject, public socket_basic {
  116   Q_OBJECT
  117 
  118 public:
  119   socket_server (string host, unsigned short port=6561);
  120   ~socket_server();
  121   string read (int clt);
  122   void write (int clt, string s);
  123   enum state st;
  124   int err;
  125   int srv_count() { return N(clts); }
  126 public slots:
  127   void connection (int);
  128   void disconnection (socket_link* clt);
  129 private :
  130   socket_link* find_client (int id);
  131   hashset<pointer> clts;
  132   QSocketNotifier *qsnc;
  133 };