Esempio n. 1
0
        private uint SendSyncPacket(IDSocket socket, SmartPacket packet, int timeout)
        {
            uint errCode = (uint)ErrCode.Success;

            m_eventSyncPacket.Reset();
            socket.SendPacket(packet);

            if (!m_eventSyncPacket.WaitOne(timeout))
            {
                errCode = (uint)ErrCode.NetworkTimeout;
            }

            return errCode;
        }
Esempio n. 2
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;
        }
Esempio n. 3
0
        public void SendPacket(SmartPacket packet)
        {
            packet.PrepareSendBuffer();

            try
            {
                Write(packet.EncodedBuffer);
            }
            catch (Exception e)
            {
                HandleLog(string.Format("IDSocket.SendPacket() Exception={0}", e.Message));
                OnIOError(e, false);
            }
        }