void TCPSocket::setSendTimeout(double timeout) { timeval tv = DUNE_TIMEVAL_INIT_SEC_FP(timeout); if (setsockopt(m_handle, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)) < 0) throw NetworkError(DTR("unable to set send timeout"), getLastErrorMessage()); }
void ClientSocket::tcpNoDelay(bool enable) { const bool ret = setSocketFlag(sockFd, IPPROTO_TCP, TCP_NODELAY, enable); if(!ret) { throw std::runtime_error(std::string("error setting TCP_NODELAY: ") + getLastErrorMessage()); } }
size_t ClientSocket::read(void* data, size_t len) { const ssize_t ret = ::recv(sockFd, (RecvBufferType)data, len, 0); if(ret <= 0) { throw std::runtime_error(std::string("error reading from socket: ") + getLastErrorMessage()); } return ret; }
void Library::assertIsLoaded() { if( !isLoaded() ) { std::string errorMsg; getLastErrorMessage( errorMsg ); CORAL_THROW( ModuleLoadException, "error loading library '" << _fileName << "': " << errorMsg ); } }
Address TCPSocket::getBoundAddress(void) { sockaddr_in name = {0}; socklen_t size = sizeof(name); if (getsockname(m_handle, (sockaddr*)&name, &size) != 0) throw NetworkError(DTR("unable to get bound address"), getLastErrorMessage()); return Address(Utils::ByteCopy::fromBE((uint32_t)name.sin_addr.s_addr)); }
uint16_t TCPSocket::getBoundPort(void) { sockaddr_in name = {0}; socklen_t size = sizeof(name); if (getsockname(m_handle, (sockaddr*)&name, &size) != 0) throw NetworkError(DTR("unable to get bound port"), getLastErrorMessage()); return Utils::ByteCopy::fromBE(name.sin_port); }
void TCPSocket::connect(const Address& addr, uint16_t port) { sockaddr_in ad; ad.sin_family = AF_INET; ad.sin_port = Utils::ByteCopy::toBE(port); ad.sin_addr.s_addr = addr.toInteger(); if (::connect(m_handle, (struct sockaddr*)&ad, sizeof(ad)) < 0) throw NetworkError(DTR("unable to connect"), getLastErrorMessage()); }
TCPSocket::TCPSocket(bool create): m_handle(INVALID_SOCKET) { if (create) { m_handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_handle == INVALID_SOCKET) throw NetworkError(DTR("unable to create socket"), getLastErrorMessage()); disableSIGPIPE(); } }
Error closeLibrary(void* pLib) { if (!::FreeLibrary((HMODULE)pLib)) { Error error = systemError(::GetLastError(), ERROR_LOCATION); error.addProperty("dlerror", getLastErrorMessage()); return error; } else { return Success(); } }
void ClientSocket::write(const void* data, size_t len) { size_t sent = 0; while(sent < len) { const ssize_t ret = ::send(sockFd, (const char*)data + sent, len - sent, 0); if(ret <= 0) { throw std::runtime_error(std::string("error writing to socket: ") + getLastErrorMessage()); } sent += ret; } }
bool ClientSocket::writable() { if(sockFd < 0) return false; struct pollfd pfd; pfd.fd = sockFd; pfd.events = POLLOUT; const int ret = poll(&pfd, 1, 0); if(ret < 0) { throw std::runtime_error(std::string("error polling socket: ") + getLastErrorMessage()); } return ret > 0; }
Error loadSymbol(void* pLib, const std::string& name, void** ppSymbol) { *ppSymbol = NULL; *ppSymbol = (void*)::GetProcAddress((HINSTANCE)pLib, name.c_str()); if (*ppSymbol == NULL) { Error error = systemError(::GetLastError(), ERROR_LOCATION); error.addProperty("dlerror", name + " - " + getLastErrorMessage()); return error; } else { return Success(); } }
Error loadLibrary(const std::string& libPath, void** ppLib) { // use *ppLib = NULL; *ppLib = (void*)::LoadLibraryEx(libPath.c_str(), NULL, 0); if (*ppLib == NULL) { Error error = systemError(::GetLastError(), ERROR_LOCATION); error.addProperty("dlerror", libPath + " - " + getLastErrorMessage()); return error; } else { return Success(); } }
void Library::doUnload() { #ifdef CORAL_OS_WIN bool unloaded = ( FreeLibrary( static_cast<HINSTANCE>( _handle ) ) != FALSE ); #else bool unloaded = ( _noDlClose || ( dlclose( _handle ) == 0 ) ); #endif if( !unloaded ) { std::string errorMsg; getLastErrorMessage( errorMsg ); CORAL_THROW( ModuleLoadException, "error unloading library '" << _fileName << "': " << errorMsg ); } _handle = 0; }
size_t TCPSocket::doRead(uint8_t* bfr, size_t size) { ssize_t rv = ::recv(m_handle, (char*)bfr, size, 0); if (rv == 0) { throw ConnectionClosed(); } else if (rv < 0) { if (errno == ECONNRESET) throw ConnectionClosed(); throw NetworkError(DTR("error receiving data"), getLastErrorMessage()); } return static_cast<size_t>(rv); }
int TCPSocket::read(char* bfr, int size) { int rv = ::recv(m_handle, bfr, size, 0); if (rv == 0) { throw ConnectionClosed(); } else if (rv == -1) { if (errno == ECONNRESET) throw ConnectionClosed(); throw NetworkError(DTR("error receiving data"), getLastErrorMessage()); } return rv; }
size_t TCPSocket::doWrite(const uint8_t* bfr, size_t size) { int flags = 0; #if defined(MSG_NOSIGNAL) flags = MSG_NOSIGNAL; #endif ssize_t rv = ::send(m_handle, (char*)bfr, size, flags); if (rv < 0) { if (errno == EPIPE) throw ConnectionClosed(); throw NetworkError(DTR("error sending data"), getLastErrorMessage()); } return static_cast<size_t>(rv); }
int TCPSocket::write(const char* bfr, int size) { int flags = 0; #if defined(MSG_NOSIGNAL) flags = MSG_NOSIGNAL; #endif int rv = ::send(m_handle, bfr, size, flags); if (rv == -1) { if (errno == EPIPE) throw ConnectionClosed(); throw NetworkError(DTR("error sending data"), getLastErrorMessage()); } return rv; }
TCPSocket* TCPSocket::accept(Address* a, uint16_t* port) { sockaddr_in addr; socklen_t size = sizeof(addr); int rv = ::accept(m_handle, (sockaddr*)&addr, &size); if (rv == 0) throw NetworkError(DTR("failed to accept connection"), getLastErrorMessage()); if (a) *a = (sockaddr*)&addr; if (port) *port = Utils::ByteCopy::fromBE(addr.sin_port); TCPSocket* ns = new TCPSocket(false); ns->m_handle = rv; ns->disableSIGPIPE(); return ns; }
void TCPSocket::bind(uint16_t port, Address addr, bool reuse) { if (reuse) { int on = 1; #if defined(DUNE_OS_WINDOWS) setsockopt(m_handle, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&on, sizeof(int)); on = 1; #endif setsockopt(m_handle, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(int)); } sockaddr_in self; self.sin_family = AF_INET; self.sin_port = Utils::ByteCopy::toBE(port); self.sin_addr.s_addr = addr.toInteger(); if (::bind(m_handle, (::sockaddr*)&self, sizeof(self)) < 0) throw NetworkError(DTR("unable to bind to socket"), getLastErrorMessage()); }
ClientSocket::ClientSocket(const char* host, const char* port) : sockFd(-1), streamBuf(this) { struct addrinfo hints; struct addrinfo* res = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC;// use IPv4 or IPv6, whichever hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE;// fill in my IP for me if(getaddrinfo(host, port, &hints, &res)) { throw std::runtime_error(std::string("error getting address info for ") + host + ":" + port + " (" + getLastErrorMessage() + ")"); } sockFd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(sockFd < 0) { freeaddrinfo(res); throw std::runtime_error(std::string("error connecting to ") + host + ":" + port + " (" + getLastErrorMessage() + ")"); } setSocketFlag(sockFd, SOL_SOCKET, SO_REUSEADDR, true); setSocketFlag(sockFd, SOL_SOCKET, SO_KEEPALIVE, true); if(connect(sockFd, res->ai_addr, res->ai_addrlen)) { freeaddrinfo(res); close(); throw std::runtime_error(std::string("error connecting to ") + host + ":" + port + "(" + getLastErrorMessage() + ")"); } freeaddrinfo(res); }
/*--------------------------------------------------------------------------*/ int C2F(sci_lasterror)(char *fname,unsigned long fname_len) { SciErr sciErr; BOOL bClearLastError = TRUE; int NbLines = 0; const char **errorMessage = NULL; Rhs = Max(0, Rhs); CheckRhs(0,1); CheckLhs(1,4); if (Rhs == 1) { int *piAddressVarOne = NULL; /* get Address of inputs */ sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (isBooleanType(pvApiCtx, piAddressVarOne)) { if (isScalar(pvApiCtx, piAddressVarOne)) { getScalarBoolean(pvApiCtx, piAddressVarOne, &bClearLastError); } else { Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 1); } } else { Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 1); } } errorMessage = getLastErrorMessage(&NbLines); if ((NbLines <= 0) || (errorMessage == NULL)) { createEmptyMatrix(pvApiCtx, Rhs + 1); } else { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, NbLines, 1, (char**)errorMessage); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999,_("%s: Memory allocation error.\n"), fname); return 0; } } LhsVar(1) = Rhs + 1; if (Lhs >= 2) { double dLastErrorValue = (double) getLastErrorValue(); createScalarDouble(pvApiCtx, Rhs + 2, dLastErrorValue); LhsVar(2) = Rhs + 2; } if (Lhs >= 3) { double dLinePosition = (double) getLastErrorLinePosition(); createScalarDouble(pvApiCtx, Rhs + 3, dLinePosition); LhsVar(3) = Rhs + 3; } if (Lhs == 4) { createSingleString(pvApiCtx, Rhs + 4, (char*)getLastErrorFunctionName()); LhsVar(4) = Rhs + 4; } if (bClearLastError) { clearLastError(); } PutLhsVar(); return 0; }
///----------------------------------------------------------------------------- ///! @brief TODO enter a description ///! @remark ///----------------------------------------------------------------------------- void convertToCString(const std::wstring& str, std::string& out) { char buffer[4096]; // BOOL usedDefaultChar = FALSE; int numberOfCChars = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, str.c_str(), (int)str.size(), buffer, 0, 0, 0); if (numberOfCChars > 0) { out.reserve(numberOfCChars); WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, str.c_str(), (int)str.size(), buffer, numberOfCChars, 0, 0); buffer[numberOfCChars] = 0; out = buffer; } else { DWORD error = GetLastError(); HRESULT hr = HRESULT_FROM_WIN32(error); MSG_TRACE_CHANNEL("String Conversion Error", "Failed to convert from MB to UTF8 with Hresult: 0x%08x, %s", hr, getLastErrorMessage(error)); } }
///----------------------------------------------------------------------------- ///! @brief TODO enter a description ///! @remark ///----------------------------------------------------------------------------- void convertToUTF16String(const std::string& str, std::wstring& out) { wchar_t buffer[4096]; int numberOfWideChars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), (int)str.size(), buffer, 0); if (numberOfWideChars > 0) { out.reserve(numberOfWideChars); MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), (int)str.size(), buffer, numberOfWideChars); buffer[numberOfWideChars] = 0; out = buffer; } else { DWORD error = GetLastError(); HRESULT hr = HRESULT_FROM_WIN32(error); MSG_TRACE_CHANNEL("String Conversion Error", "Failed to convert from UTF8 to MB with Hresult: 0x%08x, %s", hr, getLastErrorMessage(error)); } }
///----------------------------------------------------------------------------- ///! @brief TODO enter a description ///! @remark ///----------------------------------------------------------------------------- void MouseController::initialise(HWND hwnd) { RAWINPUTDEVICE rawInputDevice; rawInputDevice.usUsagePage = 0x01; rawInputDevice.usUsage = 0x02; rawInputDevice.dwFlags = RIDEV_NOLEGACY; // adds HID mouse and also ignores legacy mouse messages rawInputDevice.hwndTarget = hwnd; m_connected = true; //InputSystem.addRawInputDevice(rawInputDevice); if (RegisterRawInputDevices(&rawInputDevice, 1, sizeof(RAWINPUTDEVICE)) == FALSE) { m_connected = false; unsigned int error = GetLastError(); MSG_TRACE_CHANNEL("MOUSE CONTROLLER ERROR:", "Failed to register the mouse device with error: %d, %s", error, getLastErrorMessage(error)); } HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); MONITORINFO info; info.cbSize = sizeof(MONITORINFO); GetMonitorInfo(monitor, &info); m_monitorWidth = info.rcMonitor.right - info.rcMonitor.left; m_monitorHeight = info.rcMonitor.bottom - info.rcMonitor.top; }
//#if defined(_WIN32) && !defined(WITHOUT_GUI) //int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) //#else int main(int argc, char *argv[]) //#endif { int iRet = 0; #ifdef ENABLE_MPI initScilabMPI(); #endif ScilabEngineInfo* pSEI = InitScilabEngineInfo(); #ifdef WITHOUT_GUI /* Building Scilab-cli-bin. We won't ever had the gui nor the jvm */ pSEI->iConsoleMode = 1; pSEI->iNoJvm = 1; setScilabMode(SCILAB_NWNI); #else setScilabMode(SCILAB_STD); #endif //#if defined(_WIN32) && !defined(WITHOUT_GUI) // { // LPSTR my_argv[256]; // int nArgs = 0; // LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); // if (szArglist) // { // for (int i = 0; i < nArgs; i++) // { // my_argv[i] = wide_string_to_UTF8(szArglist[i]); // } // LocalFree(szArglist); // } // get_option(nArgs, my_argv, pSEI); // } //#else get_option(argc, argv, pSEI); //#endif // if WITHOUT_GUI is defined // force Terminal IO -> Terminal IO + StartScilabEngine // WITHOUT_GUI (All Platform) => Terminal IO + StartScilabEngine // GUI (MacOSX) => [no option] -> Console IO + InitMacOSXEnv // | [-nwni] -> Terminal IO + StartScilabEngine // | [-nw] -> Terminal IO + InitMacOSXEnv #ifndef WITHOUT_GUI if (pSEI->iConsoleMode) { setScilabMode(SCILAB_NW); setScilabInputMethod(&getCmdLine); setScilabOutputMethod(&TermPrintf); #if defined(__APPLE__) if (pSEI->iNoJvm == 0) { return initMacOSXEnv(pSEI); } #endif // !defined(__APPLE__) } else { setScilabMode(SCILAB_STD); setScilabInputMethod(&ConsoleRead); setScilabOutputMethod(&ConsolePrintf); #if defined(__APPLE__) return initMacOSXEnv(pSEI); #endif // !defined(__APPLE__) } #else setScilabMode(SCILAB_NWNI); setScilabInputMethod(&getCmdLine); setScilabOutputMethod(&TermPrintf); #endif // defined(WITHOUT_GUI) if (pSEI->iShowVersion == 1) { disp_scilab_version(); exit(0); } int val = setjmp(ScilabJmpEnv); if (!val) { StartScilabEngine(pSEI); iRet = RunScilabEngine(pSEI); StopScilabEngine(pSEI); FREE(pSEI); return iRet; } else { // We probably had a segfault so print error std::wcerr << getLastErrorMessage() << std::endl; return val; } }
void TCPSocket::listen(int backlog) { if (::listen(m_handle, backlog) < 0) throw NetworkError(DTR("unable to listen"), getLastErrorMessage()); }