示例#1
0
        public void ConnectServer(string hostNameOrAddress, int port, TcpSocketInvokeElement invokeElement)
        {
#if DEBUG_NETIO
            UnityEngine.Debug.Log(string.Format("Connect :{0} {1}", hostNameOrAddress, port));
#endif
            Interlocked.Exchange(ref tcpSocketState, 0);
            tcpClient.ConnectServer(hostNameOrAddress, port, invokeElement);
        }
示例#2
0
        public void ConnectServer(string hostNameOrAddress, int port, Action onConnectCompleted, Action <SocketError> onSocketError)
        {
            var invokeElement = new TcpSocketInvokeElement();

            invokeElement.OnSocketCompleted  = ConnectCompleted;
            invokeElement.OnSocketCompleted += onConnectCompleted;
            invokeElement.OnSocketError      = onSocketError;
            ConnectServer(hostNameOrAddress, port, invokeElement);
        }
示例#3
0
        private bool Reconnect(TcpSocketUserToken userToken, TcpSocketInvokeElement invokeElement, int maxCount)
        {
            if (userToken.TcpSocket != null)
            {
                if (Interlocked.Exchange(ref tcpSocketState, -1) != -1)
                {
                    if (maxCount > 0 && reconnectCount >= maxCount)
                    {
                        return(false);
                    }

                    if (!(!userToken.TcpSocket.Connected || (userToken.TcpSocket.Poll(1000, SelectMode.SelectRead) && (userToken.TcpSocket.Available == 0))))
                    {
                        UnityEngine.Debug.LogWarning("Reconnect --> TcpSocket is Connected");

                        ReconnectCompleted();
                        // invokeElement.SocketCompletedInvoke(userToken);
                        SendMsgPacket();
                    }
                    else
                    {
                        UnityEngine.Debug.LogWarning("Reconnect --> TcpSocket is not Connected");

                        // invokeElement.ClearSendMsgPool();
                        if (heartbeatTread != null)
                        {
                            heartbeatTread.Abort();
                        }
                        reconnectCount++;
                        tcpClient.ReconnectSocket(invokeElement);
                    }
                }
                return(true);
            }
            return(false);
        }
示例#4
0
 public bool RetryReconnect(TcpSocketInvokeElement invokeElement)
 {
     Interlocked.Exchange(ref tcpSocketState, -2);
     return(Reconnect(tcpClient.UserToken, invokeElement, 0));
 }
示例#5
0
 public bool TryReconnect(TcpSocketUserToken userToken, TcpSocketInvokeElement invokeElement, int maxCount = 1)
 {
     return(Reconnect(userToken, invokeElement, maxCount));
 }