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