Exemple #1
0
        public bool Update(T t, OnSession onSession)
        {
            bool         retVal = false;
            ISession     s      = null;
            ITransaction trans  = null;

            try
            {
                s     = hibernateOper.GetCurrentSession();
                trans = s.BeginTransaction();

                if (onSession != null)
                {
                    onSession(this, s);
                }

                T temp = Get(s, t);
                t.ModDate    = DateTime.Now;
                t.UploadDate = temp.UploadDate;
                t.TrashFlag  = temp.TrashFlag;
                t.ExHost     = temp.ExHost;
                t.Del        = false;

                s.Merge(t);

                trans.Commit();

                retVal = true;
            }
            catch (Exception e)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                throw e;
            }
            finally
            {
//                hibernateOper.Close(s);
            }

            return(retVal);
        }
Exemple #2
0
        public static void Exec(IHibernateOper hibernateOper, OnSession onSession)
        {
            ISession s = null;
            ITransaction trans = null;
            try
            {
                s = hibernateOper.GetCurrentSession();
                trans = s.BeginTransaction();

                onSession(s);
                
                trans.Commit();
            }
            catch (Exception e)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                throw e;
            }
        }
Exemple #3
0
        public static void Exec(IHibernateOper hibernateOper, OnSession onSession)
        {
            ISession     s     = null;
            ITransaction trans = null;

            try
            {
                s     = hibernateOper.GetCurrentSession();
                trans = s.BeginTransaction();

                onSession(s);

                trans.Commit();
            }
            catch (Exception e)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                throw e;
            }
        }
Exemple #4
0
        public string Add(T t, OnSession onSession)
        {
            string       retVal = "";
            ISession     s      = null;
            ITransaction trans  = null;

            try
            {
                s     = hibernateOper.GetCurrentSession();
                trans = s.BeginTransaction();

                if (onSession != null)
                {
                    onSession(this, s);
                }

                s.Save(t);

                trans.Commit();

                retVal = t.Id.ToString();
            }
            catch (Exception e)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                throw e;
            }
            finally
            {
                //                hibernateOper.Close(s);
            }

            return(retVal);
        }
Exemple #5
0
        /// <summary>
        /// Processes a received frame depending on frame type and current connection state
        /// </summary>
        /// <param name="frame">The frame to process</param>
        private void _processFrame(Frame frame)
        {
            // do NOT process anything if in a closed or errored state
            if (State == ConnectionState.End || State == ConnectionState.ConnectionError)
            {
                return;
            }

            switch (frame.Type)
            {
                #region Connection Frame Handling

            case FrameType.ConnectionStart:
            {
                if (State == ConnectionState.Start)
                {
                    Log.DebugFormat("[{0}] FrameType.ConnectionStart received", ID);

                    // decode connection descriptor content
                    var descriptor = BsonCodec.Decode <ConnectionDescriptor>(frame.Payload);

                    // store any data in the property - used by application to provide details about the connection
                    Data = descriptor.Data;

                    // is the protocol compatible
                    if (descriptor.ProtocolVersion.Major == Frame.ProtocolVersion.Major)
                    {
                        if (descriptor.MaxFrameSize < Frame.MaxFrameSize)
                        {
                            // protocol will use the smallest of the two max frame sizes
                            Frame.MaxFrameSize = descriptor.MaxFrameSize;
                        }

                        // update connection state
                        State = ConnectionState.ConnectionAcceptSent;

                        // send accept frame (version and (new) frame size will be returned)
                        _sendFrame(FrameType.ConnectionAccept, Frame.CreateConnectionAcceptFrame());
                    }
                    else
                    {
                        // set state
                        State = ConnectionState.End;

                        // reject the connection (description of why provided)
                        _sendFrame(FrameType.ConnectionReject, Frame.CreateConnectionRejectFrame("Protocol version incompatible"));

                        // should close here
                        _closeInternal();
                    }
                }
            } break;

            case FrameType.ConnectionAccept:
            {
                if (State == ConnectionState.ConnectionStartSent)
                {
                    Log.DebugFormat("[{0}] FrameType.ConnectionAccept received", ID);

                    // now in open state
                    State = ConnectionState.Open;

                    // send the open frame to the endpoint
                    _sendFrame(FrameType.ConnectionOpen, Frame.CreateConnectionOpenFrame());

                    // signal that the connection is open
                    _connectionOpen.Set();
                }
            } break;

            case FrameType.ConnectionReject:
            {
                if (State == ConnectionState.ConnectionStartSent)
                {
                    Log.DebugFormat("[{0}] FrameType.ConnectionReject received", ID);

                    // transition to end state
                    State = ConnectionState.End;

                    // close everything
                    _closeInternal();
                }
            } break;

            case FrameType.ConnectionOpen:
            {
                // used for both client and server endpoints
                if (State == ConnectionState.ConnectionAcceptSent || State == ConnectionState.ConnectionOpenSent)
                {
                    Log.DebugFormat("[{0}] FrameType.ConnectionOpen received", ID);

                    // connection is now open - set state
                    State = ConnectionState.Open;

                    // signal that connection is open
                    _connectionOpen.Set();
                }
            } break;

            case FrameType.ConnectionClose:
            {
                // valid in any state (except errored - but shoulnd't have received it in the first place)

                Log.DebugFormat("[{0}] FrameType.ConnectionClose received", ID);

                // close the connection
                State = ConnectionState.End;

                // close the physcial connection and any associated components
                _closeInternal();

                // raise the closed event
                OnClosed?.Invoke(this, null);
            } break;

                #endregion

                #region Session Frame Handling

            case FrameType.SessionStart:
            {
                var descriptor = BsonCodec.Decode <SessionDescriptor>(frame.Payload);

                // TODO: getting a null descriptor here!
                Log.DebugFormat("[{0}] FrameType.SessionStart received: ID [{1}], Name [{2}]", ID, frame.SessionID, descriptor.Name);

                // validate
                if (_pendingSessions.ContainsKey(frame.SessionID))
                {
                    Log.DebugFormat("[{0}] Received SessionStart from when one is already pending", ID);
                    return;
                }

                if (_activeSessions.ContainsKey(frame.SessionID))
                {
                    Log.DebugFormat("[{0}] Received SessionStart from when one is already active", ID);
                    return;
                }

                // create the incoming session and add to the pending dictionary
                var session = new Session(this, descriptor, frame.SessionID);
                _pendingSessions.Add(frame.SessionID, session);

                // notify application
                _ = Task.Run(() => OnSession?.Invoke(this, session));
            } break;

            case FrameType.SessionAccept:
            {
                Log.DebugFormat("[{0}] FrameType.SessionAccept received: ID [{1}]", ID, frame.SessionID);

                // move to active session but do not trigger Settle until Open received
                Session session;
                if (_pendingSessions.Remove(frame.SessionID, out session))
                {
                    // move session from pending to active
                    _activeSessions.Add(frame.SessionID, session);

                    Log.DebugFormat("[{0}] Session moved from pending to active: ID [{1}], Pending [{2}], Active [{3}]", ID, frame.SessionID, _pendingSessions.Count, _activeSessions.Count);

                    // send session open
                    _sendFrame(FrameType.SessionOpen, Frame.CreateSessionOpenFrame(frame.SessionID, null));
                }
            } break;

            case FrameType.SessionReject:
            {
                Log.DebugFormat("[{0}] FrameType.SessionReject received: ID [{1}]", ID, frame.SessionID);

                _pendingSessions.Remove(frame.SessionID);

                Log.DebugFormat("[{0}] Session removed from pending sessions: ID [{1}], Pending [{2}], Active [{3}]", ID, frame.SessionID, _pendingSessions.Count, _activeSessions.Count);
            } break;

            case FrameType.SessionOpen:
            {
                Log.DebugFormat("[{0}] FrameType.SessionOpen received: ID [{1}]", ID, frame.SessionID);

                Session session;
                if (_pendingSessions.Remove(frame.SessionID, out session))
                {
                    // move session from pending to active
                    _activeSessions.Add(frame.SessionID, session);

                    Log.DebugFormat("[{0}] Session moved from pending to active: ID [{1}], Pending [{2}], Active [{3}]", ID, frame.SessionID, _pendingSessions.Count, _activeSessions.Count);

                    // respond with session open
                    _sendFrame(FrameType.SessionOpen, Frame.CreateSessionOpenFrame(frame.SessionID, null));

                    // signal that the session is now Open
                    session.NotifySessionOpen();
                }
                else if (_activeSessions.ContainsKey(frame.SessionID))
                {
                    // signal that the session is now Open
                    _activeSessions[frame.SessionID].NotifySessionOpen();
                }
            }
            break;

            case FrameType.SessionClose:
            {
                Log.DebugFormat("[{0}] FrameType.SessionClose received: ID [{1}]", ID, frame.SessionID);

                Session session;
                if (_activeSessions.Remove(frame.SessionID, out session))
                {
                    // remove session from active collection
                    _activeSessions.Remove(frame.SessionID);

                    // call close on the session
                    session.Close();
                }
            } break;

                #endregion

                #region Message Frame Handling

            case FrameType.MessageSend:
            case FrameType.MessageAccept:
            case FrameType.MessageReject:
            {
                Log.DebugFormat("[{0}] {1} received", ID, frame.Type.ToString());

                var sessionId = frame.SessionID;
                if (_activeSessions.ContainsKey(sessionId))
                {
                    _activeSessions[sessionId].NotifyMessage(frame.Type, frame.MessageID, frame.Payload);
                }
            } break;

                #endregion

            default:
                break;
            }
        }