protected virtual void Close()
 {
     if (RpcCallContext.Current.HasSessionId)
     {
         RpcSession.KillSession(new Guid(RpcCallContext.Current.SessionId.ToByteArray()));
     }
 }
 public static void KillSession(RpcSession session)
 {
     if (session == null)
     {
         return;
     }
     lock (_sessions)
         _sessions.Remove(session._key);
     session.Dispose();
 }
Beispiel #3
0
        protected virtual RpcMultiPartResponse MultiPartMessage(RpcMultiPartRequest request, ExtensionRegistry registry)
        {
            RpcSession session   = RpcCallContext.Session;
            string     messageId = request.TransactionId.ToBase64();
            Stream     message;

            if (!session.TryGetValue(messageId, out message))
            {
                if (request.CurrentPosition != 0)
                {
                    throw new InvalidDataException("The TransactionId is not valid.");
                }
                session.Add(messageId, message = CreateStream(request.TotalBytes));
                message.SetLength(request.TotalBytes);
            }
            if (request.MessageStatus == RpcMultiPartRequest.Types.RpcMessageStatus.CANCEL)
            {
                message.Dispose();
                session.Remove(messageId);
                return(RpcMultiPartResponse.CreateBuilder().SetContinue(false).Build());
            }
            if (message.Position != request.CurrentPosition || message.Length != request.TotalBytes ||
                request.BytesSent != request.PayloadBytes.Length)
            {
                throw new InvalidDataException();
            }
            request.PayloadBytes.WriteTo(message);

            if (request.MessageStatus == RpcMultiPartRequest.Types.RpcMessageStatus.COMPLETE)
            {
                using (message)
                {
                    session.Remove(messageId);
                    if (message.Position != request.TotalBytes)
                    {
                        throw new InvalidDataException();
                    }
                    message.Position = 0;
                    byte[] response =
                        next.CallMethod(request.MethodName, CodedInputStream.CreateInstance(message), registry).
                        ToByteArray();
                    return(RpcMultiPartResponse.CreateBuilder()
                           .SetResponseBytes(ByteString.CopyFrom(response))
                           .SetContinue(true)
                           .Build());
                }
            }
            return(RpcMultiPartResponse.CreateBuilder()
                   .SetContinue(true)
                   .Build());
        }
        public static RpcSession GetSession(Guid sessionId)
        {
            if (!EnableSessions)
            {
                throw new InvalidOperationException("Session state disabled.");
            }

            RpcSession session;

            lock (_sessions)
            {
                if (!_sessions.TryGetValue(sessionId, out session))
                {
                    _sessions.Add(sessionId, session = new RpcSession(sessionId));
                }
                session._lastUsed = DateTime.Now;
            }
            return(session);
        }