Esempio n. 1
0
bool ConditionVariable::Wait(Mutex &lock, time_ms timeout_ms) {
  int result = 0;
  if(timeout_ms < 0) {
    result = pthread_cond_wait(&m_handle, &lock.m_handle);
    BASE_ASSERT(result == 0);
  } else {

    int kMsecPerSec = 1000;
    long kNsecPerMsec = 1000000;
    long kNsecPerSec = 1000000000;
    int kUsecPerMsec = 1000;
    // long kUsecPerSec = 1000000;
    timespec relative_time = {timeout_ms / kMsecPerSec,
                              (timeout_ms % kMsecPerSec) * kNsecPerMsec};

    struct timeval now;
    gettimeofday(&now, NULL);
    struct timespec absolute_time = {now.tv_sec, now.tv_usec / kUsecPerMsec};
    absolute_time.tv_sec +=
        relative_time.tv_sec + relative_time.tv_nsec / kNsecPerSec;
    absolute_time.tv_nsec =
        (absolute_time.tv_nsec + relative_time.tv_nsec) % kMsecPerSec;

    result = pthread_cond_timedwait(&m_handle, &lock.m_handle, &absolute_time);
    BASE_ASSERT(result == 0 || result == ETIMEDOUT);
  }

  return result == 0;
}
Esempio n. 2
0
LONG File::Seek(LONG lOff, LONG nFrom)
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);
    BASE_ASSERT(nFrom == begin || nFrom == end || nFrom == current);

    long dwNew = fseek(m_pStream,lOff,(int)nFrom);
    if (dwNew  == (unsigned long)-1)
        return(-1);

    return dwNew;
}
Esempio n. 3
0
File* File::Duplicate() const
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);

    File* pFile = new File(NULL);
    FILE *hFile;
    if((hFile=freopen(m_strFileName.c_str(),(const char*)m_strOpenFlags.c_str(),m_pStream))==NULL)
    {
        delete pFile;
        return(NULL);
    }
    pFile->m_pStream = hFile;
    BASE_ASSERT(pFile->m_pStream != NULL);
    pFile->m_bCloseOnDelete = m_bCloseOnDelete;
    return pFile;
}
Esempio n. 4
0
TBOOL File::Write(const void* lpBuf, LONG nCount)
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);

    if (nCount == 0)
        return(TRUE);     // avoid Win32 "null-write" option

    BASE_ASSERT(lpBuf != NULL);

    long nWritten;
    nWritten=(long)fwrite(lpBuf,sizeof(char),(size_t)nCount,m_pStream);

    if (nWritten != nCount)
        return(FALSE);
    else
        return(TRUE);
}
Esempio n. 5
0
LONG File::Read(void* lpBuf, LONG nCount)
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);

    if (nCount == 0)
        return 0;

    BASE_ASSERT(lpBuf != NULL);

    long dwRead;
    if((dwRead=fread(lpBuf, sizeof(char),(int)nCount,m_pStream))==0)
    {
        if(ferror(m_pStream)!=0)
            return(-1);
    }

    return dwRead;
}
Esempio n. 6
0
long File::GetPosition() const
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);

    long dwPos = ftell(m_pStream);
    if (dwPos  == (unsigned long)-1)
        return(-1);

    return dwPos;
}
Esempio n. 7
0
TBOOL AfxComparePath(LPCSTR lpszPath1, LPCSTR lpszPath2)
{
    // use case insensitive compare as a starter
    if (strcasecmp(lpszPath1, lpszPath2) != 0)
        return FALSE;

    int nLen = strlen(lpszPath1);
    if (nLen != (int)strlen(lpszPath2))
        return FALSE;
    
    BASE_ASSERT(nLen < _BOSS_MAX_PATH);

    return TRUE; // otherwise file name is truly the same
}
Esempio n. 8
0
void SimplePlugin::Recv(ConnectionHandle conn, void *buffer,
                        unsigned int nbytes,
                        std::function<void(void *, unsigned int)> func) {
  BASE_ASSERT(conn != 0);
  u32 nrecv = 0;
  int data_received = 0;
  void *work_buffer = buffer;
  unsigned int work_buffer_size = nbytes;

  int total_available = GetTotalRecvAvailable(conn);

  static const unsigned int kBufferTooSmall = -2;

  // only process limited amount of data in this call
  while(data_received < total_available) {
    do {
      if(nrecv == kBufferTooSmall) {
        // previous recv call failed because buffer was too small. allocating
        // bigger buffer.
        work_buffer_size = GetRecvAvailable(conn);
        work_buffer = malloc(work_buffer_size);
        PLUGIN_INFO("recv failed - buffer too small. %d expecting %d.",
                    work_buffer_size, work_buffer_size);
      }

      //			((char*)work_buffer)[work_buffer_size-1] = 0;

      nrecv = ::Recv(conn, work_buffer, work_buffer_size /*-1*/);

      if(0 < nrecv) {
        func(work_buffer, nrecv);
        data_received += nrecv;
      }

      if(work_buffer != buffer) {
        // free temp buffer and switch back to input buffer.
        free(work_buffer);
        work_buffer = buffer;
        work_buffer_size = nbytes;
      }

    } while(nrecv == kBufferTooSmall); // if buffer was too small try receiving
                                       // with bigger buffer.
  }
}
Esempio n. 9
0
rush_time_t RttInfo::PacketReceived(rush_sequence_t id, rush_time_t now) {
  rush_time_t rtt = 0;

  bool found_rtt = false;
  {
    for(u32 i = 0; i < kSampleCount; ++i) {
      if(m_id[i] == id) {
        rtt = now - m_send_time[i];
        found_rtt = true;
        break;
      }
    }
  }
  if(found_rtt) {
    BASE_ASSERT(rtt >= 0.f, "rtt value for %d at %f is %f", id, now, rtt);
    m_average += (rtt - m_average) * 0.05f; // kSmoothFactor;
  }
  return rtt;
}
Esempio n. 10
0
TBOOL File::Close()
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);

    TBOOL bError = FALSE;
    if (m_pStream != NULL)
        bError = (fclose(m_pStream)!=0);

    m_pStream = NULL;
    m_bCloseOnDelete = FALSE;
    m_strFileName.clear();

	

    if (bError)
        return(FALSE);
    return(TRUE);
}
Esempio n. 11
0
TBOOL File::LockRange(unsigned long dwPos, unsigned long dwCount)
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);

    LONG lCurrentPos=GetPosition();
    if(Seek(dwPos,File::begin)==FALSE)
    {
        return(FALSE);
    }

#ifdef WIN32
    if(_locking(fileno(m_pStream),_LK_LOCK,dwCount))
#else
    if(lockf(fileno(m_pStream),F_LOCK,dwCount))
#endif
        return(FALSE);
    Seek(lCurrentPos,File::begin);
    
    return(TRUE);
}
Esempio n. 12
0
UINT stGetFileName(LPCSTR lpszPathName, LPSTR lpszTitle, UINT nMax)
{
    BASE_ASSERT(lpszPathName!=NULL);

    // always capture the complete file name including extension (if present)
    LPSTR lpszTemp = (LPSTR)lpszPathName;
    for (LPCSTR lpsz = lpszPathName; *lpsz != '\0'; lpsz ++ ) //= _tcsinc(lpsz))
    {
        // remember last directory/drive separator
        if (*lpsz == '\\' || *lpsz == '/' || *lpsz == ':')
            lpszTemp = (LPSTR) (lpsz+1) ; //_tcsinc(lpsz);
    }

    // lpszTitle can be NULL which just returns the number of bytes
    if (lpszTitle == NULL)
        return strlen(lpszTemp)+1;

    // otherwise copy it into the buffer provided
    strncpy(lpszTitle, lpszTemp, nMax);
    return 0;
}
Esempio n. 13
0
unsigned long File::GetLength() const
{
    BASE_ASSERT_VALID(this);

    unsigned long dwLen, dwCur;

    // Seek is a non const operation
    File* pFile = (File*)this;
#ifndef WIN32 
    dwCur = pFile->Seek(0L, current);
    dwLen = pFile->SeekToEnd();
	BASE_VERIFY(dwCur == (unsigned long)pFile->Seek(dwCur, begin));
#else
	dwCur = pFile->GetPosition() ;
	BASE_ASSERT(pFile->SeekToEnd() == 0 ) ;
	dwLen = pFile->GetPosition() ;
	pFile->Seek(dwCur,begin) ;
#endif
   
    return dwLen;
}
Esempio n. 14
0
TBOOL File::Open(LPCSTR lpszFileName, LPCSTR lpszOpenFlags/*,TCFileException* pException*/)
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(lpszFileName!=NULL);


    m_bCloseOnDelete = FALSE;
    m_pStream = NULL; //(UINT)NULL;

    m_strFileName=lpszFileName;
    
    m_strOpenFlags=lpszOpenFlags;

    // attempt file creation
    FILE *hFile = fopen(lpszFileName, m_strOpenFlags.c_str());
    if (hFile == NULL)
    {
        return FALSE;
    }
    m_pStream = hFile;
    m_bCloseOnDelete = TRUE;

    return TRUE;
}
Esempio n. 15
0
void ConditionVariable::Terminate() {
  int ret = pthread_cond_destroy(&m_handle);
  BASE_ASSERT(ret == 0);
}
Esempio n. 16
0
void ConditionVariable::Initialize() {
  int ret = pthread_cond_init(&m_handle, NULL);
  BASE_ASSERT(ret == 0);
}
Esempio n. 17
0
void ConditionVariable::NotifyAll() {
  int res = pthread_cond_broadcast(&m_handle);
  BASE_ASSERT(res == 0);
}
Esempio n. 18
0
UINT stGetFileTitle(LPCSTR lpszPathName, LPSTR lpszTitle, UINT nMax)
{
    BASE_ASSERT(lpszPathName!=NULL);

    return(stGetFileName(lpszPathName,lpszTitle,nMax));
}
Esempio n. 19
0
void ConditionVariable::Notify() {
  int res = pthread_cond_signal(&m_handle);
  BASE_ASSERT(res == 0);
}
Esempio n. 20
0
// File does not support direct buffering (CMemFile does)
UINT File::GetBufferPtr(UINT nCommand, UINT /*nCount*/,
    void** /*ppBufStart*/, void** /*ppBufMax*/)
{
    BASE_ASSERT(nCommand == bufferCheck);
    return 0;   // no support
}
Esempio n. 21
0
bool BinaryWriter::WriteStringId(StringId value) {
  BASE_ASSERT(sizeof(StringId::Type) == sizeof(u32));

  const StringId::Type val = value.GetValue();
  return m_stream.Write(&val, 4);
}
Esempio n. 22
0
File::File(LPCSTR lpszFileName, LPCSTR lpszOpenFlags)
{
    BASE_ASSERT(lpszFileName!=NULL);

    Open(lpszFileName, lpszOpenFlags);
}