Пример #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            USocket client = (USocket)ar.AsyncState;

            try
            {
                if (client.timeoutCheckTimer != null)
                {
                    client.timeoutCheckTimer.Dispose();
                    client.timeoutCheckTimer = null;
                }
                if (client.isActive)
                {
                    client.failTimes = 0;
                    //从远程设备读取Number据
                    int bytesRead = client.mSocket.EndReceive(ar);
                    if (bytesRead > 0)
                    {
                        //Debug.Log("receive len==" + bytesRead);
                        // 有Number据,存储.
                        onReceiveCallback(client, client.mTmpBuffer, bytesRead);
                    }
                    else if (bytesRead < 0)
                    {
                        client.onConnectStateChg(false, Errors.receivebytesLenError);
                    }
                    else
                    {
                        // 所有Number据读取完毕.
                        Debug.Log("receive zero=====" + bytesRead);
                        client.onConnectStateChg(false, Errors.serverClosedConnection);
                        return;
                    }

                    // 继续读取.
                    client.ReceiveAsync();
                }
                else
                {
                    client.onConnectStateChg(false, Errors.connectionIsClosed);
                }
            }
            catch (Exception e)
            {
                Debug.Log(e);
            }
        }
Пример #2
0
        private void connectCallback(IAsyncResult ar)
        {
            // 从stateobject获取socket.
            USocket client = (USocket)ar.AsyncState;

            if (client.mSocket.Connected)
            {
                // 完成连接.
                client.mSocket.EndConnect(ar);
                client.isActive  = true;
                client.failTimes = 0;
                client.onConnectStateChg(true, Errors.success);
            }
            else
            {
                client.onConnectStateChg(false, Errors.connectFailed);
            }
            if (connectTimeout != null)
            {
                connectTimeout.Dispose();
                connectTimeout = null;
            }
        }