bool SINQHMListener::connect(const Poco::Net::SocketAddress &address) { std::string host = address.toString(); std::string::size_type i = host.find(':'); if (i != std::string::npos) { host.erase(i); } httpcon.setHost(host); httpcon.setPort(address.port()); httpcon.setKeepAlive(true); connected = true; return true; }
PothosPacketSocketEndpointInterfaceUdt(const Poco::Net::SocketAddress &addr, const bool server): server(server), connected(false), sess(getUDTSession()) { if (server) { this->serverSock = makeSocket(); if (UDT::ERROR == UDT::bind(this->serverSock, addr.addr(), addr.length())) { throw Pothos::RuntimeException("UDT::bind("+addr.toString()+")", UDT::getlasterror().getErrorMessage()); } UDT::listen(this->serverSock, 1/*only one client expected*/); } else { this->clientSock = makeSocket(); if (UDT::ERROR == UDT::connect(this->clientSock, addr.addr(), addr.length())) { throw Pothos::RuntimeException("UDT::connect("+addr.toString()+")", UDT::getlasterror().getErrorMessage()); } this->connected = true; } }
bool UDPSocket::bind(const Poco::Net::SocketAddress & address) { _bound = false; _error.clear(); if(_connected) { _error = "Impossible to bind a connected UDPSocket, close the socket before"; return false; } try { _socket.bind(address); _manager.add(_socket,*this); _bound = true; } catch(Exception& ex) { _error = format("Impossible to bind to %s, %s",address.toString(),ex.displayText()); } return _bound; }
/** Connect to the specified address and checks that is valid * @param address The IP address and port to contact (port is ignored). * @return True if the connection was successfully established */ bool ISISHistoDataListener::connect(const Poco::Net::SocketAddress &address) { m_daeName = address.toString(); // remove the port part auto i = m_daeName.find(':'); if (i != std::string::npos) { m_daeName.erase(i); } // set IDC reporter function for errors IDCsetreportfunc(&ISISHistoDataListener::IDCReporter); if (IDCopen(m_daeName.c_str(), 0, 0, &m_daeHandle, address.port()) != 0) { m_daeHandle = NULL; return false; } m_numberOfPeriods = getInt("NPER"); g_log.information() << "Number of periods " << m_numberOfPeriods << std::endl; // Set the spectra list to load std::vector<specid_t> spectra = getProperty("SpectraList"); if (!spectra.empty()) { setSpectra(spectra); } // Set the period list to load std::vector<int> periodList = getProperty("PeriodList"); if (!periodList.empty()) { setPeriods(periodList); } loadSpectraMap(); loadTimeRegimes(); return true; }
void Broadcaster::OnReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification>&) { char *buffer = new char[512]; Poco::Net::SocketAddress socketAddress; //Put the packet into the buffer int length = socket.receiveFrom(buffer, 512, socketAddress); printf("Packet from %s\n", socketAddress.toString().c_str()); if (strcmp(buffer, "\xff\xff\xff\xffTSource Engine Query\0") == 0){ char *buffer = "\xff\xff\xff\xffI\aThis is fake\0maps/ns2_dm.level\0ns2\0Practice\0\x38\x13\0\x10\0lw\0\0\x31.0.0.0\0\x91\xa0\x69\5\xec\xdb\x09\x4c\6\x40\1\x38\x13\0\0\0\0\0\0"; socket.sendTo(buffer, 111, socketAddress); }else{ int pos = 6; ServerInfo info; info.ServerName = std::string(buffer+pos); pos += info.ServerName.length()+1; info.CurrentLevel = std::string(buffer+pos); pos += info.CurrentLevel.length()+1; info.Game = std::string(buffer+pos); pos += info.Game.length()+1; info.Mod = std::string(buffer+pos); printf("Server %s on map %s from %s\n", info.ServerName.c_str(), info.CurrentLevel.c_str(), socketAddress.toString().c_str()); } }
int main(int argc, char **argv) { if (argc != 2) { printf("usage: hsWorkServ <Port>\n"); exit(1); } int port = atoi(argv[1]); Poco::Net::SocketAddress socketAddress(Poco::Net::IPAddress(), port); Poco::Net::DatagramSocket datagramSocket(socketAddress); MySQL::Connector::registerConnector(); try { _pSession = new Session(SessionFactory::instance().create(MySQL::Connector::KEY, _dbConnString)); }catch (ConnectionException& ex) { std::cout << "!!! WARNING: Connection failed. MySQL tests will fail !!!" << std::endl; std::cout << ex.displayText() << std::endl; exit(1); } if (_pSession && _pSession->isConnected()) std::cout << "*** Connected to " << '(' << _dbConnString << ')' << std::endl; else { MySQL::Connector::unregisterConnector(); exit(1); } char buffer[1024]; while (true) { memset(buffer,0x00,sizeof(buffer)); Poco::Net::SocketAddress sender; int n = datagramSocket.receiveFrom(buffer, sizeof(buffer) - 1, sender); buffer[n] = '\0'; std::cout << sender.toString() << ": " << buffer << std::endl; try { string sql=""; if(8888 == port) writeToDB(buffer,sql); else if(8889 == port) writeToDB2(buffer,sql); else { std::cout << "Error param" << std::endl; break; } Statement stmt(*_pSession); stmt << sql; stmt.execute(); } catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; break; } catch(StatementException& se){ std::cout << se.displayText() << std::endl; break; } catch(Exception& e){ std::cout << e.displayText() << std::endl; break; } } MySQL::Connector::unregisterConnector(); return 0; }
void NetworkListenTask::run() { Poco::Net::SocketAddress incomingAddress; TaskState lState; char* buffer; int receivedSize; // Create the receive buffer buffer = new char[Constants::NETWORK_RECEIVE_BUFFER_SIZE]; // A loop that receives network messages. The loop checks the state of this task in // a thread safe manner, and terminates if the the state is TS_KILLED. If the state is TS_SUSPEND // the loop does not receive any messages. for (lState = getState(); lState != TS_KILLED; lState = getState()) { if (lState == TS_SUSPENDED) { this->sleep(1); // Immediately continue to the next iteration if the the task is suspended continue; } try { // Listen for incoming messages receivedSize = mListenSocket.receiveFrom(buffer, Constants::NETWORK_RECEIVE_BUFFER_SIZE, incomingAddress); // If we received a message, send it to our NetworkListenBehaviour instance for processing if (receivedSize > 0) { LOG_DEBUG(EngineLog::LM_INFO_ENGINE, "NetworkListenTask::run(): Received message from: " << incomingAddress.toString()); // Lock for reading mListenBehaviour Poco::ScopedRWLock lock(rwLock, false); if (mListenBehaviour != NULL) { mListenBehaviour->processReceivedMessage(incomingAddress.host(), buffer, receivedSize); } else { LOG(EngineLog::LM_WARNING, "NetworkListenTask::run(): Received message dropped: there is no NetworkListenBehaviour instance available to process the message."); } } } catch (Exception& e) { LOG(EngineLog::LM_ERROR, "NetworkListenTask::run(): caught Exception: " << e.getMsg()); } catch (Poco::TimeoutException& e) { // ignore the TimeoutException, it is thrown often in non-blocking mode } catch (Poco::Exception& e) { LOG(EngineLog::LM_ERROR, "NetworkListenTask::run(): caught Poco::Exception: " << e.displayText()); } this->sleep(1); } // Free the receive buffer delete[] buffer; }