Ejemplo n.º 1
0
        private ErrCode OnRecvPacket(SmartPacket packet)
        {
            ErrCode            errCode             = ErrCode.Success;
            PacketCode         packetCode          = (PacketCode)packet.Code;
            NotifyEventCode   nec                 = NotifyEventCode.None;
            bool                isNotifyPacket      = false;
            bool                isHandlerCalled     = false;
            object              param1              = null;
            object              param2              = null;

            Log(string.Format("[VAAL] OnRecvPacket : PacketCode={0}", packetCode));

            lock (m_lockVal)
            {
                m_packetCode    = packetCode;
                m_resultCode    = packet.Process(m_proxyServer);
                param1          = packet.Param1;
                param2          = packet.Param2;
                nec             = (NotifyEventCode)packet.Nec;
                isNotifyPacket  = !(packet is TwoWayPacket);

                OneWayPacket onewayPacket = packet as OneWayPacket;
                if (onewayPacket != null && onewayPacket.RecvTransNum > 0)
                {
                    if (m_recvTransNum >= onewayPacket.RecvTransNum)
                    {
                        Log(string.Format("[VAAL] OnRecvPacket : CurRecvTransNum = {0}, PacketRecvTransNum = {1}", m_recvTransNum, onewayPacket.RecvTransNum));
                        return errCode;
                    }
                    m_recvTransNum = onewayPacket.RecvTransNum;
                }

                switch (packetCode)
                {
                    case PacketCode.Hello:
                        {
                            m_eventSyncPacket.Set();

                            if (m_resultCode == (uint)ErrCode.VaalSvrClientVersion)
                            {
                                CallNotifyHandler(NotifyEventCode.LowerClientVersion, m_resultCode, null, null);
                            }

                            isNotifyPacket = true;
                            isHandlerCalled = true;
                        }
                        break;

                    case PacketCode.GetItemListReply:
                        {
                            if (m_resultCode == (uint)ErrCode.Success)
                            {
                                GetItemListReplyPacket packetReply = (GetItemListReplyPacket)packet;

                                byte[] temp = new byte[m_buffer.Length + packetReply.m_buffer.Length];
                                Array.Copy(m_buffer, temp, m_buffer.Length);
                                Array.Copy(packetReply.m_buffer, 0, temp, m_buffer.Length, packetReply.m_buffer.Length);
                                m_buffer = temp;

                                if (packetReply.m_isLastPacket == 0)
                                {
                                    // Waits for next packets.
                                    isHandlerCalled = true;
                                    isNotifyPacket = true;
                                }
                                else
                                {
                                    MemoryStream inMStream = new MemoryStream(m_buffer);
                                    MemoryStream outMStream = new MemoryStream();
                                    ZOutputStream outZStream = new ZOutputStream(outMStream);

                                    CopyStream(inMStream, outZStream);

                                    ItemDataType itemType = (ItemDataType)(byte)param1;

                                    switch (itemType)
                                    {
                                        case ItemDataType.Stage:
                                            StageManager stageManager = new StageManager();
                                            SimpleStream streamStage = new SimpleStream();

                                            streamStage.SetBuffer(outMStream.ToArray(), false);
                                            stageManager.SetHash(m_proxyServer.StageManager.Hash);
                                            stageManager.DeSerialize(streamStage);
                                            m_proxyServer.SetStageManager(stageManager);

                                            param2 = stageManager.Hash;
                                            break;

                                        case ItemDataType.Item:
                                            ItemManager itemManager = new ItemManager();
                                            SimpleStream streamItem = new SimpleStream();

                                            streamItem.SetBuffer(outMStream.ToArray(), false);
                                            itemManager.SetHash(m_proxyServer.ItemManager.Hash);
                                            itemManager.DeSerialize(streamItem);
                                            m_proxyServer.SetItemManager(itemManager);

                                            param2 = itemManager.Hash;
                                            break;
                                    }

                                    outZStream.Close();
                                    outMStream.Close();
                                    inMStream.Close();
                                }
                            }
                        }
                        break;

                    case PacketCode.Login:
                        {
                            LoginPacket packetReply = (LoginPacket)packet;

                            if (m_isLoginPacketSync)
                            {
                                m_eventSyncPacket.Set();
                                isNotifyPacket = true;
                            }

                            if (m_resultCode == (uint)ErrCode.Success)
                            {
                                m_proxyServer.SetLoginState(true);

                                if (packetReply.m_isAlreadyLogined == 0)
                                {
                                    m_sendTransNum = 0;
                                    m_recvTransNum = 0;
                                }
                            }
                        }
                        break;
                }
            }

            if (nec != NotifyEventCode.None)
            {
                if (!isNotifyPacket)
                {
                    lock (m_retryPackets)
                    {
                        List<RetryPacket> packetToBeRemoved = new List<RetryPacket>();

                        foreach (RetryPacket rp in m_retryPackets)
                        {
                            rp.m_event.Set();
                            rp.m_packet.Dispose();

                            if (rp.m_nec == nec)
                            {
                                m_retryPackets.Remove(rp);
                                break;
                            }
                            else
                            {
                                packetToBeRemoved.Add(rp);
                                Log(string.Format("[VAAL] No response. NC={0}", rp.m_nec));
                            }
                        }

                        foreach (RetryPacket rp in packetToBeRemoved)
                        {
                            m_retryPackets.Remove(rp);
                        }
                    }
                }

                if (!isHandlerCalled)
                    CallNotifyHandler(nec, m_resultCode, param1, param2);
            }

            return errCode;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 클래스를 초기화한다. 실제 서버와 연결을 시도하는 것은 아니고, 연결 정보를 저장하고 초기화한다.<br/>
        /// keepAliveTimeout의 값이 0이면 KeepAlive 패킷을 전송하지 않는다.<br/>
        /// <br/>
        /// ProxyServer 클래스의 정적인 데이터는 외부에 따로 저장할 수가 있는데, 
        /// 클라이언트는 ProxyServer.Uninitialize() 메소드 호출을 통해서 얻은 정적인 데이터를 ProxyServer.Initialize() 호출시에 다시 설정할 수 있다.
        /// </summary>
        /// <param name="serverIp">Server IP</param>
        /// <param name="serverPort">Server Port</param>
        /// <param name="methodTimeout">Method의 타임아웃. milliseconds</param>
        /// <param name="keepAliveTimeout">KeepAlive 패킷 전송 간격. milliseconds</param>
        /// <param name="isTestVer">클라이언트의 테스트 버전 여부.</param>
        /// <param name="isPaidVer">클라이언트의 유료 버전 여부.</param>
        /// <param name="marketCode">클라이언트의 마켓 코드.</param>
        /// <param name="localeCode">클라이언트의 로케일 코드.</param>
        /// <param name="configData">단말기에 저장되어 있는 캐싱 데이터 스트림. 데이터가 없으면 null을 세팅한다.</param>
        /// <returns>0이면 성공, 이외의 값은 에러</returns>
        public uint Initialize(string serverIp, int serverPort, int methodTimeout, int keepAliveTimeout, bool isTestVer, bool isPaidVer, MarketCode marketCode, LocaleCode localeCode, byte[] configData)
        {
            ErrCode errCode = ErrCode.Success;

            if (!m_isInitialized)
            {
                byte[] temp = null;
                Uninitialize(out temp);

                m_socketTimeout     = methodTimeout;
                m_keepAliveTimeout  = keepAliveTimeout;
                m_clientProductVer  = (byte)((isTestVer ? 0x80 : 0x00) + (isPaidVer ? 0x40: 0x00) + (byte)marketCode);
                m_localeCode        = localeCode;

                m_stageManager  = new StageManager();
                m_stageManager.SetServer(this);
                m_itemManager   = new ItemManager();
                m_itemManager.SetServer(this);

                if (configData != null)
                {
                    try
                    {
                        NMCryptor.DecryptBuffer(configData, SentinelValues.DATA_ENCRYPT_SEED);

                        SimpleStream stream = new SimpleStream();
                        stream.SetBuffer(configData, false);

                        DeSerialize(stream);
                    }
                    catch (Exception e)
                    {
                        Log(string.Format("[PSVR] Initialize() : {0}", e.Message));
                    }
                }

                if (serverIp != m_serverIp || serverPort != m_serverPort)
                {
                    m_lastConnectedServerIp     = m_serverIp        = serverIp;
                    m_lastConnectedServerPort   = m_serverPort      = serverPort;
                }

                if (m_lastConnectedServerIp == null || m_lastConnectedServerIp.Length < 1)
                {
                    m_lastConnectedServerIp     = m_serverIp;
                    m_lastConnectedServerPort   = m_serverPort;
                }

                m_serverVal = new ValServer();
                m_serverVal.NotifyHandler    = OnNotifyHandler;
                m_serverVal.LogHandler       = sm_logHandler;
                m_serverVal.Initialize(this);

                m_isInitialized = true;
            }

            return (uint)errCode;
        }
Ejemplo n.º 3
0
 internal void SetStageManager(StageManager stageManager)
 {
     stageManager.SetServer(this);
     m_stageManager = stageManager;
     m_stageManager.SetHashFromServer(stageManager.Hash);
 }
Ejemplo n.º 4
0
        protected override bool OnDeSerialize(SimpleStream.SerializableDecoder decoder, SimpleStream stream)
        {
            bool ret = true;

            try
            {
                m_serverIp                  = decoder.DecodeString();
                m_serverPort                = (int)decoder.Decode4();
                m_lastConnectedServerIp     = decoder.DecodeString();
                m_lastConnectedServerPort   = (int)decoder.Decode4();

                ValConfig config = new ValConfig();
                if (config.DeSerialize(stream))
                    m_config = config;
                else
                    m_config = null;

                StageManager stageManager = new StageManager();
                if (stageManager.DeSerialize(stream))
                {
                    stageManager.SetServer(this);
                    m_stageManager = stageManager;
                }

                ItemManager itemManager = new ItemManager();
                if (itemManager.DeSerialize(stream))
                {
                    itemManager.SetServer(this);
                    m_itemManager = itemManager;
                }
            }
            catch (Exception)
            {
                ret = false;
            }

            return ret;
        }