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