コード例 #1
0
        public async Task SendMessage(String stageId, RemoteStageMessage message)
        {
            message.Destination = stageId;
            message.Source      = _ownStageId;

            var transportMessage = _serializer.Serialize(message);

            await _socketServer.SendResponse(stageId, transportMessage);
        }
コード例 #2
0
        public async Task <ActorResponse> SendRemoteActorRequest(String stageId, ActorRequest request, TimeSpan?timeout = null)
        {
            if (request is LocalActorRequest)
            {
                request = ((LocalActorRequest)request).ToRemote(_serializer);
            }

            var message = new RemoteStageMessage()
            {
                ActorRequest = request,
                MessageType  = RemoteMessageType.ActorRequest
            };

            if (request.FireAndForget)
            {
                await _socketClient.SendRequest(stageId, _serializer.Serialize(message));

                return(new ActorResponse()
                {
                    Success = true
                });
            }
            else
            {
                var semaphore = new SemaphoreSlim(0, 1);

                var queueItem = _serverResponseBuffer[request.Id] = new Tuple <SemaphoreSlim, ActorResponse>(semaphore, null);

                try
                {
                    await _socketClient.SendRequest(stageId, _serializer.Serialize(message));

                    var responseArrived = await semaphore.WaitAsync(timeout ?? TimeSpan.FromMilliseconds(-1));

                    if (responseArrived)
                    {
                        if (_serverResponseBuffer.TryRemove(request.Id, out queueItem))
                        {
                            return(queueItem.Item2);
                        }
                    }

                    throw new TimeoutException();
                }
                finally
                {
                    _serverResponseBuffer.TryRemove(request.Id, out _);
                }
            }
        }
コード例 #3
0
        public Task ProcessPingRequest(string stageId, Ping ping)
        {
            var message = new RemoteStageMessage()
            {
                Ping        = ping,
                MessageType = RemoteMessageType.PingResponse
            };

            if (_paused == 0)
            {
                return(SendMessage(stageId, message));
            }
            else
            {
                return(Task.FromResult(0));
            }
        }
コード例 #4
0
        public async Task SendActorResponse(string stageId, ActorResponse response)
        {
            if (String.IsNullOrEmpty(stageId))
            {
                //message for local client
                _remoteClient.Value.ServerResponse(response);
            }
            else
            {
                var message = new RemoteStageMessage()
                {
                    MessageType   = RemoteMessageType.ActorResponse,
                    ActorResponse = response
                };

                await this.SendMessage(stageId, message);
            }
        }
コード例 #5
0
        public async Task <TimeSpan?> PingStage(String stageId)
        {
            var message = new RemoteStageMessage()
            {
                MessageType = RemoteMessageType.PingRequest,
                Ping        = new Ping()
            };


            var semaphore = _pingResponseBuffer[message.Ping.Id] = new SemaphoreSlim(0, 1);

            var sw = Stopwatch.StartNew();

            try
            {
                await _socketClient.SendRequest(stageId, _serializer.Serialize(message));

                if (await semaphore.WaitAsync(1000))
                {
                    sw.Stop();
                    return(sw.Elapsed);
                    //return TimeSpan.FromTicks(DateTimeOffset.UtcNow.Ticks - message.Ping.Timestamp);
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
            finally
            {
                _pingResponseBuffer.TryRemove(message.Ping.Id, out _);
                sw.Stop();
            }
        }