Esempio n. 1
0
        // Class Static ========================================================================

        public static SockSess Accept(SockSess sess)
        {
            try {
                Socket sock = sess.sock.Accept();
                sock.SendTimeout = 1000;
                return(new SockSess(SockType.accept, sock));
            } catch (Exception) {
                return(null);
            }
        }
Esempio n. 2
0
        public void SendSession(SockSess sess, byte[] data)
        {
            ThreadCheck(false);

            if (sess.type == SockType.listen)
            {
                foreach (var child in FindAcceptSession(sess))
                {
                    child.WfifoSet(data);
                }
            }
            else
            {
                sess.WfifoSet(data);
            }
        }
Esempio n. 3
0
        public SockSess AddConnect(IPEndPoint ep)
        {
            ThreadCheck(false);

            SockSess retval = SockSess.Connect(ep);

            if (retval == null)
            {
                log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
                .Warn(String.Format("Connected to {0} failed.", ep.ToString()));
                return(null);
            }

            AddSession(retval);
            return(retval);
        }
Esempio n. 4
0
        private SockSess[] FindAcceptSession(SockSess sess)
        {
            List <SockSess> retval = new List <SockSess>();

            if (sess.type == SockType.listen)
            {
                var subset = from s in sess_table
                             where s.type == SockType.accept && sess.lep.Port == s.lep.Port
                             select s;
                foreach (var item in subset)
                {
                    retval.Add(item);
                }
            }

            return(retval.ToArray());
        }
Esempio n. 5
0
        private void DeleteSession(SockSess sess)
        {
            if (sess_delete != null)
            {
                sess_delete(this, sess);
            }

            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            if (sess.type == SockType.listen)
            {
                log.Info(String.Format("Session #* deleted from {0}.", sess.lep.ToString()));
            }
            else
            {
                log.Info(String.Format("Session #* deleted from {0}.", sess.rep.ToString()));
                sess.sock.Shutdown(SocketShutdown.Both);
            }

            sess.sock.Close();
            sess_table.Remove(sess);
        }
Esempio n. 6
0
        // Self Methods ========================================================================

        private void AddSession(SockSess sess)
        {
            sess_table.Add(sess);

            if (sess_create != null)
            {
                sess_create(this, sess);
            }

            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            if (sess.type == SockType.listen)
            {
                log.Info(String.Format("Session #L listened at {0}.", sess.lep.ToString()));
            }
            else if (sess.type == SockType.accept)
            {
                log.Info(String.Format("Session #A accepted to {0}.", sess.rep.ToString()));
            }
            else// if (sess.type == SockType.connect)
            {
                log.Info(String.Format("Session #C connected to {0}.", sess.rep.ToString()));
            }
        }
Esempio n. 7
0
        public SockSess FindSession(SockType type, IPEndPoint lep, IPEndPoint rep)
        {
            ThreadCheck(false);

            SockSess retval = null;

            switch (type)
            {
            case SockType.listen:
            case SockType.connect:
                foreach (var item in sess_table)
                {
                    if (item.type == type && item.lep.Equals(lep))
                    {
                        retval = item;
                        break;
                    }
                }
                break;

            case SockType.accept:
                foreach (var item in sess_table)
                {
                    if (item.type == type && item.rep.Equals(rep))
                    {
                        retval = item;
                        break;
                    }
                }
                break;

            default:
                break;
            }

            return(retval);
        }
Esempio n. 8
0
        public void DelSession(SockSess sess)
        {
            ThreadCheck(false);

            sess.eof = true;
        }
Esempio n. 9
0
        // Methods ============================================================================

        public void Exec(int next)
        {
            ThreadCheck(true);

            if (dispatcher_delegate_table.Count != 0)
            {
                lock (dispatcher_delegate_table) {
                    foreach (var item in dispatcher_delegate_table)
                    {
                        item.Key.Method.Invoke(item.Key.Target, item.Value);
                    }
                    dispatcher_delegate_table.Clear();
                }
            }

            // ** none
            if (sess_table.Count == 0)
            {
                System.Threading.Thread.Sleep(next);
                return;
            }

            // ** read
            var       subset = from s in sess_table select s.sock;
            ArrayList list   = new ArrayList(subset.ToArray());

            Socket.Select(list, null, null, next);
            foreach (var i in list)
            {
                foreach (var item in sess_table)
                {
                    if (item.sock == i)
                    {
                        if (item.type == SockType.listen)
                        {
                            SockSess retval = SockSess.Accept(item);
                            if (retval == null)
                            {
                                continue;
                            }
                            AddSession(retval);
                        }
                        else
                        {
                            item.Recv();
                        }
                        break;
                    }
                }
            }

            // ** timeout after read & parse & send & close
            foreach (SockSess item in sess_table.ToArray())
            {
                if (item.type == SockType.accept && DateTime.Now.Subtract(item.tick).TotalSeconds > stall_time)
                {
                    item.eof = true;
                }

                if (item.RfifoRest() != 0 && sess_parse != null)
                {
                    sess_parse(this, item);
                }

                item.Send();

                if (item.eof == true)
                {
                    if (item.type == SockType.listen)
                    {
                        foreach (var i in FindAcceptSession(item))
                        {
                            i.eof = true;
                        }
                    }
                    DeleteSession(item);
                }
            }
        }