Exemplo n.º 1
0
        public void OnConnect(IAsyncResult ar)
        {
            bool         result       = false;
            LPSocker     socker       = null;
            LPConnector  connector    = null;
            HandlerParam handlerParam = null;

            try
            {
                if (LOG_ERROR(ar != null))
                {
                    goto Exit0;
                }

                handlerParam = (HandlerParam)ar.AsyncState;
                if (LOG_ERROR(handlerParam != null))
                {
                    goto Exit0;
                }

                if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Connector))
                {
                    goto Exit0;
                }
                connector = (LPConnector)handlerParam.Handler;

                LP.Logger.IMP("connect success.");

                connector.Client.EndConnect(ar);

                socker = LP.NetModule.CreateSocker(connector.Client.Client, connector.PacketParser, connector, false);
                if (LOG_ERROR(socker != null))
                {
                    goto Exit0;
                }

                result = LP.NetModule.EventMgr.PushEstablishEvent(socker);
                if (LOG_ERROR(result))
                {
                    goto Exit0;
                }
            }
            catch (SocketException e)
            {
                //if(!connector.IsStoped)
                {
                    LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}",
                                    e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace);

                    LP.NetModule.EventMgr.PushConnectorErrorEvent(connector, e.ErrorCode);
                }
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

Exit0:
            return;
        }
Exemplo n.º 2
0
        internal void PostRecv()
        {
            IAsyncResult ar           = null;
            SocketError  socketError  = SocketError.SocketError;
            HandlerParam handlerParam = null;

            try
            {
                handlerParam = new HandlerParam(EHandlerType.Socker, this);
                if (LOG_ERROR(handlerParam != null))
                {
                    goto Exit0;
                }

                ar = Sock.BeginReceive(RecvBuf.BufBytes, RecvBuf.WriteInx, RecvBuf.GetOnceWritableLen(), SocketFlags.None, out socketError,
                                       LP.NetModule.Reactor.OnReceive, handlerParam);
                if (socketError != SocketError.Success)
                {
                    Close(ESockerCloseType.PostRecvFail, 1, true);
                    goto Exit0;
                }
                if (LOG_ERROR(ar != null))
                {
                    goto Exit0;
                }
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

Exit0:
            return;
        }
Exemplo n.º 3
0
        public void OnSend(IAsyncResult ar)
        {
            bool         result       = false;
            int          sendLen      = 0;
            LPSocker     socker       = null;
            SocketError  socketError  = SocketError.SocketError;
            HandlerParam handlerParam = null;

            try
            {
                if (LOG_ERROR(ar != null))
                {
                    goto Exit0;
                }

                handlerParam = (HandlerParam)ar.AsyncState;
                if (LOG_ERROR(handlerParam != null))
                {
                    goto Exit0;
                }

                if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Socker))
                {
                    goto Exit0;
                }
                socker = (LPSocker)handlerParam.Handler;

                sendLen = socker.Sock.EndSend(ar, out socketError);
                if (sendLen <= 0 || socketError != SocketError.Success)
                {
                    result = LP.NetModule.EventMgr.PushTerminateEvent(socker);
                    if (LOG_ERROR(result))
                    {
                        goto Exit0;
                    }
                }
                else
                {
                    socker.SendBuf.FinishRead(sendLen);
                    socker.IsSending = false;
                }
            }
            catch (SocketException e)
            {
                LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}",
                                e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace);
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

Exit0:
            return;
        }
Exemplo n.º 4
0
        internal bool PostSend()
        {
            bool         haveDataSend = false;
            IAsyncResult ar           = null;
            SocketError  socketError  = SocketError.SocketError;
            HandlerParam handlerParam = null;

            try
            {
                if (SUCCESS(IsClosed))
                {
                    goto Exit1;
                }
                if (SUCCESS(IsSending))
                {
                    goto Exit1;
                }

                if (SendBuf.GetOnceReadableLen() > 0)
                {
                    IsSending    = true;
                    haveDataSend = true;

                    handlerParam = new HandlerParam(EHandlerType.Socker, this);
                    if (LOG_ERROR(handlerParam != null))
                    {
                        goto Exit0;
                    }

                    ar = Sock.BeginSend(SendBuf.BufBytes, SendBuf.ReadInx, SendBuf.GetOnceReadableLen(), SocketFlags.None, out socketError,
                                        LP.NetModule.Reactor.OnSend, handlerParam);
                    if (socketError != SocketError.Success)
                    {
                        Close(ESockerCloseType.PostSendFail, 1, true);
                        goto Exit0;
                    }
                    if (LOG_ERROR(ar != null))
                    {
                        goto Exit0;
                    }
                }
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

Exit1:
Exit0:
            return(haveDataSend);
        }
Exemplo n.º 5
0
        public void OnMessage(LPSocker socker, byte[] buf, int size)
        {
            int    Index       = 0;
            UInt16 MsgId       = 0;
            UInt16 MsgSize     = 0;
            int    MsgHeadSize = 4;

            LP.Logger.IMP("OnMessage output : socker id is {0}", socker.ID);

            if (LOG_ERROR(socker != null))
            {
                goto Exit0;
            }
            if (LOG_ERROR(buf != null))
            {
                goto Exit0;
            }
            if (LOG_ERROR(size >= MsgHeadSize))
            {
                goto Exit0;
            }

            MsgId = (UInt16)(buf[Index++] | buf[Index++]);
            if (LOG_ERROR(MsgId > LPProtocol.min_message_id && MsgId < LPProtocol.max_message_id))
            {
                goto Exit0;
            }

            MsgSize = (UInt16)(buf[Index++] | buf[Index++]);
            if (LOG_ERROR(MsgSize == size))
            {
                goto Exit0;
            }

            if (LOG_ERROR(m_MessageCallBackList[MsgId] != null))
            {
                goto Exit0;
            }

            try
            {
                m_MessageCallBackList[MsgId](socker, buf, size);
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

Exit0:
            return;
        }
Exemplo n.º 6
0
 public void Stop()
 {
     try
     {
         IsStoped = true;
         Listener.Stop();
     }
     catch (SocketException e)
     {
         LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}",
                         e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace);
     }
     catch (Exception e)
     {
         LPMiniDump.GenerateNormalDump(e);
     }
 }
Exemplo n.º 7
0
        public bool Start()
        {
            HandlerParam handlerParam = null;
            IAsyncResult ar           = null;

            try
            {
                Listener.Start();

                handlerParam = new HandlerParam(EHandlerType.Listener, this);
                if (LOG_ERROR(handlerParam != null))
                {
                    goto Exit0;
                }
                ar = Listener.BeginAcceptSocket(LP.NetModule.Reactor.OnAccept, handlerParam);
                if (LOG_ERROR(ar != null))
                {
                    goto Exit0;
                }
            }
            catch (SocketException e)
            {
                LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}",
                                e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace);

                if (LOG_ERROR(false))
                {
                    goto Exit0;
                }
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

            return(true);

Exit0:
            return(false);
        }
Exemplo n.º 8
0
        public bool Start(string ip, int port)
        {
            HandlerParam handlerParam = null;
            IAsyncResult ar           = null;

            try
            {
                handlerParam = new HandlerParam(EHandlerType.Connector, this);
                if (LOG_ERROR(handlerParam != null))
                {
                    goto Exit0;
                }
                ar = Client.BeginConnect(ip, port, LP.NetModule.Reactor.OnConnect, handlerParam);
                if (LOG_ERROR(ar != null))
                {
                    goto Exit0;
                }
            }
            catch (SocketException e)
            {
                LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}",
                                e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace);

                if (LOG_ERROR(false))
                {
                    goto Exit0;
                }
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

            return(true);

Exit0:
            return(false);
        }
Exemplo n.º 9
0
        public void OnAccept(IAsyncResult ar)
        {
            bool         result       = false;
            LPSocker     socker       = null;
            Socket       socket       = null;
            LPListener   listener     = null;
            HandlerParam handlerParam = null;
            IAsyncResult retAr        = null;

            try
            {
                if (LOG_ERROR(ar != null))
                {
                    goto Exit0;
                }

                handlerParam = (HandlerParam)ar.AsyncState;
                if (LOG_ERROR(handlerParam != null))
                {
                    goto Exit0;
                }

                if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Listener))
                {
                    goto Exit0;
                }
                listener = (LPListener)handlerParam.Handler;

                LP.Logger.IMP("accept success.");

                socket = listener.Listener.EndAcceptSocket(ar);
                if (LOG_ERROR(socket != null))
                {
                    goto Exit0;
                }

                retAr = listener.Listener.BeginAcceptSocket(LP.NetModule.Reactor.OnAccept, handlerParam);
                if (LOG_ERROR(retAr != null))
                {
                    goto Exit0;
                }

                socker = LP.NetModule.CreateSocker(socket, listener.PacketParser, listener, true);
                if (LOG_ERROR(socker != null))
                {
                    goto Exit0;
                }

                result = LP.NetModule.EventMgr.PushEstablishEvent(socker);
                if (LOG_ERROR(result))
                {
                    goto Exit0;
                }
            }
            catch (SocketException e)
            {
                //if(!listener.IsStoped)
                {
                    LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}",
                                    e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace);
                }
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

Exit0:
            return;
        }
Exemplo n.º 10
0
        public void OnReceive(IAsyncResult ar)
        {
            bool         result       = false;
            int          recvLen      = 0;
            int          useLen       = 0;
            LPSocker     socker       = null;
            HandlerParam handlerParam = null;
            SocketError  socketError  = SocketError.SocketError;

            try
            {
                if (LOG_ERROR(ar != null))
                {
                    goto Exit0;
                }

                handlerParam = (HandlerParam)ar.AsyncState;
                if (LOG_ERROR(handlerParam != null))
                {
                    goto Exit0;
                }

                if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Socker))
                {
                    goto Exit0;
                }
                socker = (LPSocker)handlerParam.Handler;

                recvLen = socker.Sock.EndReceive(ar, out socketError);
                if (recvLen <= 0 || socketError != SocketError.Success)
                {
                    result = LP.NetModule.EventMgr.PushTerminateEvent(socker);
                    if (LOG_ERROR(result))
                    {
                        goto Exit0;
                    }
                }
                else
                {
                    socker.RecvBuf.FinishWrite(recvLen);

                    while (socker.RecvBuf.GetTotalReadableLen() > 0)
                    {
                        useLen = socker.PacketParser.Parse(socker.RecvBuf);
                        if (0 == useLen)
                        {
                            //接收区已满,但还没有装下一个包,只好断开连接
                            if (socker.RecvBuf.GetTotalWritableLen() <= 0)
                            {
                                socker.Close(ESockerCloseType.RecvError, 1, false);
                                if (LOG_ERROR(false))
                                {
                                    goto Exit0;
                                }
                            }

                            break;
                        }
                        else if (useLen > 0)
                        {
                            if (useLen > socker.RecvBuf.GetTotalReadableLen())
                            {
                                socker.Close(ESockerCloseType.RecvError, 2, false);
                                if (LOG_ERROR(false))
                                {
                                    goto Exit0;
                                }
                            }

                            result = LP.NetModule.EventMgr.PushRecvEvent(socker, socker.RecvBuf, useLen);
                            if (!result)
                            {
                                socker.Close(ESockerCloseType.RecvError, 3, false);
                                if (LOG_ERROR(false))
                                {
                                    goto Exit0;
                                }
                            }
                        }
                        else
                        {
                            socker.Close(ESockerCloseType.RecvError, 4, false);
                            if (LOG_ERROR(false))
                            {
                                goto Exit0;
                            }
                        }
                    }

                    socker.PostRecv();
                }
            }
            catch (SocketException e)
            {
                LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}",
                                e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace);
            }
            catch (Exception e)
            {
                LPMiniDump.GenerateNormalDump(e);
            }

Exit0:
            return;
        }