async static Task Run(INetNode node)
        {
            node.Start();
            Console.WriteLine("Input server endpoint in form of ipaddress:port");
            string[] addressAndPort = Console.ReadLine().Split(':');
            var      endpoint       = new IPEndPoint(IPAddress.Parse(addressAndPort[0]), int.Parse(addressAndPort[1]));

            //before doing any network comms we should connect to some server node
            using (INetChannel channel = await node.Connect(endpoint))
            {
                await Task.Yield();

                Console.WriteLine("Enter your name");
                string name = Console.ReadLine();

                Console.WriteLine("Enter chat room name");
                string roomName = Console.ReadLine();

                //using connected channel we can send in request/reply pattern
                JoinRoomResponse response = await channel.SendRequestAsync <JoinRoomResponse>(
                    new JoinRoom
                {
                    ClientName = name,
                    RoomName   = roomName
                });

                if (response.RetCode == JoinRoomRetCode.NameIsTaken)
                {
                    Console.WriteLine("Name is taken");
                    return;
                }

                Console.WriteLine("You are connected, say something or enter exit");

                bool exit = false;
                while (!exit)
                {
                    await Task.Yield(); //client uses single threaded processing, so it's better noto block it with Console.Readline

                    string text = Console.ReadLine();
                    if (text == "exit")
                    {
                        exit = true;
                    }
                    else
                    {
                        //or we can send just one way messages
                        channel.Send(new Say {
                            Text = text
                        });
                    }
                }
            }
        }
        async static Task Run(INetNode node)
        {
            node.Start();
            Console.WriteLine("Input server endpoint in form of ipaddress:port");
            string[] addressAndPort = Console.ReadLine().Split(':');
            var endpoint = new IPEndPoint(IPAddress.Parse(addressAndPort[0]), int.Parse(addressAndPort[1]));

            //before doing any network comms we should connect to some server node
            using (INetChannel channel = await node.Connect(endpoint))
            {
                await Task.Yield();
                Console.WriteLine("Enter your name");
                string name = Console.ReadLine();

                Console.WriteLine("Enter chat room name");
                string roomName = Console.ReadLine();

                //using connected channel we can send in request/reply pattern
                JoinRoomResponse response = await channel.SendRequestAsync<JoinRoomResponse>(
                    new JoinRoom
                    {
                        ClientName = name,
                        RoomName = roomName
                    });

                if (response.RetCode == JoinRoomRetCode.NameIsTaken)
                {
                    Console.WriteLine("Name is taken");
                    return;
                }

                Console.WriteLine("You are connected, say something or enter exit");

                bool exit = false;
                while (!exit)
                {
                    await Task.Yield(); //client uses single threaded processing, so it's better noto block it with Console.Readline
                    string text = Console.ReadLine();
                    if (text == "exit")
                    {
                        exit = true;
                    }
                    else
                    {
                        //or we can send just one way messages
                        channel.Send(new Say {Text = text});
                    }
                }
            }
        }
Example #3
0
        async protected override Task <NetCommunicationClient> CreateClientAsync(string endpoint, CancellationToken cancellationToken)
        {
            var channel = await _netNode.Connect(endpoint, cancellationToken);

            return(new NetCommunicationClient(channel));
        }
        async Task ProcessEventHubEvent(EventData ev)
        {
            if ((DateTime.UtcNow - ev.EnqueuedTimeUtc).TotalDays > 1)
            {
                _logger.ReceivedExpiredMessage(ev.EnqueuedTimeUtc);
                return;
            }
            Message msg;

            if (_serializer.TryDeserialize(new ArraySegment <byte>(ev.GetBytes()), out msg))
            {
                var actorHeader = msg.GetHeader <ActorTargetHeader>();
                if (actorHeader != null)
                {
                    _logger.ReceivedMessageForActor(actorHeader.ActorKey, msg);

                    var actor            = _actors.GetOrAdd(actorHeader.ActorKey.Id, SpawnActor);
                    var opHeader         = msg.GetHeader <OperationHeader>();
                    var actorReplyHeader = msg.GetHeader <ActorDirectReplyHeader>();
                    if (opHeader != null && opHeader.Type == OperationType.Request && actorReplyHeader != null)
                    {
                        var     operationContext = new OperationContext(null, msg, opHeader.ActivityId);
                        Message reply            = null;
                        try
                        {
                            reply = await actor.Process(operationContext);
                        }
                        catch (ProcessingException ex)
                        {
                            reply = new ErrorMessage(ex.ErrorCode, ex.Message);
                        }

                        if (reply != null)
                        {
                            if (actorReplyHeader.ActorRef.Location.Equals(_serverNode.Address))
                            {
                                OnDirectReplyMessage(new OperationContext(null, reply, opHeader.ActivityId));
                            }
                            else
                            {
                                try
                                {
                                    INetChannel replyChannel = await _serverNode.Connect(actorReplyHeader.ActorRef.Location);

                                    replyChannel.Reply(operationContext, reply);
                                }
                                catch (Exception ex)
                                {
                                    _logger.FailedToSendReply(actorHeader.ActorKey, msg, ex);
                                }
                            }
                        }
                    }
                    else
                    {
                        var activityId = opHeader?.ActivityId ?? Guid.NewGuid();
                        try
                        {
                            await actor.Process(new OperationContext(null, msg, activityId));
                        }
                        catch (ProcessingException ex)
                        {
                            //log and skip
                        }
                    }
                }
                else
                {
                    _logger.ReceivedMessageWithoutActorHeader(_id, msg);
                }
            }
            else
            {
                _logger.FailedToDeserializeEvent(_id, ev.PartitionKey, ev.SequenceNumber, ev.Offset);
            }
        }