HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FS_ServerSocketListener.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: FS_ServerSocketListener.h ( FS Library, C++ )
7  *
8  * COMMENTS:
9  * This event generator opens a socket and waits for connections.
10  * When a client makes a connection and sends data, a callback
11  * is called. Note that multiple active connections can exist at once.
12  */
13 
14 #ifndef __FS_ServerSocketListener_h__
15 #define __FS_ServerSocketListener_h__
16 
17 #include "FS_API.h"
18 #include <UT/UT_String.h>
19 #include <UT/UT_SysClone.h>
20 #include "FS_EventGenerator.h"
22 class UT_NetSocket;
23 
25 {
26 public:
27  // Use this static method to create a instance of this class.
28  // Note that this method might return null if the socket could not be
29  // created (eg. if the port is already in use).
30  // callback: this function will be called when data has been read from
31  // the socket. callback_data will be passed to this function.
32  // ip_mask: restricts who is allowed to connect to this socket. If
33  // it is null, it defaults to +.+.+.+.
34  // remap_privileged_ports: if set, port numbers will be mapped into
35  // the range of unprivileged port numbers.
36  static FS_ServerSocketListener *createSocketListener(int port,
38  void *callback_data = 0,
39  const char *ip_mask = 0,
40  bool remap_privileged_ports = false);
41  ~FS_ServerSocketListener() override;
42 
43  const char *getClassName() const override
44  { return "FS_ServerSocketListener"; }
45 
46  // If the original port number was privileged and was remapped, getPort()
47  // will return the actual port number that was used and getRequestedPort()
48  // will return the port number that was requested.
49  int getPort() const;
50  int getRequestedPort() const { return myRequestedPort; }
51 
52  // The IP mask determines which machines are allowed to connect to the
53  // socket.
54  const UT_String &getIPMask() const { return myIPMask; }
55 
56  // The listener can be told to close the server socket when a child
57  // process exits. To turn off this behaviour, use INVALID_PID for
58  // the pid. If delete_on_child_exit is set, this listener will also
59  // uninstall and delete itself when the child exits. If a callback is
60  // given, it will be called when we delete ourselves because the child
61  // process is no longer running.
62  //
63  // Note that if the listener is waiting on a child process that has not
64  // exited and someone else deletes the listener, the callback will be
65  // called with listener_forced_to_close set to true.
66  typedef void (*ChildExitedCallback)(FS_ServerSocketListener &,
67  void *callback_data,
68  bool listener_forced_to_close);
69  void closeSocketOnChildExit(
70  pid_t child_pid,
71  bool delete_on_child_exit = false,
72  ChildExitedCallback child_exited_callback = 0,
73  void *callback_data = 0);
74 
75  // This method is called from the wrapper class to see if events are
76  // waiting.
77  bool areEventsWaiting() override;
78 
79  // When there is data on the socket (ie. some is trying to connect)
80  // to it, this method is called.
81  int processEvents() override;
82 
83  // Override FS_EventGenerator's getFileDescriptor() method to return the
84  // file descriptor of the server socket.
85  int getFileDescriptor() override;
86 
87  // Override FS_EventGenerator's getPollTime() method. If the listener
88  // is waiting for a child to exit, it will return a positive poll time
89  // so it can check if the child is still running. Otherwise, it will
90  // return a negative poll time, so it only processes events if a socket
91  // connection is attempted.
92  int getPollTime() override;
93 
94 protected:
95  // This protected constructor is called by the createSocketListener()
96  // static method.
98  int requested_port,
100  void *callback_data,
101  const char *ip_mask);
102 
103  // Subclasses may need access to the server socket.
104  UT_NetSocket *getServerSocket() { return myServerSocket; }
105 
106  // Subclasses should override this method to create the correct type of
107  // FS_ConnectedSocketListener subclass. Since the connected socket
108  // listener will delete itself when the connection is closed, this
109  // method does not return anything.
110  virtual void createConnectedSocketListener();
111 
112 private:
113  void closeServerSocket();
114  void acceptConnectionFromClient();
115  bool shouldStopBecauseChildHasExited();
116 
117  // Data:
118  UT_NetSocket *myServerSocket;
119  int myRequestedPort;
120  FS_ConnectedSocketListener::Callback myDataReadyCallback;
121  void *myDataReadyCallbackData;
122  UT_String myIPMask;
123 
124  pid_t myChildPid;
125  bool myDeleteOnChildExit;
126  ChildExitedCallback myChildExitedCallback;
127  void *myChildExitedCallbackData;
128 };
129 
130 #endif
virtual int getFileDescriptor()
void
Definition: png.h:1083
virtual bool areEventsWaiting()
virtual int getPollTime()
virtual int processEvents()=0
const UT_String & getIPMask() const
const char * getClassName() const override
void(* Callback)(void *callback_data, const char *data, int length, UT_NetSocket &connection_socket)
#define FS_API
Definition: FS_API.h:10