NS_IMETHODIMP MsgFileStream::Flush(void) { if (mFileDesc == nullptr) return NS_BASE_STREAM_CLOSED; int32_t cnt = PR_Sync(mFileDesc); if (cnt == -1) return ErrorAccordingToNSPR(); return NS_OK; }
/* unsigned long long available (); */ NS_IMETHODIMP MsgFileStream::Available(uint64_t *aResult) { if (!mFileDesc) return NS_BASE_STREAM_CLOSED; int64_t avail = PR_Available64(mFileDesc); if (avail == -1) return ErrorAccordingToNSPR(); *aResult = avail; return NS_OK; }
NS_IMETHODIMP MsgFileStream::Tell(int64_t *result) { if (mFileDesc == nullptr) return NS_BASE_STREAM_CLOSED; int64_t cnt = PR_Seek64(mFileDesc, 0, PR_SEEK_CUR); if (cnt == int64_t(-1)) { return ErrorAccordingToNSPR(); } *result = cnt; return NS_OK; }
NS_IMETHODIMP MsgFileStream::Write(const char *buf, uint32_t count, uint32_t *result) { if (mFileDesc == nullptr) return NS_BASE_STREAM_CLOSED; int32_t cnt = PR_Write(mFileDesc, buf, count); if (cnt == -1) { return ErrorAccordingToNSPR(); } *result = cnt; return NS_OK; }
/* [noscript] unsigned long read (in charPtr aBuf, in unsigned long aCount); */ NS_IMETHODIMP MsgFileStream::Read(char * aBuf, uint32_t aCount, uint32_t *aResult) { if (!mFileDesc) { *aResult = 0; return NS_OK; } int32_t bytesRead = PR_Read(mFileDesc, aBuf, aCount); if (bytesRead == -1) return ErrorAccordingToNSPR(); *aResult = bytesRead; return NS_OK; }
NS_IMETHODIMP MsgFileStream::Seek(int32_t whence, int64_t offset) { if (mFileDesc == nullptr) return NS_BASE_STREAM_CLOSED; bool seekingToEnd = whence == PR_SEEK_END && offset == 0; if (seekingToEnd && mSeekedToEnd) return NS_OK; int64_t cnt = PR_Seek64(mFileDesc, offset, (PRSeekWhence)whence); if (cnt == int64_t(-1)) { return ErrorAccordingToNSPR(); } mSeekedToEnd = seekingToEnd; return NS_OK; }
NS_IMETHODIMP nsServerSocket::InitWithAddress(const PRNetAddr *aAddr, int32_t aBackLog) { NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED); nsresult rv; // // configure listening socket... // mFD = PR_OpenTCPSocket(aAddr->raw.family); if (!mFD) { NS_WARNING("unable to create server socket"); return ErrorAccordingToNSPR(PR_GetError()); } PRSocketOptionData opt; opt.option = PR_SockOpt_Reuseaddr; opt.value.reuse_addr = true; PR_SetSocketOption(mFD, &opt); opt.option = PR_SockOpt_Nonblocking; opt.value.non_blocking = true; PR_SetSocketOption(mFD, &opt); if (PR_Bind(mFD, aAddr) != PR_SUCCESS) { NS_WARNING("failed to bind socket"); goto fail; } if (aBackLog < 0) aBackLog = 5; // seems like a reasonable default if (PR_Listen(mFD, aBackLog) != PR_SUCCESS) { NS_WARNING("cannot listen on socket"); goto fail; } // get the resulting socket address, which may be different than what // we passed to bind. if (PR_GetSockName(mFD, &mAddr) != PR_SUCCESS) { NS_WARNING("cannot get socket name"); goto fail; } // Set any additional socket defaults needed by child classes rv = SetSocketDefaults(); if (NS_WARN_IF(NS_FAILED(rv))) { goto fail; } // wait until AsyncListen is called before polling the socket for // client connections. return NS_OK; fail: rv = ErrorAccordingToNSPR(PR_GetError()); Close(); return rv; }