Пример #1
0
        //#####################################################################################
        // 작업 쓰레드용 함수

        protected void AcceptThreadJob()
        {
            while (m_acceptRun)
            {
                // 보류중인 접속이 없으면 대기
                while (!m_listener.Pending())
                {
                    if (m_acceptRun == false)
                    {
                        return;
                    }

                    Thread.Sleep(128);
                }

                // 접속확인
                Socket newClient = m_listener.AcceptSocket();

                // 클라이언트 객체 생성
                if (newClient != null)
                {
                    ServerVisitor client = new ServerVisitor(newClient);

                    m_clientList.Add(client);


                    // 이벤트 발생
                    WhenConnected(client);
                }
            }
        }
        //#####################################################################################

        public void Run(int number, ServerVisitor client, NetMessageStream reader)
        {
            if (number < 0)
            {
                throw new ArgumentException("number는 0보다 작을 수 없습니다.");
            }

            if (number < m_procList.Count &&
                m_procList[number] != null)
            {
                try
                {
                    var responce = m_procList[number](client, reader);

                    if (responce != null)
                    {
                        client.Sender.SendMessage(responce);
                    }
                }
                catch (FormatException)
                {
                    // TODO: 패킷변조 경고
                }
                catch (System.IO.EndOfStreamException)
                {
                    // TODO: 패킷변조 경고
                }
#if !DEBUG
                catch (Exception)
                {
                }
#endif
            }
        }
Пример #3
0
        public void SendMessageToAll(NetMessage message)
        {
            for (int i = 0; i < m_clientList.Count; ++i)
            {
                ServerVisitor client = m_clientList[i];

                if (client != null)
                {
                    client.Sender.SendMessage(message.Clone());
                }
            }
        }
Пример #4
0
        public void DisconnectClient(int id)
        {
            for (int i = 0; i < m_clientList.Count; ++i)
            {
                ServerVisitor client = m_clientList[i];

                if (client.ID == id)
                {
                    this.RemoveClientAt(i);
                    break;
                }
            }
        }
Пример #5
0
        public void ReceiveMessageInto(NetServerProcedure procList)
        {
            for (int i = 0; i < m_clientList.Count; ++i)
            {
                ServerVisitor client = m_clientList[i];

                var msg = client.Receiver.ReceiveMessage();
                if (msg != null)
                {
                    procList.Run(msg.Header.MessageNumber, client, new IO.NetMessageStream(msg));
                }
            }
        }
Пример #6
0
        //#####################################################################################
        // 접속자

        public ServerVisitor GetClientByID(int id)
        {
            for (int i = 0; i < m_clientList.Count; ++i)
            {
                ServerVisitor client = m_clientList[i];

                if (client.ID == id)
                {
                    return(client);
                }
            }


            return(null);
        }
Пример #7
0
        //#####################################################################################
        // 메세지 처리

        public KeyValuePair <ServerVisitor, NetMessage>[] ReceiveMessageFromAll()
        {
            if (m_clientList.Count > 0)
            {
                var result = new KeyValuePair <ServerVisitor, NetMessage> [m_clientList.Count];

                for (int i = 0; i < result.Length; ++i)
                {
                    ServerVisitor client = m_clientList[i];

                    result[i] = new KeyValuePair <ServerVisitor, NetMessage>(client,
                                                                             client.Receiver.ReceiveMessage());
                }


                return(result);
            }


            return(null);
        }
Пример #8
0
        protected void CheckThreadJob()
        {
            while (m_checkRun)
            {
                for (int i = 0; i < m_clientList.Count; ++i)
                {
                    ServerVisitor client = m_clientList[i];

                    // 클라이언트 접속이 유효하지 않으면
                    if (client.IsValid == false ||
                        Utility.IsConnected(client.Socket) == false)
                    {
                        this.RemoveClientAt(i);
                        --i;
                    }
                }


                Thread.Sleep(3000);
            }
        }