]> git.lyx.org Git - lyx.git/blob - src/lyxsocket.h
more cursor dispatch
[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 <string>
21 #include <set>
22
23 class LyXServerSocket;
24 class LyXDataSocket;
25
26 /** Sockets can be in two states: listening and connected.
27  *  Connected sockets are used to transfer data, and will therefore
28  *  be called Data Sockets. Listening sockets are used to create
29  *  Data Sockets when clients connect, and therefore will be called
30  * Server Sockets.
31
32  * This class encapsulates local (unix) server socket operations and
33  * manages LyXDataSockets objects that are created when clients connect.
34  */
35 class LyXServerSocket
36 {
37 public:
38         LyXServerSocket(LyXFunc *, std::string const &);
39         ~LyXServerSocket();
40         /// File descriptor of the socket
41         int fd() const;
42         /// Address of the local socket
43         std::string const & address() const;
44         /// To be called when there is activity in the server socket
45         void serverCallback();
46         /// To be called when there is activity in the data socket
47         void dataCallback(LyXDataSocket *);
48
49 private:
50         /// Close the connection to the argument client
51         void close(LyXDataSocket *);
52
53         LyXFunc * func;
54         /// File descriptor for the server socket
55         int fd_;
56         /// Stores the socket filename
57         std::string address_;
58         /// Maximum number of simultaneous clients
59         enum {
60                 MAX_CLIENTS = 10
61         };
62         /// All connections
63         std::set<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 {
72 public:
73         LyXDataSocket(LyXServerSocket *);
74         ~LyXDataSocket();
75         /// The object that allocated us
76         LyXServerSocket * server() const;
77         /// File descriptor of the connection
78         int fd() const;
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
86 private:
87         LyXServerSocket * server_;
88         /// File descriptor for the data socket
89         int fd_;
90         /// True if the connection is up
91         bool connected_;
92         /// buffer for input data
93         std::string buffer;
94 };
95
96 #endif // LYXSOCKET_H