void StreamingSocket::close() { #if JUCE_WINDOWS if (handle != SOCKET_ERROR || connected) closesocket (handle); connected = false; #else if (connected) { connected = false; if (isListener) { // need to do this to interrupt the accept() function.. StreamingSocket temp; temp.connect ("localhost", portNumber, 1000); } } if (handle != -1) ::close (handle); #endif hostName.clear(); portNumber = 0; handle = -1; isListener = false; }
void StreamingSocket::close() { #if JUCE_WINDOWS if (handle != SOCKET_ERROR || connected) closesocket (handle); connected = false; #else if (connected) { connected = false; if (isListener) { // need to do this to interrupt the accept() function.. StreamingSocket temp; temp.connect (IPAddress::local().toString(), portNumber, 1000); } } if (handle != -1) { ::shutdown (handle, SHUT_RDWR); ::close (handle); } #endif hostName.clear(); portNumber = 0; handle = -1; isListener = false; }
void StreamingSocket::close() { #if JUCE_WINDOWS closesocket (handle); connected = false; #else if (connected) { connected = false; if (isListener) { // need to do this to interrupt the accept() function.. StreamingSocket temp; temp.connect ("localhost", portNumber, 1000); } } ::close (handle); #endif hostName = String::empty; portNumber = 0; handle = -1; isListener = false; }
bool SunriseCheckWanMicroJob::runJob() { StreamingSocket streamingSocket; const int64 start = Time::getCurrentTime().toMilliseconds(); const bool ret = streamingSocket.connect ("google.com", 80, 5000); setProgress ((double) (Time::getCurrentTime().toMilliseconds() - start)); return (ret); }
static void closeSocket (std::atomic<int>& handle, CriticalSection& readLock, bool isListener, int portNumber, std::atomic<bool>& connected) noexcept { const SocketHandle h = handle.load(); handle = -1; #if JUCE_WINDOWS ignoreUnused (portNumber, isListener, readLock); if (h != (unsigned) SOCKET_ERROR || connected) closesocket (h); // make sure any read process finishes before we delete the socket CriticalSection::ScopedLockType lock (readLock); connected = false; #else if (connected) { connected = false; if (isListener) { // need to do this to interrupt the accept() function.. StreamingSocket temp; temp.connect (IPAddress::local().toString(), portNumber, 1000); } } if (h != -1) { // unblock any pending read requests ::shutdown (h, SHUT_RDWR); { // see man-page of recv on linux about a race condition where the // shutdown command is lost if the receiving thread does not have // a chance to process before close is called. On Mac OS X shutdown // does not unblock a select call, so using a lock here will dead-lock // both threads. #if JUCE_LINUX || JUCE_ANDROID CriticalSection::ScopedLockType lock (readLock); ::close (h); #else ::close (h); CriticalSection::ScopedLockType lock (readLock); #endif } } #endif }
SmugID SmugMug::uploadFile(int queue, int index) { SmugID retval; lock.enter(); UploadFile& uf = uploadQueue[queue]->getImageFileInfo(index); lock.exit(); int64 bytesDone = 0; MD5 md5(uf.file); Time start = Time::getCurrentTime(); startTimer(LOGOUT_TIMER); String headers; String filename = uf.file.getFileName(); headers = "PUT http://upload.smugmug.com/" + URL::addEscapeChars(filename, false) + " HTTP/1.1\r\n" + "Host: upload.smugmug.com\r\n" + "Content-Length: " + String(uf.file.getSize()) + "\r\n" + "Content-MD5: " + md5.toHexString() + "\r\n" + "X-Smug-SessionID: " + sessionId + "\r\n" + "X-Smug-Version: 1.2.2\r\n" + "X-Smug-ResponseType: REST\r\n" + "X-Smug-AlbumID: " + String(uploadQueue[queue]->getAlbumId().id) + "\r\n" + "X-Smug-FileName: " + filename + "\r\n\r\n"; #ifdef JUCE_DEBUG Logger::outputDebugString(headers); #endif const char* headerUtf8 = headers.toUTF8(); StreamingSocket soc; if (soc.connect("upload.smugmug.com", 80)) { int bytesWritten = soc.write(headerUtf8, (int)strlen(headerUtf8)); if (bytesWritten == -1) { uf.status = UploadFile::Failed; return retval; } FileInputStream* fos = uf.file.createInputStream(); if (fos) { char buffer[1024 * 8]; while (!fos->isExhausted()) { int in = fos->read(buffer, sizeof(buffer)); int out = soc.write(buffer, in); startTimer(LOGOUT_TIMER); if (in != out) { delete fos; uf.status = UploadFile::Failed; return retval; } else { bytesDone += in; uf.complete = float(bytesDone)/float(uf.file.getSize()); } if (uf.status == UploadFile::Cancelled) { delete fos; return retval; } } delete fos; } else { uf.status = UploadFile::Failed; return retval; } String response; response.preallocateBytes(1024); while (1) { char buffer; int read = soc.read(&buffer, 1, true); if (read == -1) break; response += buffer; if (response.endsWith(("\r\n\r\n")) || response.endsWith(("\n\n"))) { String len = response.fromFirstOccurrenceOf(("Content-Length: "), false, true); if (len.isNotEmpty()) { // normal mode String num; int i = 0; while (CharacterFunctions::isDigit(len[i])) num += len[i++]; int bytes = num.getIntValue(); char* buffer = new char[bytes + 1]; soc.read(buffer, bytes, true); buffer[bytes] = 0; response += buffer; delete[] buffer; } else { // chunked while (1) { String line; char ch; while (!line.endsWith("\r\n")) { soc.read(&ch, 1, true); line += ch; } int sz = line.getHexValue32(); if (sz == 0) break; char* buf = new char[sz + 1]; soc.read(buf, sz, true); buf[sz] = 0; response += buf; delete buf; soc.read(&ch, 1, true); soc.read(&ch, 1, true); } } #ifdef JUCE_DEBUG Logger::outputDebugString(response); #endif soc.close(); String xml = response.fromFirstOccurrenceOf(("<?xml"), true, true); XmlDocument doc(xml); XmlElement* e = doc.getDocumentElement(); if (e) { XmlElement* image = e->getChildByName(("Image")); if (image) { int val = image->getIntAttribute(("id")); if (val >= 0) { uf.status = UploadFile::Finished; uf.complete = 1.0f; uf.url = image->getStringAttribute("URL"); Time end = Time::getCurrentTime(); RelativeTime diff = end - start; addLogEntry(("Info: ") + uf.file.getFileName() + (" uploaded in ") + String(int(diff.inSeconds())) + (" seconds [") + String(uf.file.getSize() / 1024 / diff.inSeconds(), 1) + ("KB/s]")); retval.id = val; retval.key = image->getStringAttribute(("Key")); delete e; return retval; } } delete e; } } } } uf.status = UploadFile::Failed; return retval; }