00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Luke Moore 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: FS_ServerSocketListener.h ( FS Library, C++ ) 00015 * 00016 * COMMENTS: 00017 * This event generator opens a socket and waits for connections. 00018 * When a client makes a connection and sends data, a callback 00019 * is called. Note that multiple active connections can exist at once. 00020 */ 00021 00022 #ifndef __FS_ServerSocketListener_h__ 00023 #define __FS_ServerSocketListener_h__ 00024 00025 #include "FS_API.h" 00026 #include <UT/UT_String.h> 00027 #include <UT/UT_SysClone.h> 00028 #include "FS_EventGenerator.h" 00029 #include "FS_ConnectedSocketListener.h" 00030 class UT_NetSocket; 00031 00032 class FS_API FS_ServerSocketListener : public FS_EventGenerator 00033 { 00034 public: 00035 // Use this static method to create a instance of this class. 00036 // Note that this method might return null if the socket could not be 00037 // created (eg. if the port is already in use). 00038 // callback: this function will be called when data has been read from 00039 // the socket. callback_data will be passed to this function. 00040 // ip_mask: restricts who is allowed to connect to this socket. If 00041 // it is null, it defaults to +.+.+.+. 00042 // remap_privileged_ports: if set, port numbers will be mapped into 00043 // the range of unprivileged port numbers. 00044 static FS_ServerSocketListener *createSocketListener(int port, 00045 FS_ConnectedSocketListener::Callback callback, 00046 void *callback_data = 0, 00047 const char *ip_mask = 0, 00048 bool remap_privileged_ports = false); 00049 virtual ~FS_ServerSocketListener(); 00050 00051 virtual const char *getClassName() const 00052 { return "FS_ServerSocketListener"; } 00053 00054 // If the original port number was privileged and was remapped, getPort() 00055 // will return the actual port number that was used and getRequestedPort() 00056 // will return the port number that was requested. 00057 int getPort() const; 00058 int getRequestedPort() const { return myRequestedPort; } 00059 00060 // The IP mask determines which machines are allowed to connect to the 00061 // socket. 00062 const UT_String &getIPMask() const { return myIPMask; } 00063 00064 // The listener can be told to close the server socket when a child 00065 // process exits. To turn off this behaviour, use INVALID_PID for 00066 // the pid. If delete_on_child_exit is set, this listener will also 00067 // uninstall and delete itself when the child exits. If a callback is 00068 // given, it will be called when we delete ourselves because the child 00069 // process is no longer running. 00070 // 00071 // Note that if the listener is waiting on a child process that has not 00072 // exited and someone else deletes the listener, the callback will be 00073 // called with listener_forced_to_close set to true. 00074 typedef void (*ChildExitedCallback)(FS_ServerSocketListener &, 00075 void *callback_data, 00076 bool listener_forced_to_close); 00077 void closeSocketOnChildExit( 00078 pid_t child_pid, 00079 bool delete_on_child_exit = false, 00080 ChildExitedCallback child_exited_callback = 0, 00081 void *callback_data = 0); 00082 00083 // This method is called from the wrapper class to see if events are 00084 // waiting. 00085 virtual bool areEventsWaiting(); 00086 00087 // When there is data on the socket (ie. some is trying to connect) 00088 // to it, this method is called. 00089 virtual int processEvents(); 00090 00091 // Override FS_EventGenerator's getFileDescriptor() method to return the 00092 // file descriptor of the server socket. 00093 virtual int getFileDescriptor(); 00094 00095 // Override FS_EventGenerator's getPollTime() method. If the listener 00096 // is waiting for a child to exit, it will return a positive poll time 00097 // so it can check if the child is still running. Otherwise, it will 00098 // return a negative poll time, so it only processes events if a socket 00099 // connection is attempted. 00100 virtual int getPollTime(); 00101 00102 protected: 00103 // This protected constructor is called by the createSocketListener() 00104 // static method. 00105 FS_ServerSocketListener(UT_NetSocket *server_socket, 00106 int requested_port, 00107 FS_ConnectedSocketListener::Callback callback, 00108 void *callback_data, 00109 const char *ip_mask); 00110 00111 // Subclasses may need access to the server socket. 00112 UT_NetSocket *getServerSocket() { return myServerSocket; } 00113 00114 // Subclasses should override this method to create the correct type of 00115 // FS_ConnectedSocketListener subclass. Since the connected socket 00116 // listener will delete itself when the connection is closed, this 00117 // method does not return anything. 00118 virtual void createConnectedSocketListener(); 00119 00120 private: 00121 void closeServerSocket(); 00122 void acceptConnectionFromClient(); 00123 bool shouldStopBecauseChildHasExited(); 00124 00125 // Data: 00126 UT_NetSocket *myServerSocket; 00127 int myRequestedPort; 00128 FS_ConnectedSocketListener::Callback myDataReadyCallback; 00129 void *myDataReadyCallbackData; 00130 UT_String myIPMask; 00131 00132 pid_t myChildPid; 00133 bool myDeleteOnChildExit; 00134 ChildExitedCallback myChildExitedCallback; 00135 void *myChildExitedCallbackData; 00136 }; 00137 00138 #endif
1.5.9