Пример #1
0
            /// <summary>
            /// 连接
            /// </summary>
            /// <param name="ar"></param>
            public static void Connect(IAsyncResult ar)
            {
                UserSock self = (UserSock)ar.AsyncState;

                if (self.m_State == SockState.Idle)
                {
                    LogSystem.Log("Idle");
                    return;
                }
                try
                {
                    self.m_Socket.EndConnect(ar);
                }
                catch (SocketException e)
                {
                    LogSystem.Log("SocketException e");
                    self.SetSockError(e.ErrorCode, e.ToString());
                    self.ConnectFail();
                    return;
                }
                catch (Exception e)
                {
                    LogSystem.Log("Exception e", e.ToString());
                    return;
                }
                self.StopConnect();
            }
Пример #2
0
            /// <summary>
            /// 接收
            /// </summary>
            /// <param name="ar"></param>
            public static void Receive(IAsyncResult ar)
            {
                UserSock self = (UserSock)ar.AsyncState;

                if (self.m_State == SockState.Idle)
                {
                    return;
                }
                int size;

                try
                {
                    size = self.m_Socket.EndReceive(ar);
                }
                catch (SocketException e)
                {
                    if (self.m_Socket.Connected)
                    {
                        self.SetSockError(e.ErrorCode, e.Message);
                    }
                    else
                    {
                        self.Close();
                    }
                    return;
                }
                catch (Exception)
                {
                    return;
                }
                self.StopReceive(size);
            }
Пример #3
0
            public int OnSockConnected(UserSock sock, string addr, int port)
            {
                LogSystem.Log("OnSockConnected");
                m_gamesock.SetConnected(true);
                if (null != m_gamesock.m_lgsockcall)
                {
                    m_gamesock.m_lgsockcall.on_connected(addr, port);
                    return(1);
                }
                else
                {
                    if (m_gamesock != null)
                    {
                        VarList args = VarList.GetVarList();
                        args.AddString(addr);
                        args.AddInt(port);
                        m_gamesock.Excute_CallBack("on_connected", args);
                        args.Collect();
                        return(1);
                    }
                    else
                    {
                        LogSystem.LogError("Error, OnSockConnected gamesock is null");
                    }

                    return(0);
                }
            }
Пример #4
0
        public void InitUserSock()
        {
            if (m_sock != null)
            {
                try
                {
                    if (m_sock.Connected)
                    {
                        m_sock.Disconnect(false);
                        //m_sock.Close();
                    }
                }
                catch (System.Exception ex)
                {
                    LogSystem.LogError(ex.ToString());
                }
            }

            m_sock   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            m_scoket = new UserSock(m_sockcall, m_sock);
            if (m_gameSender == null)
            {
                m_gameSender = new GameSender();
            }
            m_gameSender.SetSocket(ref m_scoket);
        }
Пример #5
0
            public int OnSockConnectFail(UserSock sock, string addr, int port)
            {
                //Log.Trace("OnSockConnectFail");

                if (null != m_gamesock.m_lgsockcall)
                {
                    m_gamesock.m_lgsockcall.on_connect_failed(addr, port);
                    return(1);
                }
                else
                {
                    if (m_gamesock != null)
                    {
                        VarList args = new VarList();
                        args.AddString(addr);
                        args.AddInt(port);
                        m_gamesock.Excute_CallBack("on_connect_fail", args);
                        return(1);
                    }
                    else
                    {
                        //Log.TraceError("Error, OnSockConnectFail gamesock is null");
                    }
                    return(0);
                }
            }
Пример #6
0
            public int OnSockConnectFail(UserSock sock, string addr, int port)
            {
                LogSystem.Log("OnSockConnectFail");

                PromptManager.Instance.ShowPromptUI(XmlManager.Instance.GetCommonText("System0001"));

                m_gamesock.SetConnected(false);
                if (null != m_gamesock.m_lgsockcall)
                {
                    m_gamesock.m_lgsockcall.on_connect_failed(addr, port);
                    return(1);
                }
                else
                {
                    if (m_gamesock != null)
                    {
                        VarList args = VarList.GetVarList();
                        args.AddString(addr);
                        args.AddInt(port);
                        m_gamesock.Excute_CallBack("on_connect_fail", args);
                        args.Collect();
                        return(1);
                    }
                    else
                    {
                        LogSystem.LogError("Error, OnSockConnectFail gamesock is null");
                    }
                    return(0);
                }
            }
Пример #7
0
            /// <summary>
            /// 发送
            /// </summary>
            /// <param name="ar"></param>
            public static void Send(IAsyncResult ar)
            {
                UserSock self = (UserSock)ar.AsyncState;

                Log.Trace(self.m_State.ToString());
                if (self.m_State == SockState.Idle)
                {
                    return;
                }
                int size;

                try
                {
                    size = self.m_Socket.EndSend(ar);
                    Log.Trace("size " + size);
                }
                catch (SocketException e)
                {
                    self.SetSockError(e.ErrorCode, e.Message);
                    return;
                }
                catch (Exception)
                {
                    return;
                }
                self.StopSend(size);
            }
Пример #8
0
            /// <summary>
            /// 发送
            /// </summary>
            /// <param name="ar"></param>
            public static void Send(IAsyncResult ar)
            {
                SendContext self  = (SendContext)ar.AsyncState;
                UserSock    uSock = self.uUserSock;

                if (uSock.m_State == SockState.Idle)
                {
                    return;
                }
                int size = 0;

                try
                {
                    size = uSock.m_Socket.EndSend(ar);
                }
                catch (SocketException e)
                {
                    uSock.SetSockError(e.ErrorCode, e.ToString());
                    LogSystem.LogError(e.ToString());
                }
                catch (Exception ex)
                {
                    LogSystem.LogError(ex.ToString());
                }
                finally
                {
                }
                uSock.StopSend(self.sendBuff, size);
                uSock.PushSendContext(self);
            }
Пример #9
0
            /// <summary>
            /// 连接
            /// </summary>
            /// <param name="ar"></param>
            public static void Connect(IAsyncResult ar)
            {
                UserSock self = (UserSock)ar.AsyncState;

                if (self.m_State == SockState.Idle)
                {
                    //Log.Trace("Idle");
                    return;
                }
                try
                {
                    self.m_Socket.EndConnect(ar);
                }
                catch (SocketException e)
                {
                    //Log.Trace("SocketException e");
                    self.SetSockError(e.ErrorCode, e.Message);
                    self.ConnectFail();
                    return;
                }
                catch (Exception e)
                {
                    //Log.Trace("Exception e" + e.Message);
                    return;
                }
                self.StopConnect();
            }
Пример #10
0
            /// <summary>
            /// 接收
            /// </summary>
            /// <param name="ar"></param>
            public static void Receive(IAsyncResult ar)
            {
                RecvContext recvContext = (RecvContext)ar.AsyncState;
                UserSock    self        = recvContext.uUserSock;

                if (self.m_State == SockState.Idle)
                {
                    return;
                }
                int size = 0;

                try
                {
                    size = self.m_Socket.EndReceive(ar);
                    if (size == 0)
                    {
                        size = -1;
                    }
                }
                catch (SocketException e)
                {
                    if (self.m_Socket.Connected)
                    {
                        if (e.ErrorCode == 10035)
                        {
                            size = 0;
                        }
                        else
                        {
                            size = -1;
                            self.SetSockError(e.ErrorCode, e.ToString());
                        }
                    }
                }
                catch (Exception e)
                {
                    size = -1;
                    if (self.m_Socket.Connected)
                    {
                        self.SetSockError(0, e.ToString());
                    }
                }
                finally
                {
                }

                self.StopReceive(recvContext.RecvBuff, size);
                self.PushRecvContext(recvContext);
            }
Пример #11
0
        public RecvContext GetRecvContext(UserSock uSock, AsyncEvent.RecvBufferData recvBuff)
        {
            RecvContext recvContext = null;

            lock (moRecvQueue)
            {
                if (m_queueRecvContext.Count > 0)
                {
                    recvContext = m_queueRecvContext.Dequeue();
                }
            }
            if (recvContext == null)
            {
                recvContext = new RecvContext(uSock, recvBuff);
            }
            else
            {
                recvContext.uUserSock = uSock;
                recvContext.RecvBuff  = recvBuff;
            }
            return(recvContext);
        }
Пример #12
0
        public SendContext GetSendContext(UserSock uSock, SendBuffer sendBuff)
        {
            SendContext sendContext = null;

            lock (moSendQueue)
            {
                if (m_queueSendContext.Count > 0)
                {
                    sendContext = m_queueSendContext.Dequeue();
                }
            }
            if (sendContext == null)
            {
                sendContext = new SendContext(uSock, sendBuff);
            }
            else
            {
                sendContext.uUserSock = uSock;
                sendContext.sendBuff  = sendBuff;
            }
            return(sendContext);
        }
Пример #13
0
            public int OnSockClose(UserSock sock)
            {
                //Log.Trace("OnSockClose");

                if (null != m_gamesock.m_lgsockcall)
                {
                    m_gamesock.m_lgsockcall.on_close();
                    return(1);
                }
                else
                {
                    if (m_gamesock != null)
                    {
                        m_gamesock.Excute_CallBack("on_close", new VarList());
                        return(1);
                    }
                    else
                    {
                        //Log.TraceError("Error, OnSockClose gamesock is null");
                    }
                    return(0);
                }
            }
Пример #14
0
        public bool Init(ISockCallee sockcall)
        {
            m_lgsockcall = sockcall;
            m_sockcall   = new SockCall();
            m_sockcall.SetGameSock(this);
            if (SysUtil.IsIos)
            {
                m_sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            }
            else
            {
                m_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            m_scoket = new UserSock(m_sockcall, m_sock);

            m_gameSender = new GameSender();
            m_gameSender.SetSocket(ref m_scoket);

            m_gameReceiver = new GameReceiver();

            m_gameSender.SetReceiver(m_gameReceiver);
            return(true);
        }
Пример #15
0
            /// <summary>
            /// 断开
            /// </summary>
            /// <param name="ar"></param>
            public static void Disconnect(IAsyncResult ar)
            {
                UserSock self = (UserSock)ar.AsyncState;

                if (self.m_State == SockState.Idle)
                {
                    return;
                }
                try
                {
                    self.m_Socket.EndDisconnect(ar);
                }
                catch (SocketException e)
                {
                    self.SetSockError(e.ErrorCode, e.ToString());
                    return;
                }
                catch (Exception)
                {
                    return;
                }
                self.StopDisconnect();
            }
Пример #16
0
 public int OnSockClose(UserSock sock)
 {
     m_gamesock.SetConnected(false);
     if (null != m_gamesock.m_lgsockcall)
     {
         m_gamesock.m_lgsockcall.on_close();
         return(1);
     }
     else
     {
         if (m_gamesock != null)
         {
             VarList varlist = VarList.GetVarList();
             m_gamesock.Excute_CallBack("on_close", varlist);
             varlist.Collect();
             return(1);
         }
         else
         {
             LogSystem.LogError("Error, OnSockClose gamesock is null");
         }
         return(0);
     }
 }
Пример #17
0
 public int OnSockError(UserSock sock, int error_type, string error_msg)
 {
     Log.TraceError("Error, OnSockError error_type: " + error_type.ToString() + " error_msg:" + error_msg);
     return(1);
 }
Пример #18
0
 public int OnSockLog(UserSock sock, string msg)
 {
     //Log.Trace("OnSockLog " + msg);
     return(1);
 }
Пример #19
0
 public int OnSockError(UserSock sock, int error_type, string error_msg)
 {
     LogSystem.LogError("Error, OnSockError error_type: ", error_type.ToString(), " error_msg:", error_msg);
     return(1);
 }
Пример #20
0
 public void SetSocket(ref UserSock sender)
 {
     m_sender = sender;
 }
Пример #21
0
 public int OnSockLog(UserSock sock, string msg)
 {
     LogSystem.Log("OnSockLog ", msg);
     return(1);
 }
Пример #22
0
 public int OnSockSend(UserSock sock, int size)
 {
     return(1);
 }
Пример #23
0
 public int OnSockReceive(UserSock sock, byte[] data, int size)
 {
     m_gamesock.ProcessMsg(data, size);
     return(1);
 }
Пример #24
0
 public RecvContext(UserSock uSock, AsyncEvent.RecvBufferData RecvBuff)
 {
     this.uUserSock = uSock;
     this.RecvBuff  = RecvBuff;
 }
Пример #25
0
 public SendContext(UserSock uSock, SendBuffer sendBuff)
 {
     this.uUserSock = uSock;
     this.sendBuff  = sendBuff;
 }