+ _CLIENT_ONREQUEST_ID = 1000,
+ _SERVER_ONREQUEST_ID
+};
+
+// --------------------------------------------------------------------------
+// wxTCPEventHandlerModule (private class)
+// --------------------------------------------------------------------------
+
+class wxTCPEventHandlerModule : public wxModule
+{
+public:
+ wxTCPEventHandlerModule() : wxModule() { }
+
+ // get the global wxTCPEventHandler creating it if necessary
+ static wxTCPEventHandler& GetHandler()
+ {
+ if ( !ms_handler )
+ ms_handler = new wxTCPEventHandler;
+
+ return *ms_handler;
+ }
+
+ // as ms_handler is initialized on demand, don't do anything in OnInit()
+ virtual bool OnInit() { return true; }
+ virtual void OnExit() { wxDELETE(ms_handler); }
+
+private:
+ static wxTCPEventHandler *ms_handler;
+
+ DECLARE_DYNAMIC_CLASS(wxTCPEventHandlerModule)
+ DECLARE_NO_COPY_CLASS(wxTCPEventHandlerModule)
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxTCPEventHandlerModule, wxModule)
+
+wxTCPEventHandler *wxTCPEventHandlerModule::ms_handler = NULL;
+
+// --------------------------------------------------------------------------
+// wxIPCSocketStreams
+// --------------------------------------------------------------------------
+
+#define USE_BUFFER
+
+// this class contains the various (related) streams used by wxTCPConnection
+// and also provides a way to read from the socket stream directly
+//
+// for writing to the stream use the IPCOutput class below
+class wxIPCSocketStreams
+{
+public:
+ // ctor initializes all the streams on top of the given socket
+ //
+ // note that we use a bigger than default buffer size which matches the
+ // typical Ethernet MTU
+ wxIPCSocketStreams(wxSocketBase& sock)
+ : m_socketStream(sock),
+#ifdef USE_BUFFER
+ m_bufferedOut(m_socketStream, 1500),
+#else
+ m_bufferedOut(m_socketStream),
+#endif
+ m_dataIn(m_socketStream),
+ m_dataOut(m_bufferedOut)
+ {
+ }
+
+ // expose the IO methods needed by IPC code (notice that writing is only
+ // done via IPCOutput)
+
+ // flush output
+ void Flush()
+ {
+#ifdef USE_BUFFER
+ m_bufferedOut.Sync();
+#endif
+ }
+
+ // simple wrappers around the functions with the same name in
+ // wxDataInputStream
+ wxUint8 Read8()
+ {
+ Flush();
+ return m_dataIn.Read8();
+ }
+
+ wxUint32 Read32()
+ {
+ Flush();
+ return m_dataIn.Read32();
+ }
+
+ wxString ReadString()
+ {
+ Flush();
+ return m_dataIn.ReadString();
+ }
+
+ // read arbitrary (size-prepended) data
+ //
+ // connection parameter is needed to call its GetBufferAtLeast() method
+ void *ReadData(wxConnectionBase *conn, size_t *size)
+ {
+ Flush();
+
+ wxCHECK_MSG( conn, NULL, "NULL connection parameter" );
+ wxCHECK_MSG( size, NULL, "NULL size parameter" );
+
+ *size = Read32();
+
+ void * const data = conn->GetBufferAtLeast(*size);
+ wxCHECK_MSG( data, NULL, "IPC buffer allocation failed" );
+
+ m_socketStream.Read(data, *size);
+
+ return data;
+ }
+
+ // same as above but for data preceded by the format
+ void *
+ ReadFormatData(wxConnectionBase *conn, wxIPCFormat *format, size_t *size)
+ {
+ wxCHECK_MSG( format, NULL, "NULL format parameter" );
+
+ *format = static_cast<wxIPCFormat>(Read8());
+
+ return ReadData(conn, size);
+ }
+
+
+ // these methods are only used by IPCOutput and not directly
+ wxDataOutputStream& GetDataOut() { return m_dataOut; }
+ wxOutputStream& GetUnformattedOut() { return m_bufferedOut; }
+
+private:
+ // this is the low-level underlying stream using the connection socket
+ wxSocketStream m_socketStream;
+
+ // the buffered stream is used to avoid writing all pieces of an IPC
+ // request to the socket one by one but to instead do it all at once when
+ // we're done with it
+#ifdef USE_BUFFER
+ wxBufferedOutputStream m_bufferedOut;
+#else
+ wxOutputStream& m_bufferedOut;
+#endif
+
+ // finally the data streams are used to be able to write typed data into
+ // the above streams easily
+ wxDataInputStream m_dataIn;
+ wxDataOutputStream m_dataOut;
+
+ DECLARE_NO_COPY_CLASS(wxIPCSocketStreams)