]> git.lyx.org Git - lyx.git/blob - src/ServerSocket.h
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / ServerSocket.h
1 // -*- C++ -*-
2 /**
3  * \file ServerSocket.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 LYXSERVERSOCKET_H
15 #define LYXSERVERSOCKET_H
16
17 #include "support/FileName.h"
18
19 #include <string>
20 #include <map>
21 #include <memory>
22
23
24 namespace lyx {
25
26 class LyXDataSocket;
27
28
29 /** Sockets can be in two states: listening and connected.
30  *  Connected sockets are used to transfer data, and will therefore
31  *  be called Data Sockets. Listening sockets are used to create
32  *  Data Sockets when clients connect, and therefore will be called
33  * Server Sockets.
34
35  * This class encapsulates local (unix) server socket operations and
36  * manages LyXDataSockets objects that are created when clients connect.
37  */
38 class ServerSocket {
39 public:
40         ///
41         ServerSocket(support::FileName const &);
42         ///
43         ~ServerSocket();
44         /// Address of the local socket
45         std::string const address() const;
46         /// To be called when there is activity in the server socket
47         void serverCallback();
48         /// To be called when there is activity in the data socket
49         void dataCallback(int fd);
50 private:
51         ///
52         void writeln(std::string const &);
53         /// File descriptor for the server socket
54         int fd_;
55         /// Stores the socket filename
56         support::FileName address_;
57         /// Maximum number of simultaneous clients
58         enum {
59                 MAX_CLIENTS = 10
60         };
61         /// All connections
62         std::map<int, std::shared_ptr<LyXDataSocket>> clients;
63 };
64
65
66 /** This class encapsulates data socket operations.
67  *  It provides read and write IO operations on the socket.
68  */
69 class LyXDataSocket {
70 public:
71         ///
72         LyXDataSocket(int fd);
73         ///
74         ~LyXDataSocket();
75         /// Connection status
76         bool connected() const;
77         /// Line buffered input from the socket
78         bool readln(std::string &);
79         /// Write the string + '\n' to the socket
80         void writeln(std::string const &);
81 private:
82         /// File descriptor for the data socket
83         int fd_;
84         /// True if the connection is up
85         bool connected_;
86         /// buffer for input data
87         std::string buffer_;
88 };
89
90 /// Implementation is in LyX.cpp
91 extern ServerSocket & theServerSocket();
92
93
94 } // namespace lyx
95
96 #endif // LYXSERVERSOCKET_H