private void ServiceSocket(StreamSocket socket)
{
// Sleep a thread on the socket
new Task(async () =>
{
try
{
// The the listener someone connected
m_listener.OnConnect();
// Make the readers and writers
DataReader reader = new DataReader(socket.InputStream);
DataWriter writer = new DataWriter(socket.OutputStream);
if(m_mode == CommmandServerMode.Client)
{
// If this is the client make sure we push the writer out.
m_clientDataWriter = writer;
}
// Loop
while (true)
{
// Wait for a command
Command cmd = await WaitForCommand(reader);
// Send the command to the consumer
Command response = m_listener.OnCommand(cmd);
// If the gave a message back, send it.
if(response != null)
{
await InternalSendMessage(response, writer);
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("The socket listener hit an exception: " + e.Message);
}
// Call disconnect and clean up.
m_socket = null;
m_clientDataWriter = null;
m_listener.OnDisconnected();
}).Start();
}