void HttpClient::on_connected(gpointer data, bool succeeded) { HttpClient *oHttpClient = (HttpClient *)data; if (!succeeded) { gchar *mes = g_strdup_printf("Can not connect to %s: %s\n", oHttpClient->host_.c_str(), Socket::get_error_msg().c_str()); on_error_.emit(oHttpClient, mes); g_free(mes); return; } #ifdef _WIN32 oHttpClient->channel_ = g_io_channel_win32_new_socket(oHttpClient->sd_); #else oHttpClient->channel_ = g_io_channel_unix_new(oHttpClient->sd_); #endif g_io_channel_set_encoding(oHttpClient->channel_, NULL, NULL); /* make sure that the channel is non-blocking */ int flags = g_io_channel_get_flags(oHttpClient->channel_); flags |= G_IO_FLAG_NONBLOCK; GError *err = NULL; g_io_channel_set_flags(oHttpClient->channel_, GIOFlags(flags), &err); if (err) { gchar *str = g_strdup_printf("Unable to set the channel as non-blocking: %s", err->message); on_error_.emit(oHttpClient, str); g_free(str); g_error_free(err); return; } if (oHttpClient->SendGetRequest()) return; oHttpClient->out_source_id_ = g_io_add_watch(oHttpClient->channel_, GIOCondition(G_IO_OUT), on_io_out_event, oHttpClient); oHttpClient->in_source_id_ = g_io_add_watch(oHttpClient->channel_, GIOCondition(G_IO_IN | G_IO_ERR), on_io_in_event, oHttpClient); }
GamepadController::GamepadController() : m_gamepadDevices(Nix::Gamepads::itemsLengthCap) { m_udev = udev_new(); m_gamepadsMonitor = udev_monitor_new_from_netlink(m_udev, "udev"); udev_monitor_enable_receiving(m_gamepadsMonitor); udev_monitor_filter_add_match_subsystem_devtype(m_gamepadsMonitor, "input", 0); GIOChannel *channel = g_io_channel_unix_new(udev_monitor_get_fd(m_gamepadsMonitor)); g_io_add_watch(channel, GIOCondition(G_IO_IN), static_cast<GIOFunc>(&GamepadController::onGamepadChange), this); g_io_channel_unref(channel); struct udev_enumerate* enumerate = udev_enumerate_new(m_udev); udev_enumerate_add_match_subsystem(enumerate, "input"); udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1"); udev_enumerate_scan_devices(enumerate); struct udev_list_entry* cur; struct udev_list_entry* devs = udev_enumerate_get_list_entry(enumerate); udev_list_entry_foreach(cur, devs) { const char* devname = udev_list_entry_get_name(cur); struct udev_device* device = udev_device_new_from_syspath(m_udev, devname); if (isGamepadDevice(device)) registerDevice(udev_device_get_devnode(device)); udev_device_unref(device); } udev_enumerate_unref(enumerate); }
int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd) { GIOChannel* channel = g_io_channel_unix_new(fd); GIOCondition cond = GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR); unsigned id = g_io_add_watch(channel, cond, EndProcessDetector, proc_data); g_io_channel_unref(channel); return int(id); }
void WebSocketSinkManager::addPoll(int fd) { GIOChannel *chan = g_io_channel_unix_new(fd); guint sourceid = g_io_add_watch(chan, GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR),(GIOFunc)gioPollingFunc,chan); g_io_channel_set_close_on_unref(chan,true); g_io_channel_unref(chan); //Pass ownership of the GIOChannel to the watch. m_ioChannelMap[fd] = chan; m_ioSourceMap[fd] = sourceid; }
void DictClient::on_resolved(gpointer data, struct hostent *ret) { DictClient *oDictClient = (DictClient *)data; oDictClient->sd_ = Socket::socket(); if (oDictClient->sd_ == -1) { on_error_.emit("Can not create socket: " + Socket::get_error_msg()); return; } #ifdef _WIN32 oDictClient->channel_ = g_io_channel_win32_new_socket(oDictClient->sd_); #else oDictClient->channel_ = g_io_channel_unix_new(oDictClient->sd_); #endif /* RFC2229 mandates the usage of UTF-8, so we force this encoding */ g_io_channel_set_encoding(oDictClient->channel_, "UTF-8", NULL); g_io_channel_set_line_term(oDictClient->channel_, "\r\n", 2); /* make sure that the channel is non-blocking */ int flags = g_io_channel_get_flags(oDictClient->channel_); flags |= G_IO_FLAG_NONBLOCK; GError *err = NULL; g_io_channel_set_flags(oDictClient->channel_, GIOFlags(flags), &err); if (err) { g_io_channel_unref(oDictClient->channel_); oDictClient->channel_ = NULL; on_error_.emit("Unable to set the channel as non-blocking: " + std::string(err->message)); g_error_free(err); return; } if (!Socket::connect(oDictClient->sd_, ret, oDictClient->port_)) { gchar *mes = g_strdup_printf("Can not connect to %s: %s\n", oDictClient->host_.c_str(), Socket::get_error_msg().c_str()); on_error_.emit(mes); g_free(mes); return; } oDictClient->source_id_ = g_io_add_watch(oDictClient->channel_, GIOCondition(G_IO_IN | G_IO_ERR), on_io_event, oDictClient); }
void StarDictClient::on_connected(gpointer data, bool succeeded) { StarDictClient *oStarDictClient = (StarDictClient *)data; if (!succeeded) { static bool showed_once = false; if (!showed_once) { showed_once = true; gchar *mes = g_strdup_printf(_("Can not connect to %s: %s\n"), oStarDictClient->host_.c_str(), Socket::get_error_msg().c_str()); on_error_.emit(mes); g_free(mes); } return; } #ifdef _WIN32 oStarDictClient->channel_ = g_io_channel_win32_new_socket(oStarDictClient->sd_); #else oStarDictClient->channel_ = g_io_channel_unix_new(oStarDictClient->sd_); #endif g_io_channel_set_encoding(oStarDictClient->channel_, NULL, NULL); /* make sure that the channel is non-blocking */ int flags = g_io_channel_get_flags(oStarDictClient->channel_); flags |= G_IO_FLAG_NONBLOCK; GError *err = NULL; g_io_channel_set_flags(oStarDictClient->channel_, GIOFlags(flags), &err); if (err) { g_io_channel_unref(oStarDictClient->channel_); oStarDictClient->channel_ = NULL; gchar *str = g_strdup_printf(_("Unable to set the channel as non-blocking: %s"), err->message); on_error_.emit(str); g_free(str); g_error_free(err); return; } oStarDictClient->is_connected_ = true; oStarDictClient->waiting_banner_ = true; oStarDictClient->reading_type_ = READ_LINE; oStarDictClient->in_source_id_ = g_io_add_watch(oStarDictClient->channel_, GIOCondition(G_IO_IN | G_IO_ERR), on_io_in_event, oStarDictClient); }
void StarDictClient::write_str(const char *str, GError **err) { int len = strlen(str); int left_byte = len; GIOStatus res; gsize bytes_written; while (left_byte) { res = g_io_channel_write_chars(channel_, str+(len - left_byte), left_byte, &bytes_written, err); if (res == G_IO_STATUS_ERROR) { disconnect(); return; } left_byte -= bytes_written; } res = g_io_channel_flush(channel_, err); if (res == G_IO_STATUS_ERROR) { disconnect(); } if (out_source_id_ == 0) out_source_id_ = g_io_add_watch(channel_, GIOCondition(G_IO_OUT), on_io_out_event, this); }
void query() { //////////////////////////////////////////////// extern int curSpeed; extern int curRPM; extern int initFuel, prevFuel, curFuel; extern int initDist, curDist; extern int curTemp; extern int leftDist, rightDist; //Time Globals extern time_t rawTime; extern struct tm *prevTime, *curTime; extern int timeOffset; extern bool forwardMode; //////////////////////////////////////////////// int temp, count=0; UART uart; myI2C *sensorPtr0 = new myI2C(); myI2C *sensorPtr1 = new myI2C(); std::string RPM="010C\r"; std::string Speed="010D\r"; std::string Temperature="0105\r"; std::string Fuel="012F\r"; std::string Dist="0131\r"; // sensorPtr0->i2cSetAddress(DEVICE_ADDR0); // sensorPtr1->i2cSetAddress(DEVICE_ADDR1); uart.initUart(); uart.sendLine("ATZ\r"); int fd = open( "/sys/class/gpio/gpio67/value", O_RDONLY | O_NONBLOCK ); GIOChannel* channel = g_io_channel_unix_new( fd ); GIOCondition cond = GIOCondition( G_IO_PRI ); guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 ); usleep(5000000); //Get initial Distance //====================================== uart.sendLine(Dist); temp=-1; while(temp==-1) { if ((temp=uart.receiveLineData("013141 31 ",5))!=-1) { initDist=temp; curDist=initDist; } usleep(500000); } //====================================== //Get initial time //====================================== time(&rawTime); prevTime=localtime(&rawTime); prevTime->tm_hour+=timeOffset; curTime=localtime(&rawTime); curTime->tm_hour+=timeOffset; if(prevTime->tm_hour<0) { prevTime->tm_hour+=24; curTime->tm_hour+=24; } //====================================== //Get initial fuel level //====================================== uart.sendLine(Fuel); temp=-1; while(temp==-1) { if ((temp=uart.receiveLineData("012F41 2F ",2))!=-1) { initFuel=temp; prevFuel=initFuel; curFuel=initFuel; } usleep(500000); } //====================================== //Query for Engine Temp //====================================== uart.sendLine(Temperature); temp=-1; while(temp==-1) { if ((temp=uart.receiveLineData("010541 05 ",2))!=-1) { curTemp=temp; } usleep(500000); } //====================================== while(1) { //Forward Mode //====================================== if(forwardMode) { //Query for Speed //====================================== uart.sendLine(Speed); if ((temp=uart.receiveLineData("010D41 0D ",2))!=-1) { curSpeed=temp; } //====================================== usleep(500000); //Query for RPM //====================================== uart.sendLine(RPM); if ((temp=uart.receiveLineData("010C41 0C ",5))!=-1) { curRPM=temp; } //====================================== //Get the time //====================================== time(&rawTime); curTime=localtime(&rawTime); curTime->tm_hour+=timeOffset; if(curTime->tm_hour < 0) { curTime->tm_hour+=24; } //====================================== usleep(500000); //if it's been a minute, check fuel level and engine Temp //====================================== if(count>99) { count=0; prevTime=localtime(&rawTime); prevTime->tm_hour+=timeOffset; if(prevTime->tm_hour < 0) { prevTime->tm_hour+=24; } uart.sendLine(Fuel); if ((temp=uart.receiveLineData("012F41 2F ",2))!=-1) { curFuel=temp; } usleep(500000); //Query for Engine Temp //====================================== if ((temp=uart.receiveLineData("010541 05 ",2))!=-1) { curTemp=temp; } //====================================== usleep(500000); } //====================================== //if the fuel level has changed, //check distance //====================================== if(prevFuel!=curFuel) { prevFuel=curFuel; uart.sendLine(Dist); if ((temp=uart.receiveLineData("013141 31 ",4))!=-1) { curDist=temp; } usleep(500000); } //if the speed is greater than 30MPH or 49 KPH //check rear distance //====================================== if(curSpeed > 49) { /*sensorPtr0->Send_I2C_Byte(0x00, 0x51); usleep(68E3); leftDist=sensorPtr0->Read_2I2C_Bytes(0x02); sensorPtr1->Send_I2C_Byte(0x00, 0x51); usleep(68E3); rightDist=sensorPtr1->Read_2I2C_Bytes(0x02);*/ } //====================================== count++; } //====================================== //Reverse Mode //====================================== else { usleep(1000000); //Query reverse distance as much as possible //====================================== /*sensorPtr0->Send_I2C_Byte(0x00, 0x51); usleep(68E3); leftDist=sensorPtr0->Read_2I2C_Bytes(0x02); sensorPtr1->Send_I2C_Byte(0x00, 0x51); usleep(68E3); rightDist=sensorPtr1->Read_2I2C_Bytes(0x02);*/ //====================================== } } }
void GtkHttpDBufRequester::request(const MC2SimpleString& paramString, DBufRequestListener* caller, request_t whereFrom ) { // FIXME: Remove this. This is a memory cache!! map<MC2SimpleString, BitBuffer*>::iterator it = m_buffers.find( paramString ); if ( it != m_buffers.end() ) { caller->requestReceived(it->first, it->second, *this); return; } if ( m_files.size() >= MAX_NBR_REQUESTS ) { m_pending.push_back(make_pair(paramString, caller) ); return; } if ( whereFrom == onlyCache ) { caller->requestReceived( paramString, NULL, *this ); return; } FILE* curFile = openFile( paramString.c_str() ); if ( curFile == NULL ) { caller->requestReceived(it->first, NULL, *this); return; } //!! TODO !! following code ...check with old return; int fd = fileno(curFile); // Set nonblocking non portable etiher fcntl(fd, F_SETFL, O_NONBLOCK); // Signal when there is something on the fd // attach a watch on IO channel GIOChannel *channel = g_io_channel_unix_new(fd); mc2dbg << "channel fd = "<<g_io_channel_unix_get_fd(channel) <<endl; mc2dbg << "real fd = "<< fd <<endl; guint event_id = g_io_add_watch( channel, GIOCondition(G_IO_IN | G_IO_ERR), GtkHttpDBufRequester::gdkReadCallback, static_cast<gpointer>(this)); /* SigC::Slot3<void,gint,GdkInputCondition,int> tmp_slot = SigC::slot(*this, &GtkHttpDBufRequester::readCallBack); // Pass the counter in the last parameter of the callback function. Gtk::InputSig::SlotType my_slot = SigC::bind( tmp_slot, counter); SigC::Connection conn = Gtk::Main::input.connect(my_slot, fd, GdkInputCondition(GDK_INPUT_READ| GDK_INPUT_EXCEPTION)); */ m_files.insert(make_pair(fd, GtkHttpConnData(caller, curFile, paramString, event_id) ) ); }