public static async Task <StreamSocketConnection> AcceptConnectionAsync(StreamSocket socket,
                                                                                IObjectSerializer serializer)
        {
            // Receive connection request, send connection response
            using (var reader = new DataReader(socket.InputStream))
                using (var writer = new DataWriter(socket.OutputStream))
                {
                    var request = await serializer.DeserializeFromStreamAsync(reader, CancellationToken.None)
                                  as ConnectionRequestMessage;

                    if (request == null)
                    {
                        // Wrong message received => reject connection
                        await serializer.SerializeToStreamAsync(new ConnectionResponseMessage(null), writer);

                        socket.Dispose();
                        return(null);
                    }
                    else
                    {
                        // Accept connection request
                        var connectionId = "SSC_" + Guid.NewGuid();
                        await serializer.SerializeToStreamAsync(new ConnectionResponseMessage(connectionId), writer);

                        reader.DetachStream();
                        writer.DetachStream();
                        var connection = new StreamSocketConnection(connectionId, socket, serializer);
                        return(connection);
                    }
                }
        }
Example #2
0
 public StreamSocketRequest(int id, object message, StreamSocketConnection connection)
 {
     RequestId   = id;
     Message     = message;
     _connection = connection;
 }