示例#1
0
void SocketTrySend(HSocket hs, const char* ptr, int nbytes, int milliseconds, transresult_t& rt)
{
    rt.nbytes = 0;
    rt.nresult = 0;

    if(!ptr|| nbytes<1)
    {
        return;
    }


    int n;
    CMyTimeSpan start;

    while(1)
    {
        n = send(hs, ptr+rt.nbytes, nbytes, NONBLOCKREADWRITE|SENDNOSIGNAL);

        if(n>0)
        {
            rt.nbytes += n;
            nbytes -= n;

            if(rt.nbytes >= nbytes)
            {
                rt.nresult = 0;
                break;
            }
        }
        else if( n==0)
        {
            rt.nresult= -2;
            break;
        }
        else
        {
            n = GetLastSocketError();

            if(ETRYAGAIN(n))
            {
                // CLightThread::DiscardTimeSlice();
                int  dwSleep=20;
                std::this_thread::sleep_for(std::chrono::milliseconds(dwSleep*1000));
            }
            else
            {
                rt.nresult = -1;
                break;
            }
        }

        if(start.GetSpaninMilliseconds() > (unsigned int)milliseconds)
        {
            rt.nresult= 1;
            break;
        }
    }
}
示例#2
0
// if timeout occurs, nbytes=-1, nresult=1
// if socket error, nbyte=-1, nresult=-1
// if the other side has disconnected in either block mode or nonblock mode, nbytes=0, nresult=-1
void SocketTryRecv(HSocket hs, char* ptr, int nbytes, int milliseconds, transresult_t& rt)
{
    rt.nbytes = 0;
    rt.nresult = 0;

    if(!ptr|| nbytes<1)
    {
        return;
    }

    if(milliseconds>2)
    {
        CMyTimeSpan start;

        while(1)
        {
            rt.nbytes = recv(hs, ptr, nbytes, NONBLOCKREADWRITE);

            if(rt.nbytes>0)
            {
                break;
            }
            else if(rt.nbytes==0)
            {
                rt.nresult = -1;
                break;
            }
            else
            {
                rt.nresult = GetLastSocketError();

                if( ETRYAGAIN(rt.nresult))
                {
                    if(start.GetSpaninMilliseconds() > (unsigned int)milliseconds)
                    {
                        rt.nresult= 1;
                        break;
                    }

                    //CLightThread::DiscardTimeSlice();
                    int  dwSleep=20;
                    std::this_thread::sleep_for(std::chrono::milliseconds(dwSleep*1000));
                }
                else
                {
                    rt.nresult = -1;
                    break;
                }
            }

        }
    }
    else
    {
        SocketRecv(hs, ptr, nbytes, rt);
    }
}
示例#3
0
// if timeout occurs, nbytes=-1, nresult=1
// if socket error, nbyte=-1, nresult=-1
// if the other side has disconnected in either block mode or nonblock mode, nbytes=0, nresult=-1
void SocketTryRecv(HSocket hs, char *ptr, int nbytes, int milliseconds, transresult_t &rt)
{
    rt.nbytes = 0;
    rt.nresult = 0;
    if(!ptr|| nbytes<1) return;

    if(milliseconds>2)
    {
        CMyTimeSpan start;
        while(1)
        {
            rt.nbytes = recv(hs, ptr, nbytes, NONBLOCKREADWRITE);
            if(rt.nbytes>0)
            {
               break;
            }
            else if(rt.nbytes==0)
            {
                rt.nresult = -1;
                break;
            }
            else
            {
                rt.nresult = GetLastSocketError();
                if( ETRYAGAIN(rt.nresult))
                {
                   if(start.GetSpaninMilliseconds()>milliseconds)  { rt.nresult= 1; break;}
                   CLightThread::DiscardTimeSlice();
                }
                else
                {
                    rt.nresult = -1;
                    break;
                }
            }

        }
    }
    else
    {
        SocketRecv(hs, ptr, nbytes, rt);
    }
}
示例#4
0
//  nbytes= the count of bytes sent
// if timeout occurs, nresult=1
// if socket error,  nresult=-1,
// if the other side has disconnected in either block mode or nonblock mode, nresult=-2
void SocketTrySend(HSocket hs, const char *ptr, int nbytes, int milliseconds, transresult_t &rt)
{
    rt.nbytes = 0;
    rt.nresult = 0;
    if(!ptr|| nbytes<1) return;


    int n;
    CMyTimeSpan start;
    while(1)
    {
        n = send(hs, ptr+rt.nbytes, nbytes, NONBLOCKREADWRITE|SENDNOSIGNAL);
        if(n>0)
        {
            rt.nbytes += n;
            nbytes -= n;
            if(rt.nbytes >= nbytes) {    rt.nresult = 0;  break; }
        }
        else if( n==0)
        {
            rt.nresult= -2;
            break;
        }
        else
        {
            n = GetLastSocketError();
            if(ETRYAGAIN(n))
            {
                CLightThread::DiscardTimeSlice();
            }
            else
            {
                rt.nresult = -1;
                break;
            }
        }
        if(start.GetSpaninMilliseconds()>milliseconds)  { rt.nresult= 1; break;}
    }
}
示例#5
0
int CLightThreadMutex::TryLock(unsigned int dwMilliseconds)
{
	 // The function pthread_mutex_trylock() returns zero if a lock on the mutex object referenced by mutex is acquired. Otherwise, an error number is returned to indicate the error.
	 unsigned int us= dwMilliseconds*1000;
	 int rt = pthread_mutex_trylock(&m_hMutex);
	 if( rt == EBUSY)
		 {
			 CMyTimeSpan start;
			 while(rt == EBUSY)
				 {
					 if( start.GetSpaninMilliseconds()>dwMilliseconds)
						 {
							 rt = -1;
							 }
					 else
						 {
							 usleep(20000); //sleep 20ms
							 rt = pthread_mutex_trylock(&m_hMutex);
							 }
					 }
			 }
	 return rt;
}