Hello,
I have an editor plugin which sets up a listener socket for third party clients to communicate with (on the same machine). The plugin fails to clean up the connection and the sockets on shutdown. The problem specifically arises when I do a restart of the Editor (with the same project or a new one which has the same plugin enabled). There is no problem when I quit UE4Editor altogether and then restart manually
UDP
I have the following code in my StartupModule function for UDP:
ListenerSocket = FUdpSocketBuilder(TEXT("MySocket"))
.BoundToEndpoint(DEFAULT_ENDPOINT)
.Build();
mySocketReceiver = new FUdpSocketReceiver(Client, FTimespan(0, 1, 0), TEXT("MyUDPSocketReceiver"));
mySocketReceiver->OnDataReceived().BindRaw(this, &FMyPlugin::OnDataReceived);
ShutdownModule function for UDP:
if (ListenerSocket != NULL)
{
ListenerSocket->Close();
ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ListenerSocket);
ListenerSocket = NULL;
}
mySocketReceiver->Stop();
delete mySocketReceiver;
mySocketReceiver = NULL;
I’ve even tried putting this at the end:
ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->Shutdown();
The plugin does call my shutdown function when the editor restarts but when the new editor launches and tries to re-bind (in the startup module function) to the same address/port, it fails to create a socket through FUdpSocketBuilder, failing on Socket.Bind.
On further investigation using Microsoft’s TCPView. I can see that the previous connection isn’t terminated and the address/port is bound to the previous editor instance’s connection.
(Note: I’ve also tried adding the AsReusable attribute to FUdpSocketBuilder).
TCP
I’ve seen similar behaviour in a TCP listener implementation. The code was as follows:
StartupModule for TCP
TcpListener = new FTcpListener(DEFAULT_ENDPOINT);
TcpListener->OnConnectionAccepted().BindRaw(this, &FMyPlugin::HandleConnectionAccepted);
ShutdownModule for TCP
if(Client != NULL)
{
Client->Close();
Client=NULL;
}
TcpListener->Stop();
delete TcpListener;
TcpListener = NULL;
In TCP’s case, the communication just fails silently (I assume the data is being sent to the previous editor process’s port). Our new editor instance doesn’t even receive the OnConnectionAccepted callback.