void Connection::makeSet(int max){ ScopedMutexLock(pimpl_->dataAccess); // remove from old group if( pimpl_->group && pimpl_->userSocket ){ SDLNet_TCP_DelSocket(pimpl_->group, pimpl_->userSocket); } // if internal set was used, destroy it if( pimpl_->createdSet ) { SDLNet_FreeSocketSet(pimpl_->group); pimpl_->group = NULL; pimpl_->createdSet = false; } //create new set pimpl_->group = SDLNet_AllocSocketSet(max); if( pimpl_->group == NULL ){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "SDLNet_AllocSocketSet(" << max << "): " << SDLNet_GetError() << std::endl; exit(EXIT_FAILURE); } else { // add self to new group if( pimpl_->userSocket ){ SDLNet_TCP_AddSocket(pimpl_->group, pimpl_->userSocket); } pimpl_->createdSet = 1; } }
const void * BufferedTransport::peek(size_t &size) { ScopedMutexLock(lock_); if(remainingSize() < size) { size = remainingSize(); } return currentPtr(); }
int Connection::send(const inp::INFPacket& data){ // prepare packet for sending PacketList packList; splitForSend(data, packList); // if there is nothing to send, leave. if( packList.empty() ){ return 1; } ScopedMutexLock(pimpl_->dataAccess); if( pimpl_->userSocket != NULL ){ // Send packets in list for(size_t i = 0; i < packList.size(); ++i ){ if( !packList[i].empty() ){ //check if everything went through if( SDLNet_TCP_Send( pimpl_->userSocket, &packList[i][0], packList[i].size() ) < packList[i].size() ) { SDLNet_TCP_Close( pimpl_->userSocket ); pimpl_->userSocket = NULL; pimpl_->peer = NULL; pimpl_->active = false; pimpl_->connectTime = 0; pimpl_->lastSent.clear(); return -1; } } } pimpl_->lastSent = packList; } else { pimpl_->connectTime = 0; return -1; } return 1; }
FileSystemError SimpleFileSystemVolume::open(const UnicodeString& filename, FileReader& file) const { auto lock = ScopedMutexLock(mutex_); for (auto entry : entries_) { if (filename == entry->name) { auto data = Vector<byte_t>(); try { data.resize(entry->data.size()); } catch (const std::bad_alloc&) { return OutOfMemoryFileSystemError; } memcpy(data.getData(), entry->data.getData(), data.size()); file.openMemoryFile(data); return NoFileSystemError; } } return ResourceMissingFileSystemError; }
void BufferedTransport::fastforward(size_t size) { ScopedMutexLock(lock_); int temp = readPos_ + size; if(temp > (int)buffer_.size()) throw Exception("readpos exceed buffer size by fastforward"); readPos_ = temp; }
int Connection::connect(const std::string &host, int port){ if( (port == 0) || isSpace(host) ){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "Connection::connect(string, int): Port can't be 0. Host can't be whitespace." << std::endl; return -1; } IPaddress ip; if( SDLNet_ResolveHost(&ip, host.c_str(), port) == -1 ){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "SDLNet_ResolveHost: " << SDLNet_GetError() << std::endl; return -1; } ScopedMutexLock(pimpl_->dataAccess); //make sure is dissconected if( pimpl_->userSocket != NULL ){ SDLNet_TCP_Close( pimpl_->userSocket ); pimpl_->userSocket = NULL; } pimpl_->userSocket = SDLNet_TCP_Open(&ip); if( pimpl_->userSocket == NULL ){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "SDLNet_TCP_Open: " << SDLNet_GetError() << std::endl; pimpl_->active = false; pimpl_->connectTime = 0; return -1; } pimpl_->peer = SDLNet_TCP_GetPeerAddress(pimpl_->userSocket); pimpl_->active = true; pimpl_->connectTime = SDL_GetTicks(); return 1; }
void Connection::clean(){ ScopedMutexLock(pimpl_->dataAccess); --pimpl_->refCount; if( pimpl_->refCount == 0 ){ if( pimpl_->group != NULL ){ if( pimpl_->userSocket ){ SDLNet_TCP_DelSocket(pimpl_->group, pimpl_->userSocket); } if( pimpl_->createdSet ){ SDLNet_FreeSocketSet(pimpl_->group); pimpl_->createdSet = 0; } pimpl_->group = NULL; } if( pimpl_->userSocket != NULL ){ SDLNet_TCP_Close( pimpl_->userSocket ); pimpl_->userSocket = NULL; } pimpl_->peer = NULL; pimpl_->active = false; pimpl_->connectTime = 0; delete pimpl_; } pimpl_ = NULL; }
void connect() { if(redisContext_) { throw IllegalStateException("Error redisContext already created"); } state_ = REDISREQUEST_CONNECTING; ScopedMutexLock(lockRedis_); redisContext_ = redisAsyncConnect(host_.c_str(), port_); if (redisContext_->err) { _LOG_DEBUG("REDIS CONNECT FAILED (CREATE ERROR): %s:%d, err = %x, this = %p" , host_.c_str(), port_, redisContext_->err, this); state_ = REDISREQUEST_CONNECTFAILED; //fire_onRedisRequest_Error(redisContext_->err, "connect error", NULL); // disconnectCallback() is called later soon.. // error process will be executed by that function. } redisContext_->data = this; redisLibeventAttach(redisContext_, (struct event_base *)ioService_->coreHandle()); redisAsyncSetConnectCallback(redisContext_, connectCallback); redisAsyncSetDisconnectCallback(redisContext_, disconnectCallback); timerObj_->setTimer(TIMER_ID_CONNECTION_TIMEOUT, DEFAULT_CONNECT_TIMEOUT, false); _LOG_DEBUG("redis connect start : %s:%d, flag = 0x%x, fd = %d, context = %p, this = %p" , host_.c_str(), port_, redisContext_->c.flags, redisContext_->c.fd, redisContext_, this); }
void Logfile::removeOutputSink(OutputSink* sink) { Members::outputSinks.erase(sink); auto lock = ScopedMutexLock(queuedOutputSinkCallsMutex); queuedOutputSinkCalls.eraseIf([&](const QueuedOutputSinkCall& call) { return call.sink == sink; }); }
void Connection::setSocket(const TCPsocket& newUserSocket){ ScopedMutexLock(pimpl_->dataAccess); pimpl_->userSocket = newUserSocket; pimpl_->peer = SDLNet_TCP_GetPeerAddress(pimpl_->userSocket); pimpl_->active = true; pimpl_->connectTime = SDL_GetTicks(); }
void BufferedTransport::setReadPos(size_t pos) { ScopedMutexLock(lock_); if(pos >= totalSize()) throw ProtocolException("readpos less than 0 by rewind"); readPos_ = pos; }
void BufferedTransport::rewind(size_t size) { ScopedMutexLock(lock_); int temp = readPos_ - size; if(temp < 0) { throw ProtocolException("readpos less than 0 by rewind"); } readPos_ = temp; }
int BufferedTransport::peek(char *buffer, size_t size) { ScopedMutexLock(lock_); if(remainingSize() < size) { size = remainingSize(); } memcpy(buffer, currentPtr(), size); return size; }
Uint32 Connection::getTimeConnected()const{ ScopedMutexLock(pimpl_->dataAccess); if( pimpl_->connectTime == 0 ){ return 0; } else { return ( SDL_GetTicks() - pimpl_->connectTime ); } }
void cancel(ticket_t ticket) { ScopedMutexLock(lockRedis_); _LOG_DEBUG("redis request canceled : ticket = %d, count = %d/%d <START>" , ticket, mapCallback_.size(), reservedCommands_.size()); _removeContext(ticket); _LOG_DEBUG("redis request canceled : ticket = %d, count = %d/%d <END>" , ticket, mapCallback_.size(), reservedCommands_.size()); }
void ProgressDialog::processLogfileOutput(Logfile::OutputType outputType, const UnicodeString& line) { auto lock = ScopedMutexLock(detailedOutputMutex_); detailedOutput_ = line + "\13\10" + detailedOutput_; SetEvent(hUpdateOutputEvent_); }
void SimpleFileSystemVolume::clear() { auto lock = ScopedMutexLock(mutex_); for (auto entry : entries_) delete entry; entries_.clear(); }
Connection& Connection::operator=(Connection& rhs){ if( &rhs != this ){ ScopedMutexLock(rhs.pimpl_->dataAccess); clean(); pimpl_ = rhs.pimpl_; ++pimpl_->refCount; } return *this; }
int BufferedTransport::read(void *ptr, size_t size) { ScopedMutexLock(lock_); if(remainingSize() < size) { size = remainingSize(); } memcpy(ptr, currentPtr(), size); fastforward(size); return size; }
int BufferedTransport::read(std::string &data, size_t size) { ScopedMutexLock(lock_); if(remainingSize() < size) { size = remainingSize(); } data.assign((const char *)currentPtr(), size); fastforward(size); return size; }
void Connection::disconnect(){ ScopedMutexLock(pimpl_->dataAccess); pimpl_->peer = NULL; pimpl_->active = false; pimpl_->connectTime = 0; if( pimpl_->userSocket != NULL ){ SDLNet_TCP_Close( pimpl_->userSocket ); pimpl_->userSocket = NULL; } }
std::string BufferedTransport::readString(size_t size) { ScopedMutexLock(lock_); if(remainingSize() < size) { size = remainingSize(); } std::string str; str.assign((const char *)currentPtr(), size); fastforward(size); return str; }
// Flush the contents of the output sink call queue every frame in response to UpdateEvent. Logfile writes from other // threads cannot call the output sinks directly and so the required calls are queued and then flushed every frame here // on the main thread. static bool flushOutputSinkCallQueue(const UpdateEvent& e) { auto lock = ScopedMutexLock(queuedOutputSinkCallsMutex); for (auto& queuedCall : queuedOutputSinkCalls) queuedCall.sink->processLogfileOutput(queuedCall.outputType, queuedCall.line); queuedOutputSinkCalls.clear(); return true; }
void Connection::cleanSet(){ ScopedMutexLock(pimpl_->dataAccess); //remove from set if( pimpl_->group ){ SDLNet_TCP_DelSocket(pimpl_->group, pimpl_->userSocket); } // clean up if( pimpl_->createdSet == 1 ){ SDLNet_FreeSocketSet(pimpl_->group); pimpl_->createdSet = 0; } pimpl_->group = NULL; }
FileSystemError SimpleFileSystemVolume::deleteFile(const UnicodeString& filename) { auto lock = ScopedMutexLock(mutex_); auto entry = entries_.detect([&](const FileEntry* e) { return e->name == filename; }, nullptr); if (!entry) return ResourceMissingFileSystemError; delete entry; entries_.eraseValue(entry); return NoFileSystemError; }
int Connection::checkSet(int timeout){ ScopedMutexLock(pimpl_->dataAccess); if( pimpl_->group != NULL ){ int num = SDLNet_CheckSockets(pimpl_->group, timeout); if( num == -1 ){ pimpl_->active = false; pimpl_->connectTime = 0; } else { pimpl_->active = true; } return num; } return -2; // not in set }
std::auto_ptr<Connection> Connection::accept(){ ScopedMutexLock(pimpl_->dataAccess); if( pimpl_->userSocket == NULL){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "Connection::accept(): cant accept from a null socket" << std::endl; return std::auto_ptr<Connection> (NULL); } TCPsocket sock = SDLNet_TCP_Accept(pimpl_->userSocket); if( sock == NULL ){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "SDLNet_TCP_Accept: " << SDLNet_GetError() << std::endl; return std::auto_ptr<Connection> (NULL); } std::auto_ptr<Connection> newConnection( new Connection(sock) ); return newConnection; }
FileSystemError SimpleFileSystemVolume::enumerateFiles(const UnicodeString& directory, const UnicodeString& extension, bool recursive, Vector<UnicodeString>& files) const { auto lock = ScopedMutexLock(mutex_); for (auto entry : entries_) { auto& name = entry->name; if (name.startsWith(directory) && name.endsWith(extension) && (recursive || !name.has('/', directory.length()))) files.append(name); } return NoFileSystemError; }
bool SimpleFileSystemVolume::renameFile(const UnicodeString& currentName, const UnicodeString& newName) { auto lock = ScopedMutexLock(mutex_); if (doesFileExist(newName)) return false; for (auto entry : entries_) { if (entry->name == currentName) { entry->name = newName; return true; } } return false; }
int Connection::setSocketSet(const SDLNet_SocketSet& newGroup){ ScopedMutexLock(pimpl_->dataAccess); if( pimpl_->group == newGroup ){ return 1; } //remove from old group if( pimpl_->userSocket && pimpl_->group ){ SDLNet_TCP_DelSocket(pimpl_->group, pimpl_->userSocket); } else { return -1; } //if internal set was used, free it (its no longer needed) if( pimpl_->createdSet ){ SDLNet_FreeSocketSet(pimpl_->group); pimpl_->group = NULL; pimpl_->createdSet = false; } pimpl_->group = newGroup; SDLNet_TCP_AddSocket(pimpl_->group, pimpl_->userSocket); return 1; }