예제 #1
0
        public NetSession CreateSession()
        {
            UInt64     nNewSessionID = GetNewSessionID();
            NetSession newSession    = new NetSession(nNewSessionID, m_refCommandQueue);

            m_dicSession.Add(nNewSessionID, newSession);
            return(newSession);
        }
예제 #2
0
        public NetSession FindSession(UInt64 nSessionID)
        {
            NetSession session = null;

            try
            {
                session = m_dicSession[nSessionID];
            }
            catch {}
            return(session);
        }
예제 #3
0
        public void DeleteSession(UInt64 nSessionID)
        {
            try
            {
                NetSession session = m_dicSession[nSessionID];
                m_dicSession.Remove(nSessionID);

                session.Close();
            }
            catch {}
        }
예제 #4
0
        public void Destroy()
        {
            List <UInt64> listDestroyedSession = new List <UInt64>();

            foreach (KeyValuePair <UInt64, NetSession> pair in m_dicSession)
            {
                NetSession session = pair.Value;
                session.Close();
            }

            m_dicSession.Clear();
        }
예제 #5
0
        public void Update()
        {
            List <UInt64> listDestroyedSession = new List <UInt64>();

            foreach (KeyValuePair <UInt64, NetSession> pair in m_dicSession)
            {
                NetSession session = pair.Value;
                if (session.Destroyed)
                {
                    session.Close();
                    listDestroyedSession.Add(pair.Key);
                }
            }

            foreach (UInt64 Key in listDestroyedSession)
            {
                m_dicSession.Remove(Key);
            }
        }
예제 #6
0
        public virtual bool NewClientSession(string strServerName, string strIP, int nPort, out UInt64 nSessionID)
        {
            nSessionID = 0;

            if (strServerName.Length == 0 && strIP.Length == 0)
            {
                return(false);
            }

            if (strServerName.Length != 0 && strIP.Length == 0)
            {
                IPHostEntry host;
                try
                {
                    host = Dns.GetHostEntry(strServerName);
                    if (host.AddressList.Length == 0)
                    {
                        return(false);
                    }
                }
                catch (System.Exception e)
                {
                    FileLog.Instance.Write("Error! HostName={0}, Msg={1}", strServerName, e.Message);
                    return(false);
                }

                IPAddress addr = host.AddressList[0];
                strIP = addr.ToString();
            }

            NetSession newSession = m_sessionMgr.CreateSession();

            if (newSession.RequestConnect(strIP, nPort) == false)
            {
                m_sessionMgr.DeleteSession(newSession.SessionID);
                return(false);
            }
            nSessionID = newSession.SessionID;
            return(true);
        }
예제 #7
0
        public bool Post(NetCommand command)
        {
            if (command.SessionID == 0)
            {
                m_commandQueue.PushCommand(command);
            }
            else
            {
                NetSession session = m_sessionMgr.FindSession(command.SessionID);
                if (session == null)
                {
                    return(false);
                }

                if (session.Send(command.Buffer, command.BufferSize) == false)
                {
                    return(false);
                }
            }

            return(true);
        }