]> git.lyx.org Git - lyx.git/blob - src/lyxsocket.h
update build instructions (Qt 4.2.2 etc.)
[lyx.git] / src / lyxsocket.h
1 // -*- C++ -*-
2 /**
3  * \file lyxsocket.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author João Luis M. Assirati
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef LYXSOCKET_H
15 #define LYXSOCKET_H
16
17 #include "support/socktools.h"
18 #include "lyxfunc.h"
19
20 #include <boost/shared_ptr.hpp>
21
22 #include <string>
23 #include <map>
24
25
26 namespace lyx {
27
28 class LyXServerSocket;
29 class LyXDataSocket;
30
31
32 /** Sockets can be in two states: listening and connected.
33  *  Connected sockets are used to transfer data, and will therefore
34  *  be called Data Sockets. Listening sockets are used to create
35  *  Data Sockets when clients connect, and therefore will be called
36  * Server Sockets.
37
38  * This class encapsulates local (unix) server socket operations and
39  * manages LyXDataSockets objects that are created when clients connect.
40  */
41 class LyXServerSocket {
42 public:
43         ///
44         LyXServerSocket(LyXFunc *, std::string const &);
45         ///
46         ~LyXServerSocket();
47         /// Address of the local socket
48         std::string const & address() const;
49         /// To be called when there is activity in the server socket
50         void serverCallback();
51         /// To be called when there is activity in the data socket
52         void dataCallback(int fd);
53 private:
54         ///
55         void writeln(std::string const &);
56         ///
57         LyXFunc * func;
58         /// File descriptor for the server socket
59         int fd_;
60         /// Stores the socket filename
61         std::string address_;
62         /// Maximum number of simultaneous clients
63         enum {
64                 MAX_CLIENTS = 10
65         };
66         /// All connections
67         std::map<int, boost::shared_ptr<LyXDataSocket> > clients;
68 };
69
70
71 /** This class encapsulates data socket operations.
72  *  It provides read and write IO operations on the socket.
73  */
74 class LyXDataSocket {
75 public:
76         ///
77         LyXDataSocket(int fd);
78         ///
79         ~LyXDataSocket();
80         /// Connection status
81         bool connected() const;
82         /// Line buffered input from the socket
83         bool readln(std::string &);
84         /// Write the string + '\n' to the socket
85         void writeln(std::string const &);
86 private:
87         /// File descriptor for the data socket
88         int fd_;
89         /// True if the connection is up
90         bool connected_;
91         /// buffer for input data
92         std::string buffer_;
93 };
94
95 /// Implementation is in lyx_main.C
96 extern LyXServerSocket & theLyXServerSocket();
97
98
99 } // namespace lyx
100
101 #endif // LYXSOCKET_H