/*static*/ KValueRef CodecBinding::CreateZipAsync(const ValueList& args) { std::string directory = args.GetString(0); std::string zipFile = args.GetString(1); AutoPtr<AsyncJob> job = args.GetObject(2).cast<AsyncJob>(); KMethodRef callback = 0; if (args.size() > 3) { callback = args.GetMethod(3); } Poco::Path path(directory); path.makeDirectory(); std::ofstream stream(UTF8ToSystem(zipFile).c_str(), std::ios::binary | std::ios::trunc); Poco::Zip::Compress compressor(stream, true); try { compressor.addRecursive(path); } catch (std::exception& e) { Logger::Get("Codec")->Error("exception compressing: %s", e.what()); throw ValueException::FromFormat("Exception during zip: %s", e.what()); } compressor.close(); stream.close(); if (!callback.isNull()) { ValueList args; args.push_back(Value::NewString(zipFile)); RunOnMainThread(callback, args, true); } return Value::Undefined; }
void TCPSocketBinding::Write(const ValueList& args, KValueRef result) { args.VerifyException("Send", "o|s"); static std::string eprefix("TCPSocketBinding::Write: "); if (!this->opened && !this->connectThread.isRunning()) throw ValueException::FromString(eprefix + "Socket is not open"); BlobRef data(0); if (args.at(0)->IsString()) { std::string sendString(args.GetString(0)); data = new Blob(sendString.c_str(), sendString.size()); } else if (args.at(0)->IsObject()) { KObjectRef dataObject(args.GetObject(0)); data = dataObject.cast<Blob>(); } if (data.isNull()) throw ValueException::FromString("Cannot send non-Blob object"); { Poco::Mutex::ScopedLock lock(sendDataMutex); sendData.push(data); // Only install the ReadyForWrite handler when there is actually data // to write, because otherwise the CPU usage will spike to 100% if (!writeReadyHandlerInstalled) { this->reactor.addEventHandler(this->socket, writeObserver); writeReadyHandlerInstalled = true; } } result->SetBool(true); }
void TCPServerConnectionBinding::Write(const ValueList& args, KValueRef result) { args.VerifyException("Write", "o|s"); static std::string eprefix("TCPServerConnectionBinding::Write: "); if (this->closed) throw ValueException::FromString(eprefix + "Socket is not open"); BytesRef data(0); if (args.at(0)->IsString()) { std::string sendString(args.GetString(0)); data = new Bytes(sendString.c_str(), sendString.size()); } else if (args.at(0)->IsObject()) { KObjectRef dataObject(args.GetObject(0)); data = dataObject.cast<Bytes>(); } if (data.isNull()) throw ValueException::FromString("Cannot send non-Bytes object"); { Poco::Mutex::ScopedLock lock(sendDataMutex); sendData.push(data); // Only install the ReadyForWrite handler when there is actually data // to write, because otherwise the CPU usage will spike to 100% if (!writeReadyHandlerInstalled) { this->reactor.addEventHandler(socket, Poco::NObserver<TCPServerConnectionBinding, Poco::Net::WritableNotification>(*this, &TCPServerConnectionBinding::onWritable)); writeReadyHandlerInstalled = true; } } result->SetBool(true); }
void Bytes::_Write(const ValueList& args, KValueRef result) { args.VerifyException("write", "s|o ?n"); int offset = args.GetInt(1, 0); int bytesWritten; if (args.at(0)->IsString()) { const char* str = args.at(0)->ToString(); size_t length = strlen(str); bytesWritten = Write(str, length, offset); } else { BytesRef data(args.GetObject(0).cast<Bytes>()); if (data.isNull()) { throw ValueException::FromString("May only write strings or Bytes object"); } bytesWritten = Write(data, offset); } result->SetInt(bytesWritten); }
void UIBinding::_CreateWindow(const ValueList& args, KValueRef result) { AutoPtr<WindowConfig> config(0); if (args.size() > 0 && args.at(0)->IsObject()) { config = WindowConfig::FromProperties(args.GetObject(0)); } else if (args.size() > 0 && args.at(0)->IsString()) { std::string url(args.GetString(0)); config = AppConfig::Instance()->GetWindowByURL(url); if (config.isNull()) { config = WindowConfig::Default(); config->SetURL(url); } } // If we still do not have a configuration, just use the default. if (config.isNull()) config = WindowConfig::Default(); result->SetObject(UserWindow::CreateWindow(config, 0)); }