Beispiel #1
0
        /// <summary>
        /// 클라이언트가 Connect됬을때 가장 먼저 호출해줘야 함
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public RemoteID OnConnect(ref T session)
        {
            RemoteID remote = RemotePool.GetRemotePool();

            if (remote == RemoteID.Remote_None)
            {
                if (message_handler != null)
                {
                    message_handler(MsgType.Critical, "remotePool Empty!");
                }
            }

            lock (sync_obj)
            {
                if (RemoteClients.ContainsKey(remote))
                {
                    if (message_handler != null)
                    {
                        message_handler(MsgType.Warning,
                                        "remote key 중복되어 반복루프시작함... (멀티스레드 서버에서 close된상태에서 남은recv처리가 존재할경우 발생될수있음)");
                    }
                    while (true)
                    {
                        RemotePool.FreeRemotePool(remote);      // 기존 중복된것을 사용안했으니 바로 반환한다음..
                        remote = RemotePool.GetRemotePool();    // 다시 하나 가져오고..
                        if (RemoteClients.ContainsKey(remote) == false)
                        {
                            break;
                        }
                    }
                }
                RemoteClients.Add(remote, (IRemoteClient)session);
            }

            // 내부패킷 보내기
            CMessage    sMsg       = new CMessage();
            CPackOption sendOption = CPackOption.Basic;

            sMsg.WriteStart(PacketType.PacketType_Internal, sendOption);
            Marshaler.Write(sMsg, PackInternal.ePID_SC_Remote);
            Marshaler.Write(sMsg, remote);
            SendToClient(remote, (IRemoteClient)session, sMsg, sendOption);   // remote를 별도로 보내는 이유는 이함수가 리턴되야 session의 remote가 갱신되므로..

            return(remote);
        }
Beispiel #2
0
        public override bool Send(RemoteID remote, CMessage msg, CPackOption op)
        {
            //IRemoteClient client = null;
            //lock (sync_obj)
            //{
            //    if (RemoteClients.TryGetValue(remote, out client) == false)
            //    {
            //        return false;
            //    }
            //}
            IRemoteClient client = (IRemoteClient)GetRemoteClient(remote);

            if (client != null)
            {
                SendToClient(remote, client, msg, op);
            }
            return(true);
        }
Beispiel #3
0
 public void SendToClient(RemoteID remote, IRemoteClient client, CMessage msg, CPackOption op)
 {
     PreparePacket(remote, RemoteID.Remote_Server, msg, op);
     client.SendToRemote(CPacket.create(msg));
 }
Beispiel #4
0
        protected override bool RecvInternalMessage(RemoteID remote, PackInternal pkID, CMessage msgData, CPackOption op)
        {
            switch (pkID)
            {
            case PackInternal.ePID_CS_Test:
            {
                int    aa;
                string ss;
                Marshaler.Read(msgData, out aa);
                Marshaler.Read(msgData, out ss);
                if (message_handler != null)
                {
                    message_handler(MsgType.Debug, op.m_pack_mode + ", InterPkt: " + pkID + ", " + aa + ", " + ss);
                }
            }
            break;

            default:
            {
                // test
            }
                return(false);
            }
            return(true);
        }
Beispiel #5
0
        public void OnRecv(T remoteClient, byte[] buffer, int len)
        {
            IRemoteClient client = (IRemoteClient)remoteClient;

            if (client.GetRemoteID() == RemoteID.Remote_None)
            {
                // Connect스레드가 다를경우 조금 기다려주도록 하자
                for (int i = 0; i < WaitDelayRecvThreadCount; i++)    // 20일경우 100 * 20 즉 2000밀리세컨드 -> 최대 2초까지 기다려준다
                {
                    System.Threading.Thread.Sleep(100);
                    if (client.GetRemoteID() != RemoteID.Remote_None)
                    {
                        break;
                    }
                }

                // 그래도 할당안됬다면?
                if (client.GetRemoteID() == RemoteID.Remote_None)
                {
                    // 이경우는 연결이벤트 처리가 아직안된경우임
                    if (message_handler != null)
                    {
                        message_handler(MsgType.Error, "remoteID 할당이 안되었음.");
                    }
                }
            }

            CRecvedMsg recved_msg = new CRecvedMsg();

            try
            {
                CPackOption packOption = new CPackOption();
                CMessage    MsgBuffer  = new CMessage(buffer, 0, len);
                recved_msg.From(
                    client.GetRemoteID(),
                    MsgBuffer,
                    MsgBuffer.ReadStart(out packOption),
                    packOption
                    );

                // 예외발생 test
                //string a = "aa";
                //int.Parse(a);
            }
            catch (Exception e)
            {
                if (message_handler != null)
                {
                    message_handler(MsgType.Warning, string.Format("{0} OnPackException: {1}",
                                                                   client.Address().ToString(),
                                                                   e.Message));
                }
                client.Disconnect();
                return;
            }

            string msg = OnPacket(recved_msg);

            if (msg != string.Empty)
            {
                if (message_handler != null)
                {
                    message_handler(MsgType.Warning, string.Format("{0} {1}", client.Address().ToString(), msg));
                }
                client.Disconnect();
            }
        }
Beispiel #6
0
        // 더이상사용안함
        //public void Send(ZNet.CPacket pk)
        //{
        //    m_client.DoSend(pk);
        //}

        public override bool Send(RemoteID remote, CMessage msg, CPackOption op)
        {
            PreparePacket(remote, RemoteID.Remote_Client, msg, op);
            m_client.DoSend(CPacket.create(msg));
            return(true);
        }
Beispiel #7
0
        void SocketThread()
        {
            // ipv4로 먼저 접속 시도
            string err = SocketConnect(AddressFamily.InterNetwork);

            if (m_client.socket.Connected == false)
            {
                // ipv4로 접속 실패시 ipv6로 즉시 재시도한다 : ios경우 ipv6 only환경에서 작동되야 하므로..
                //  -> 즉시 시도해도 되는 이유는 addressfamily가 다르면 즉각 피드백오므로 문제없다
                string err2 = SocketConnect(AddressFamily.InterNetworkV6);

                if (m_client.socket.Connected == false)
                {
                    if (err != string.Empty && message_handler != null)
                    {
                        message_handler(MsgType.Info, err);
                    }

                    if (err2 != string.Empty && message_handler != null)
                    {
                        message_handler(MsgType.Info, err2);
                    }
                }
            }

            if (m_client.socket.Connected == false)
            {
                this.event_manager.enqueue_network_event(NETWORK_EVENT.connect_fail);
                thr = null;
                return;
            }

            this.event_manager.enqueue_network_event(NETWORK_EVENT.connected);

            // 여기서부터 연결이 끊길때까지 Recv무한루프가 시작된다
            bSocketThread = true;

            byte[] buf;
            int    bufsize;

            while (bSocketThread)
            {
                if (RecvLong(out buf, out bufsize) == false)
                {
                    event_manager.enqueue_network_event(NETWORK_EVENT.disconnected);
                    thr = null;
                    return;
                }

                if (bufsize - m_headsize < 0)
                {
                    if (this.message_handler != null)
                    {
                        this.message_handler(MsgType.Warning, string.Format("pksize error : {0}", bufsize));
                    }

                    event_manager.enqueue_network_event(NETWORK_EVENT.disconnected);
                    thr = null;
                    return;
                }

                bufsize -= ECore.CoreParam.HEADER_SIZE_LEN;

                try
                {
                    CRecvedMsg  recved_msg = new CRecvedMsg();
                    CPackOption packOption = new CPackOption();
                    CMessage    MsgBuffer  = new CMessage(buf, 4, bufsize);
                    recved_msg.From(
                        RemoteID.Remote_Server,
                        MsgBuffer,
                        MsgBuffer.ReadStart(out packOption),
                        packOption
                        );

                    event_manager.enqueue_network_message(recved_msg);
                }
                catch (Exception e)
                {
                    if (this.message_handler != null)
                    {
                        this.message_handler(MsgType.Warning, string.Format("recvMsgException : {0}", e.Message));
                    }

                    event_manager.enqueue_network_event(NETWORK_EVENT.disconnected);
                    thr = null;
                    return;
                }
            }
            thr = null;
        }
Beispiel #8
0
        protected override bool RecvInternalMessage(RemoteID remote, PackInternal pkID, CMessage msgData, CPackOption op)
        {
            switch (pkID)
            {
            case PackInternal.ePID_SC_Remote:
            {
                RemoteID _remote;
                Marshaler.Read(msgData, out _remote);
                remoteID = _remote;
                OnReadyClient();
            }
            break;

            default:
            {
                // test
            }
                return(false);
            }
            return(true);
        }