void ServerSocketNotify::bind(const char *path) { if (m_listener) getSocketFactory()->remove(m_listener); m_listener = getSocketFactory()->createServerSocket(); m_listener->setNotify(this); m_listener->bind(path); }
void ServerSocketNotify::bind(unsigned short minPort, unsigned short maxPort, TCPClient *client) { if (m_listener) getSocketFactory()->remove(m_listener); m_listener = getSocketFactory()->createServerSocket(); m_listener->setNotify(this); m_listener->bind(minPort, maxPort, client); }
ClientSocket::~ClientSocket() { if (m_sock) delete m_sock; for (list<ClientSocket*>::iterator it = getSocketFactory()->p->errSockets.begin(); it != getSocketFactory()->p->errSockets.end(); ++it){ if ((*it) == this){ getSocketFactory()->p->errSockets.erase(it); break; } } }
void ClientSocket::error_state(const char *err, unsigned code) { list<ClientSocket*>::iterator it; for (it = getSocketFactory()->p->errSockets.begin(); it != getSocketFactory()->p->errSockets.end(); ++it) if ((*it) == this) return; errString = ""; errCode = code; if (err) errString = err; getSocketFactory()->p->errSockets.push_back(this); QTimer::singleShot(0, getSocketFactory(), SLOT(idle())); }
void SIMClientSocket::slotConnected() { log(L_DEBUG, "Connected"); timerStop(); if (notify) notify->connect_ready(); getSocketFactory()->setActive(true); }
void SIMResolver::resolveTimeout() { bDone = true; bTimeout = true; getSocketFactory()->setActive(false); QTimer::singleShot(0, parent(), SLOT(resultsReady())); }
void ServerSocketNotify::setListener(ServerSocket *listener) { if (m_listener) getSocketFactory()->remove(m_listener); m_listener = listener; if (m_listener) m_listener->setNotify(this); }
void SIMServerSocket::error(const char *err) { close(); if (notify && notify->error(err)){ notify->m_listener = NULL; getSocketFactory()->remove(this); } }
void Proxy::proxy_connect_ready() { static_cast<ClientSocket*>(notify)->setSocket(m_sock); m_sock = NULL; if (notify) notify->connect_ready(); getSocketFactory()->remove(this); }
ClientSocket::ClientSocket(ClientSocketNotify *notify) { m_notify = notify; bRawMode = false; bClosed = false; m_sock = getSocketFactory()->createSocket(); m_sock->setNotify(this); }
void SIMClientSocket::slotLookupFinished(int state) { log(L_DEBUG, "Lookup finished %u", state); if (state == 0){ log(L_WARN, "Can't lookup"); notify->error_state(I18N_NOOP("Connect error")); getSocketFactory()->setActive(false); } }
Listener::Listener(ProxyPlugin *plugin, ProxyData *_data, ServerSocketNotify *notify, unsigned long ip) { m_ip = ip; m_plugin = plugin; m_sock = getSocketFactory()->createSocket(); m_sock->setNotify(this); data = *_data; notify->setListener(this); }
void ClientSocket::setSocket(Socket *s, bool bClearError) { if (m_sock){ if (m_sock->notify == this) m_sock->setNotify(NULL); if (bClearError){ list<ClientSocket*>::iterator it; for (it = getSocketFactory()->p->errSockets.begin(); it != getSocketFactory()->p->errSockets.end(); ++it){ if ((*it) == this){ getSocketFactory()->p->errSockets.erase(it); break; } } } } m_sock = s; if (s) s->setNotify(this); }
void Proxy::proxy_connect_ready() { if (notify){ SocketNotify *n = notify; static_cast<ClientSocket*>(n)->setSocket(m_sock); m_sock = NULL; n->connect_ready(); } getSocketFactory()->remove(this); }
void WeatherPlugin::timeout() { if (!getSocketFactory()->isActive() || m_fetch_id || (*getURL() == 0)) return; time_t now; time(&now); if ((unsigned)now < getTime() + CHECK_INTERVAL) return; m_fetch_id = fetch(NULL, getURL()); }
void SIMClientSocket::slotConnectionClosed() { log(L_WARN, "Connection closed"); timerStop(); if (notify) notify->error_state(I18N_NOOP("Connection closed")); #ifdef WIN32 bool bState; if (get_connection_state(bState) && !bState) getSocketFactory()->setActive(false); #endif }
void SIMClientSocket::connect(const char *_host, int _port) { port = _port; host = _host; log(L_DEBUG, "Connect to %s:%u", host.c_str(), port); if (inet_addr(host.c_str()) == INADDR_NONE){ log(L_DEBUG, "Start resolve %s", host.c_str()); SIMSockets *s = static_cast<SIMSockets*>(getSocketFactory()); QObject::connect(s, SIGNAL(resolveReady(unsigned long, const char*)), this, SLOT(resolveReady(unsigned long, const char*))); s->resolve(host.c_str()); return; }
unsigned SIM::fetch(const char *url, Buffer *postData, const char *headers, bool bRedirect) { #ifdef WIN32 bool bState; if (get_connection_state(bState)) getSocketFactory()->setActive(bState); #endif FetchClient *client = new FetchClient(url, postData, headers, bRedirect); if (client->id()) return client->id(); delete client; return 0; }
void WeatherPlugin::timeout() { if (!getSocketFactory()->isActive() || m_fetch_id || (*getID() == 0)) return; time_t now; time(&now); if ((unsigned)now < getTime() + CHECK_INTERVAL) return; string url = "http://xoap.weather.com/weather/local/"; url += getID(); url += "?cc=*&prod=xoap&par=1004517364&key=a29796f587f206b2&unit="; url += getUnits() ? "s" : "m"; m_fetch_id = fetch(url.c_str()); }
void UpdatePlugin::timeout() { if (!getSocketFactory()->isActive() || m_fetch_id) return; time_t now; time(&now); if ((unsigned)now >= getTime() + CHECK_INTERVAL){ string url = "http://sim.shutoff.ru/cgi-bin/update1.pl?v=" VERSION; #ifdef WIN32 url += "&os=1"; #else #ifdef QT_MACOSX_VERSION url += "&os=2"; #endif #endif #ifdef CVS_BUILD url += "&cvs="; for (const char *p = __DATE__; *p; p++){ if (*p == ' '){ url += "%20"; continue; } url += *p; } #else url += "&release"; #endif url += "&l="; QString s = i18n("Message", "%n messages", 1); s = s.replace(QRegExp("1 "), ""); for (int i = 0; i < (int)(s.length()); i++){ unsigned short c = s[i].unicode(); if ((c == ' ') || (c == '%') || (c == '=') || (c == '&')){ char b[5]; sprintf(b, "%02X", c); url += b; }else if (c > 0x77){ char b[10]; sprintf(b, "#%04X", c); url += b; }else{ url += (char)c; } } TCPClient *client = NULL; if (getContacts()->nClients()) client = static_cast<TCPClient*>(getContacts()->getClient(0)); m_fetch_id = fetch(NULL, url.c_str()); } }
int HTTP_Proxy::read(char *buf, unsigned int size) { if (!m_bHTTP) return HTTPS_Proxy::read(buf, size); if (m_head.empty()) return 0; if (size > m_head.length()) size = m_head.length(); memcpy(buf, m_head.c_str(), size); m_head = m_head.substr(size); if (m_head.empty()){ static_cast<ClientSocket*>(notify)->setSocket(m_sock); m_sock = NULL; getSocketFactory()->remove(this); } return size; }
void WeatherPlugin::timeout() { if (!getSocketFactory()->isActive() || !isDone() || (*getID() == 0)) return; time_t now = time(NULL); if ((unsigned)now < getTime() + CHECK1_INTERVAL) return; m_bForecast = false; if ((unsigned)now >= getForecastTime() + CHECK2_INTERVAL) m_bForecast = true; string url = "http://xoap.weather.com/weather/local/"; url += getID(); url += "?cc=*&prod=xoap&par=1004517364&key=a29796f587f206b2&unit="; url += getUnits() ? "s" : "m"; if (m_bForecast && getForecast()){ url += "&dayf="; url += number(getForecast()); } fetch(url.c_str()); }
bool TCPClient::error_state(const char *err, unsigned code) { log(L_DEBUG, "Socket error %s (%u)", err, code); if (m_reconnect == NO_RECONNECT){ m_timer->stop(); setStatus(STATUS_OFFLINE, getCommonStatus()); setState(Error, err, code); return false; } if (!m_timer->isActive()){ unsigned reconnectTime = m_reconnect; if (!getSocketFactory()->isActive()){ if (reconnectTime < RECONNECT_IFINACTIVE) reconnectTime = RECONNECT_IFINACTIVE; } setClientStatus(STATUS_OFFLINE); setState((m_reconnect == NO_RECONNECT) ? Error : Connecting, err, code); m_bWaitReconnect = true; log(L_DEBUG, "Wait reconnect %u sec", reconnectTime); m_timer->start(reconnectTime * 1000); } return false; }
void SIMClientSocket::connect(const char *_host, unsigned short _port) { port = _port; host = _host; #ifdef WIN32 bool bState; if (get_connection_state(bState) && !bState){ QTimer::singleShot(0, this, SLOT(slotConnectionClosed())); return; } #endif log(L_DEBUG, "Connect to %s:%u", host.c_str(), port); if (inet_addr(host.c_str()) == INADDR_NONE){ if (!host.empty() && (host[host.length() - 1] != '.')) host += "."; log(L_DEBUG, "Start resolve %s", host.c_str()); SIMSockets *s = static_cast<SIMSockets*>(getSocketFactory()); QObject::connect(s, SIGNAL(resolveReady(unsigned long, const char*)), this, SLOT(resolveReady(unsigned long, const char*))); s->resolve(host.c_str()); return; } resolveReady(inet_addr(host.c_str()), host.c_str()); }
void SIMServerSocket::activated(int) { if (sock == NULL) return; int fd = sock->accept(); if (fd >= 0){ log(L_DEBUG, "accept ready"); if (notify){ QSocket *s = new QSocket; s->setSocket(fd); if (notify->accept(new SIMClientSocket(s), htonl(s->address().ip4Addr()))){ if (notify) notify->m_listener = NULL; getSocketFactory()->remove(this); } }else{ #ifdef WIN32 ::closesocket(fd); #else ::close(fd); #endif } } }
void UpdatePlugin::timeout() { if (!getSocketFactory()->isActive() || m_fetch_id) return; time_t now; time(&now); if ((unsigned)now >= getTime() + CHECK_INTERVAL){ string url = "http://sim.shutoff.ru/cgi-bin/update.pl?v=" VERSION; #ifdef WIN32 url += "&os=1"; #endif #ifdef CVS_BUILD url += "&cvs="; for (const char *p = __DATE__; *p; p++){ if (p == ' '){ url += "%20"; continue; } url += *p; } #endif m_fetch_id = fetch(NULL, url.c_str()); } }
ClientSocket::~ClientSocket() { if (m_sock) delete m_sock; getSocketFactory()->p->errSockets.remove(this); }
void FetchClient::fetch(const char *url, const char *headers, Buffer *postData, bool bRedirect) { getSocketFactory()->checkState(); p->fetch(url, headers, postData, bRedirect); }
ServerSocketNotify::~ServerSocketNotify() { if (m_listener) getSocketFactory()->remove(m_listener); }
void StatusLabel::setPict() { string icon; const char *text; if (m_client->getState() == Client::Connecting){ if (getSocketFactory()->isActive()){ if (m_timer == NULL){ m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(timeout())); m_timer->start(1000); m_bBlink = false; } Protocol *protocol = m_client->protocol(); text = I18N_NOOP("Connecting"); unsigned status; if (m_bBlink){ icon = "online"; status = m_client->getManualStatus(); }else{ icon = "offline"; status = STATUS_OFFLINE; } if (protocol){ for (const CommandDef *cmd = protocol->statusList(); cmd->text; cmd++){ if (cmd->id == status){ icon = cmd->icon; break; } } } }else{ if (m_timer){ delete m_timer; m_timer = NULL; } Protocol *protocol = m_client->protocol(); const CommandDef *cmd = protocol->description(); icon = cmd->icon; int n = icon.find('_'); if (n > 0) icon = icon.substr(0, n); icon += "_inactive"; text = I18N_NOOP("Inactive"); } }else{ if (m_timer){ delete m_timer; m_timer = NULL; } if (m_client->getState() == Client::Error){ icon = "error"; text = I18N_NOOP("Error"); }else{ Protocol *protocol = m_client->protocol(); const CommandDef *cmd = protocol->description(); icon = cmd->icon; text = cmd->text; for (cmd = protocol->statusList(); cmd->text; cmd++){ if (cmd->id == m_client->getStatus()){ icon = cmd->icon; text = cmd->text; break; } } } } QPixmap pict = Pict(icon.c_str()); QString tip = CorePlugin::m_plugin->clientName(m_client); tip += "\n"; tip += i18n(text); setPixmap(pict); QToolTip::add(this, tip); resize(pict.width(), pict.height()); setFixedSize(pict.width(), pict.height()); };