예제 #1
0
        public void OnReceive(IConnectionChannel channel, IConnectionSession session, byte[] bytes)
        {
            var    factory    = channel.Factory;
            var    dispatcher = channel.Dispatcher;
            string crid       = null;

            try
            {
                var message = Encoding.UTF8.GetString(bytes);

                MessageLogger.LogDebug($"Received: {message}");

                var isError = false;
                var wrapper = factory.DeserializeMessage(message, out crid, ref isError);

                if (isError)
                {
                    return;
                }


                if (!dispatcher.Dispatch(session, wrapper, (r, rc) => channel.Send(session, new ResponseMessage(r, r.GetType().Name, crid, (byte)rc))))
                {
                    channel.SendError(session, crid);
                }
            }
            catch (Exception e)
            {
                channel.SendError(session, crid);
                Logger.Log("ConnectionService ex:", e);
            }
        }
예제 #2
0
        // TODO why here ?
        private void SendSignedNewLedgerNotification(IConnectionSession session)
        {
            var signedLedger = LedgerService.LedgerManager.GetSignedLedger();
            var notification = NotificationHelper.CreateSignedNewLedgerNotification(signedLedger);

            ConnectionService.BlockchainChannel.Send(session, notification);
        }
예제 #3
0
 public void ShouldOpen(IConnectionSession session)
 {
     if (shouldOpen(session))
     {
         session.Session.OpenChannel(Channel);
     }
 }
예제 #4
0
        public void Send(IConnectionSession session, NotificationMessage message)
        {
            var serialized = Factory.SerializeNotification(message);

            messageLogger.LogDebug($"Sent: {serialized}");

            var bytes = Encoding.UTF8.GetBytes(serialized);

            Channel.Send(session.Session, bytes);
        }
예제 #5
0
        public void Send(IConnectionSession session, ErrorMessage message)
        {
            var responseMessage = Factory.SerializeError(message);

            messageLogger.LogDebug($"Sent: {responseMessage}");

            var bytes = Encoding.UTF8.GetBytes(responseMessage);

            Channel.Send(session.Session, bytes);
        }
예제 #6
0
 void OnSessionEnded(IConnectionSession connection)
 {
     if (_contexts.TryRemove(connection, out IInstanceContext context))
     {
         this.SessionInstanceRemoved?.Invoke(this, context);
     }
     else
     {
         //TODO log info
     }
 }
예제 #7
0
        private bool TryRequest(IConnectionSession session, long incoming, long next)
        {
            if (preventHeight.HasValue && preventHeight.Value == next)
            {
                return(false);
            }

            // if incoming height is next or greater than next then request next
            if (next <= incoming)
            {
                targetHeight = incoming;
                ConnectionService.BlockchainChannel.Send(session, RequestHelper.CreateGetSignedLedgerRequest(next));
                logger.Log($"CatchupManager : Requesting [{next} of {incoming}] ");
                preventHeight = next;
                return(true);
            }
            return(false);
        }
예제 #8
0
 public SignedNewLedgerCommand(SignedNewLedger data, IConnectionSession session) : base(data)
 {
     this.session = session;
 }
예제 #9
0
 // TODO Change name
 // we need to see if an incoming height is ahead of us
 public bool TryProcessIncomingHeight(IConnectionSession session, long incoming)
 {
     return(TryRequest(session, incoming, LedgerService.LedgerManager.GetNextHeight()));
 }
예제 #10
0
 // TODO Change name
 // after we receive a ledger and we catch up
 public bool TryProcessHeight(IConnectionSession session)
 {
     return(TryProcessIncomingHeight(session, targetHeight));
 }
예제 #11
0
 public void Send(IConnectionSession session, Request request)
 {
     Send(session, new RequestMessage(request, request.GetType().Name, Guid.NewGuid().ToString("N")));
 }
예제 #12
0
 public void SendError(IConnectionSession session, string crid)
 {
     Send(session, new ErrorMessage(crid, (byte)ResultCode.Failed));
 }