public static Task <NetworkMessageMultiStream> Accept(
        int port,
        MonoBehaviour parent,
        MessageTransferLookUp lookUp
        )
    {
        IPAddress localAddr   = IPAddress.Parse("0.0.0.0");
        var       server      = new CancelableTcpListener(new ActivatableTCPListener(localAddr, port));
        var       taskSource  = new TaskCompletionSource <NetworkMessageMultiStream>();
        var       crossThread = new InboundCrossThread(taskSource, parent, server.GetCancellationTokenSource());

        Debug.Log("Delegating server port: " + port);
        Task.Factory
        .StartNew(() => Connect(server, crossThread, lookUp), server.GetCancellationTokenSource().Token)
        .ContinueWith((task) => {
            if (task.IsCanceled)
            {
                Debug.Log("Server dispatch has been shutdown");
            }
            else if (task.IsFaulted)
            {
                Debug.LogError("Exception in connection accept thread");
                Debug.LogError(task.Exception);
            }
            else
            {
                Debug.Log("not sure how this task shutdown");
            }
        });
        return(taskSource.Task);
    }
    private static void Connect(
        CancelableTcpListener server,
        InboundCrossThread crossThread,
        MessageTransferLookUp lookUp
        )
    {
        InboundMultiMessageSender       sender       = new InboundMultiMessageSender();
        InboundMultiMessageSubscription subscription = new InboundMultiMessageSubscription(server.GetCancellationTokenSource());

        server.Start();
        Debug.Log("Server dispatching connections");
        crossThread.AcceptStream(new NetworkMessageMultiStream(sender.GetSender(), subscription));
        while (true)
        {
            var guid = server.Accept();
            Debug.Log("Server connection established: " + guid);
            var client = server.GetClient(guid);
            crossThread.Schedule((parent) => {
                var clientSender       = ConnectionWrite.Write(client, lookUp);
                var clientSubscription = ConnectionRead.Read(client, parent, lookUp);
                sender.AddClientSender(guid, clientSender);
                subscription.AddClientSubscription(guid, clientSubscription);
            });
        }
    }
Exemple #3
0
    public static MessageHandler Write(CancelableTcpClient client, MessageTransferLookUp lookUp)
    {
        var messageData = NetworkMessageSerializer.Get(lookUp);

        return((opCode, toSend) => {
            int length = messageData.serialize(opCode, toSend);
            client.Write(messageData.buffer, 0, length);
            return length;
        });
    }
Exemple #4
0
    public static MessageSerializationData Get(MessageTransferLookUp lookUp)
    {
        var            opCodeBuf = new byte[2];
        var            lenBuf    = new byte[2];
        var            data      = new MessageSerializationData();
        MemoryStream   m         = new MemoryStream(data.buffer);
        var            writer    = new BinaryWriter(m);
        MessageHandler serialize = (ushort opCode, MessageTransferable toSerialize) => {
            m.Position = 4;
            lookUp.Serialize((ushort)opCode, writer, toSerialize);
            var newPosition = m.Position;
            var length      = newPosition - 4;
            m.Position = 0;
            writer.Write((ushort)length);
            writer.Write(opCode);
            return((ushort)(newPosition));
        };

        data.serialize = serialize;
        return(data);
    }
Exemple #5
0
    public static MessageSubscription Read(
        CancelableTcpClient client,
        MonoBehaviour parent,
        MessageTransferLookUp lookUp
        )
    {
        var crossThread = new ConnectionReadCrossThread(parent, lookUp, client.GetCancellationTokenSource());

        byte[] readBuffer = new byte[ushort.MaxValue];
        var    dataStream = new MemoryStream(readBuffer);
        var    reader     = new BinaryReader(dataStream);

        Task.Factory.StartNew(() => {
            while (true)
            {
                dataStream.Position        = 0;
                reader.BaseStream.Position = 0;
                int read = 0;
                while (read < 4)
                {
                    read = read + client.Read(readBuffer, read, 4 - read);
                }
                var dataLength = reader.ReadUInt16();
                var opCode     = reader.ReadUInt16();
                while (read < 4 + dataLength)
                {
                    read = read + client.Read(readBuffer, read, (dataLength + 4) - read);
                }
                var data = lookUp.Deserialize(opCode, reader);
                crossThread.Recieve(opCode, data);
            }
        }, client.GetCancellationTokenSource().Token).ContinueWith((task) => {
            Debug.LogError("exception in task");
            Debug.LogError(task.Exception);
        }, TaskContinuationOptions.OnlyOnFaulted);
        return(crossThread);;
    }
    public static NetworkMessageStream Open(int port, string address, MonoBehaviour parent, MessageTransferLookUp lookUp)
    {
        var            addr        = IPAddress.Parse(address);
        var            client      = new CancelableTcpClient(new TcpClient(address, port));
        var            messageData = NetworkMessageSerializer.Get(lookUp);
        var            recieve     = ConnectionRead.Read(client, parent, lookUp);
        MessageHandler send        = ConnectionWrite.Write(client, lookUp);

        return(new NetworkMessageStream(send, recieve));
    }
 public ConnectionReadCrossThread(MonoBehaviour parent, MessageTransferLookUp lookUp, CancellationTokenSource cancellation)
 {
     this.lookUp       = lookUp;
     this.cancellation = cancellation;
     parent.StartCoroutine(Update());
 }