- bool ok = connection->OnStopAdvise (topic_name, item);
- if (ok)
- codec->Write8(IPC_ADVISE_STOP);
- else
- codec->Write8(IPC_FAIL);
-
- break;
- }
- case IPC_POKE: {
- wxDataFormat format;
- size_t size;
- char *data;
-
- item = codec->ReadString();
- format = (wxDataFormat)codec->Read8();
- size = codec->Read32();
- data = new char[size];
- codec->Read(data, size);
-
- connection->OnPoke (topic_name, item, data, size, format);
-
- delete [] data;
-
- break;
- }
- case IPC_REQUEST: {
- wxDataFormat format;
-
- item = codec->ReadString();
- format = (wxDataFormat)codec->Read8();
-
- int user_size = -1;
- char *user_data = connection->OnRequest (topic_name, item, &user_size, format);
-
- if (user_data) {
- codec->Write8(IPC_REQUEST_REPLY);
- if (user_size != -1) {
- codec->Write32(user_size);
- codec->Write(user_data, user_size);
- } else
- codec->WriteString(user_data);
- } else
- codec->Write8(IPC_FAIL);
-
- break;
- }
- case IPC_DISCONNECT: {
- sock.Close();
- connection->OnDisconnect();
- break;
- }
- default:
- codec->Write8(IPC_FAIL);
- break;
- }
+ // This socket is being deleted; skip this event
+ if (!connection)
+ return;
+
+ // We lost the connection: destroy everything
+ if (evt == wxSOCKET_LOST)
+ {
+ sock->Notify(false);
+ sock->Close();
+ connection->OnDisconnect();
+ return;
+ }
+
+ // Receive message number.
+ wxIPCSocketStreams * const streams = connection->m_streams;
+
+ const wxString topic = connection->m_topic;
+ wxString item;
+
+ bool error = false;
+
+ const int msg = streams->Read8();
+ switch ( msg )
+ {
+ case IPC_EXECUTE:
+ {
+ wxIPCFormat format;
+ size_t size wxDUMMY_INITIALIZE(0);
+ void * const
+ data = streams->ReadFormatData(connection, &format, &size);
+ if ( data )
+ connection->OnExecute(topic, data, size, format);
+ else
+ error = true;
+ }
+ break;
+
+ case IPC_ADVISE:
+ {
+ item = streams->ReadString();
+
+ wxIPCFormat format;
+ size_t size wxDUMMY_INITIALIZE(0);
+ void * const
+ data = streams->ReadFormatData(connection, &format, &size);
+
+ if ( data )
+ connection->OnAdvise(topic, item, data, size, format);
+ else
+ error = true;
+ }
+ break;
+
+ case IPC_ADVISE_START:
+ {
+ item = streams->ReadString();
+
+ IPCOutput(streams).Write8(connection->OnStartAdvise(topic, item)
+ ? IPC_ADVISE_START
+ : IPC_FAIL);
+ }
+ break;
+
+ case IPC_ADVISE_STOP:
+ {
+ item = streams->ReadString();
+
+ IPCOutput(streams).Write8(connection->OnStopAdvise(topic, item)
+ ? IPC_ADVISE_STOP
+ : IPC_FAIL);
+ }
+ break;
+
+ case IPC_POKE:
+ {
+ item = streams->ReadString();
+ wxIPCFormat format = (wxIPCFormat)streams->Read8();
+
+ size_t size wxDUMMY_INITIALIZE(0);
+ void * const data = streams->ReadData(connection, &size);
+
+ if ( data )
+ connection->OnPoke(topic, item, data, size, format);
+ else
+ error = true;
+ }
+ break;
+
+ case IPC_REQUEST:
+ {
+ item = streams->ReadString();
+
+ wxIPCFormat format = (wxIPCFormat)streams->Read8();
+
+ size_t user_size = wxNO_LEN;
+ const void *user_data = connection->OnRequest(topic,
+ item,
+ &user_size,
+ format);
+
+ if ( !user_data )
+ {
+ IPCOutput(streams).Write8(IPC_FAIL);
+ break;
+ }
+
+ IPCOutput out(streams);
+ out.Write8(IPC_REQUEST_REPLY);
+
+ if ( user_size == wxNO_LEN )
+ {
+ switch ( format )
+ {
+ case wxIPC_TEXT:
+ case wxIPC_UTF8TEXT:
+ user_size = strlen((const char *)user_data) + 1; // includes final NUL
+ break;
+ case wxIPC_UNICODETEXT:
+ user_size = (wcslen((const wchar_t *)user_data) + 1) * sizeof(wchar_t); // includes final NUL
+ break;
+ default:
+ user_size = 0;
+ }
+ }
+
+ out.WriteData(user_data, user_size);
+ }
+ break;
+
+ case IPC_DISCONNECT:
+ sock->Notify(false);
+ sock->Close();
+ connection->SetConnected(false);
+ connection->OnDisconnect();
+ break;
+
+ case IPC_FAIL:
+ wxLogDebug("Unexpected IPC_FAIL received");
+ error = true;
+ break;
+
+ default:
+ wxLogDebug("Unknown message code %d received.", msg);
+ error = true;
+ break;
+ }
+
+ if ( error )
+ IPCOutput(streams).Write8(IPC_FAIL);