Ejemplo n.º 1
0
 public IPacketFactory GetPacketFactory(PacketIdDefine id)
 {
     if (m_Packets.ContainsKey(id))
     {
         return(m_Packets[id]);
     }
     LogModule.LogInfo("Can't find packet factory for id enum : {0}", id);
     return(null);
 }
Ejemplo n.º 2
0
 public IPacketHandler GetPacketHandler(PacketIdDefine id)
 {
     if (m_Handlers.ContainsKey(id))
     {
         IPacketHandler handler = m_Handlers[id];
         return(handler);
     }
     LogModule.LogInfo("Can't find packet handler for id enum : {0}", id);
     return(null);
 }
Ejemplo n.º 3
0
 public void Shutdown()
 {
     if (m_Client != null)
     {
         IPEndPoint ip = m_Client.RemoteEndPoint as IPEndPoint;
         LogModule.LogInfo("On connection shut down, ip : {0} , port : {1}", ip.Address, ip.Port);
         m_Client.Close();
         m_Client = null;
     }
     Clear();
 }
Ejemplo n.º 4
0
        public PacketBase <T> CreatePacket <T>(PacketIdDefine id) where T : new()
        {
            PacketIdDefine key = id;

            if (m_Packets.ContainsKey(key))
            {
                IPacketFactory factory = m_Packets[key];
                return(factory.CreatePacket() as PacketBase <T>);
            }
            LogModule.LogInfo("Can't find packet factory for type : {0}", key);
            return(null);
        }
Ejemplo n.º 5
0
        public void ProcessCommands(ConnectManager connMan)
        {
            // 一次最多执行10个
            for (int i = 0; i < 10; i++)
            {
                if (!m_InputStream.HasData())
                {
                    return;
                }
                int size = m_InputStream.TryReadBuffer(m_temp);
                //parse packet id and length
                if (size < 4)
                {
                    return;
                }
                ushort len      = BitConverter.ToUInt16(m_temp, 0);
                ushort packetId = BitConverter.ToUInt16(m_temp, 2);
                if (size < len)
                {
                    //还没接收完或者包不完整
                    return;
                }
                m_InputStream.SetReadIndex(len);

                try
                {
                    // 调用handle
                    IPacketFactory factory = connMan.GetPacketFactory((Packets.PacketIdDefine)packetId);
                    if (factory != null)
                    {
                        m_tempStream.Seek(0, SeekOrigin.Begin);
                        m_tempStream.SetLength(len - c_headSize);
                        object         data    = Serializer.Deserialize(factory.GetDataType(), m_tempStream);//GC狂魔
                        IPacketHandler handler = connMan.GetPacketHandler((Packets.PacketIdDefine)packetId);
                        if (handler != null)
                        {
                            handler.Handle(this, data);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogModule.LogInfo("Handle packet error, Packet id : {0}, len : {1}, msg : {2}", packetId, len, e.Message);
                    Shutdown();
                    break;
                }
            }
        }
Ejemplo n.º 6
0
        private void Regist <T1, T2>(PacketIdDefine id) where T1 : new() where T2 : IPacketHandler, new()
        {
            PacketIdDefine key = id;

            if (m_Packets.ContainsKey(key))
            {
                LogModule.LogError("Already contains key : {0}", key);
                return;
            }
            var factory = new PacketFactory <T1>(id);

            m_Packets.Add(key, factory);
            var handler = new T2();

            m_Handlers.Add(key, handler);
        }
Ejemplo n.º 7
0
 public void Select()
 {
     try
     {
         bool hasData = GetClientList(m_ErrorCheck);
         hasData = hasData && GetClientList(m_ReadCheck);
         hasData = hasData && GetClientList(m_WriteCheck);
         if (hasData)
         {
             Socket.Select(m_ReadCheck, m_WriteCheck, m_ErrorCheck, 0);
         }
     }
     catch (Exception e)
     {
         LogModule.LogInfo(string.Format("Select call error, meassage : {0}", e.Message));
     }
 }
Ejemplo n.º 8
0
        public void OnSelectError()
        {
            SocketError err = (SocketError)m_Client.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Error);

            if (err == SocketError.AlreadyInProgress ||
                err == SocketError.Success ||
                err == SocketError.WouldBlock ||
                err == SocketError.AlreadyInProgress ||
                err == SocketError.IsConnected ||
                err == SocketError.IOPending)
            {
                return;
            }

            LogModule.LogInfo("OnSelectError : {0}", err);

            Shutdown();
        }
Ejemplo n.º 9
0
 public void OnSelectWrite()
 {
     try
     {
         if (!m_OutputStream.HasData())
         {
             return;
         }
         SocketError err      = SocketError.Success;
         int         offset   = m_OutputStream.GetReadIndex();
         int         size     = m_OutputStream.TryReadBuffer(m_tempSend);
         int         sendSize = m_Client.Send(m_tempSend, 0, size, SocketFlags.None, out err);
         m_OutputStream.SetReadIndex(sendSize);
     }
     catch (Exception e)
     {
         LogModule.LogInfo("OnSelectWrite, exception message : {0}", e.Message);
     }
 }
Ejemplo n.º 10
0
        public void Tick()
        {
            try
            {
                GetListenList(m_Listeners);
                Socket.Select(null, null, m_Listeners, 0);
                bool bError = m_Listeners.Count > 0;
                if (bError)
                {
                    return;
                }

                GetListenList(m_Listeners);
                Socket.Select(m_Listeners, null, null, 0);
                bool bCanRead = m_Listeners.Count > 0;
                if (!bCanRead)
                {
                    return;
                }

                Socket newClient = m_Listener.Accept();
                if (newClient != null)
                {
                    if (m_AcceptDelegate != null)
                    {
                        m_AcceptDelegate(newClient);
                    }
                    IPEndPoint ip = (IPEndPoint)newClient.RemoteEndPoint;
                    LogModule.LogInfo("Accept one client, ip={0}, port={1}", ip.Address, ip.Port);
                }
            }
            catch (Exception e)
            {
                LogModule.LogInfo("Accept client error, Message:{0}", e.Message);
            }
        }
Ejemplo n.º 11
0
        public void Handle(ConnectInstance conn, object data)
        {
            Person p = data as Person;

            LogModule.LogInfo("PacketTestHandler handled!, {0}, {1}, {2}, {3}", p.Id, p.Name, p.Address.Line1, p.Address.Line2);
        }