// -1 if unsuccessful, else number of bytes written int TCPSocketConnection::send_all(char* data, int length) { Timer tmr; int idx = 0; if (_cid < 0 || !is_connected()) return -1; tmr.start(); // TCP Client/Server while ((tmr.read_ms() < _timeout) || _blocking) { idx += _wifi->send(_cid, &data[idx], length - idx); if (idx < 0) return -1; if (idx == length) return idx; } return (idx == 0) ? -1 : idx; }
bool GPRS::close(int socket) { char cmd[16]; char resp[16]; if (socket < 0 || socket > MAX_SOCK_NUM-1) { return false; } // if not connected, return if (is_connected(socket) == false) { return true; } snprintf(cmd, sizeof(cmd),"AT+CIPCLOSE=%d\r\n",socket); snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket); if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) { return false; } return true; }
void dns_handle_new_query(dns_request_t *m) { struct in_addr in; int retval = -1; if( m->message.question[0].type == A || m->message.question[0].type == AAA){ /* standard query */ retval = cache_lookup_name( m->cname, m->ip ); }else if( m->message.question[0].type == PTR ){ /* reverse lookup */ retval = cache_lookup_ip( m->ip, m->cname ); } debug(".......... %s ---- %s\n", m->cname, m->ip ); switch( retval ) { case 0: if( is_connected() ){ debug("Adding to list-> id: %d\n", m->message.header.id); dns_request_list = dns_list_add( dns_request_list, m ); /*!!! relay the query untouched */ inet_aton( config.name_server[0], &in ); debug("Sent Request To %s\n",config.name_server[0]); dns_write_packet( dns_sock, in, PORT, m ); }else{ debug("Not connected **\n"); dns_construct_error_reply(m); dns_write_packet( dns_sock, m->src_addr, m->src_port, m ); } break; case 1: dns_construct_reply( m ); dns_write_packet( dns_sock, m->src_addr, m->src_port, m ); debug("Cache hit\n"); break; default: debug("Unknown query type: %d\n", m->message.question[0].type ); } }
int as_node_get_connection(as_node* node, int* fd) { //cf_queue* q = asyncfd ? node->conn_q_asyncfd : node->conn_q; cf_queue* q = node->conn_q; while (1) { int rv = cf_queue_pop(q, fd, CF_QUEUE_NOWAIT); if (rv == CF_QUEUE_OK) { int rv2 = is_connected(*fd); switch (rv2) { case CONNECTED: // It's still good. return 0; case CONNECTED_BADFD: // Local problem, don't try closing. cf_warn("Found bad file descriptor in queue: fd %d", *fd); break; case CONNECTED_NOT: // Can't use it - the remote end closed it. case CONNECTED_ERROR: // Some other problem, could have to do with remote end. default: cf_close(*fd); break; } } else if (rv == CF_QUEUE_EMPTY) { // We exhausted the queue. Try creating a fresh socket. return as_node_create_connection(node, fd); } else { cf_error("Bad return value from cf_queue_pop"); *fd = -1; return CITRUSLEAF_FAIL_CLIENT; } } }
cmd_status pipesvr_win32::receive(uint8_t *command, uint8_t *buf, uint32_t *len) { assert(command); assert(buf); assert(len); assert(sizeof(unsigned long) == sizeof(uint32_t)); if (!is_connected() && !connect()) { return conn_dead; } //Read from client DWORD rlen = 0; if (!ReadFile(pipe_, buf_, LEN_NETBUF, &rlen, NULL)) { DWORD error = GetLastError(); if (error == ERROR_NO_DATA) { rlen = 0; } else { rlen = 0; log_error("Failed to read from pipe with error 0x%x.", error); svr_->terminate("Failed to read from pipe."); } } if (rlen > 0) { queue_messages_.add(buf_, rlen); } if (!queue_messages_.can_get()) { return no_cmd; } *len = queue_messages_.get(command, buf, *len); if (*len == MESSAGE_QUEUE_BUFFER_TOO_SMALL) { log_error("Message buffer too small."); len = 0; } return unhandled; }
int main() { int id[50]; int data[10][2]; int a,b; int i; printf("Enter the no of objects:\n"); scanf("%d",&objects); for(i=0;i<objects;i++) id[i]=i; for(i=0;i<objects;i++){ printf("Enter two elements\n"); scanf("%d%d",&a,&b); if(is_connected(id,a,b)) printf("Already Connected\n"); else union_cmd(id,a,b); } }
device_status device::disconnect() { if (state() != device_state_value::not_ready) return device_status_value::not_ready; if (state() != device_state_value::faulted) return device_status_value::device_error; if (!is_connected()) return device_status_value::ok; auto rc = on_disconnect(); if (rc) { set_isConnected(false); } return rc; }
void tcp_client::disconnect(bool wait_for_removal) { if (!is_connected()) { return; } //! update state m_is_connected = false; //! clear all pending requests clear_read_requests(); clear_write_requests(); //! remove socket from io service and wait for removal if necessary m_io_service->untrack(m_socket); if (wait_for_removal) { m_io_service->wait_for_removal(m_socket); } //! close the socket m_socket.close(); __TACOPIE_LOG(info, "tcp_client disconnected"); }
/** * Pass a command to usenet. * * @private * * @param command = The command to pass to usenet. * @return bool = Did we succeed? */ bool socket::send_command(const std::string command) { if (!is_connected()) { return false; } try { // Add return + newline to the command. std::string cmd = command + "\r\n"; // Send the command to usenet. if (tcp_sock != NULL) tcp_sock->write_some(boost::asio::buffer(cmd.c_str(), strlen(cmd.c_str()))); else ssl_sock->write_some(boost::asio::buffer(cmd.c_str(), strlen(cmd.c_str()))); } catch (boost::system::system_error& error) { throw NNTPSockException(error.what()); return false; } return true; }
bool send(const List& messages, const std::string& topic, const uint32_t partition = kafkaconnect::use_random_partition) { if (!is_connected()) { return false; } // TODO: make this more efficient with memory allocations. boost::asio::streambuf* buffer = new boost::asio::streambuf(); std::ostream stream(buffer); kafkaconnect::encode(stream, topic, partition, messages); boost::asio::async_write( _socket, *buffer, boost::bind(&producer::handle_write_request, this, boost::asio::placeholders::error, buffer) ); return true; }
static gboolean input_device_auto_reconnect(gpointer user_data) { struct input_device *idev = user_data; struct input_conn *iconn; GError *err = NULL; DBG("idev %p", idev); DBG("path=%s, attempt=%d", idev->path, idev->reconnect_attempt); /* Stop the recurrent reconnection attempts if the device is reconnected * or is marked for removal. */ if (device_is_temporary(idev->device) || device_is_connected(idev->device)) return FALSE; /* Only attempt an auto-reconnect for at most 3 minutes (6 * 30s). */ if (idev->reconnect_attempt >= 6) return FALSE; iconn = find_connection(idev->connections, HID_UUID); if (iconn == NULL) return FALSE; if (iconn->ctrl_io) return FALSE; if (is_connected(iconn)) return FALSE; idev->reconnect_attempt++; dev_connect(idev, iconn, &err); if (err != NULL) { error("%s", err->message); g_error_free(err); return FALSE; } return TRUE; }
/******************************************************************************* ** ** Function clcc_response ** ** Description response for CLCC command ** Can be iteratively called for each call index. Call index ** of 0 will be treated as NULL termination (Completes response) ** ** Returns bt_status_t ** *******************************************************************************/ static bt_status_t clcc_response(int index, bthf_call_direction_t dir, bthf_call_state_t state, bthf_call_mode_t mode, bthf_call_mpty_type_t mpty, const char *number, bthf_call_addrtype_t type) { CHECK_BTHF_INIT(); if (is_connected(NULL)) { tBTA_AG_RES_DATA ag_res; int xx; memset (&ag_res, 0, sizeof (ag_res)); /* Format the response */ if (index == 0) { ag_res.ok_flag = BTA_AG_OK_DONE; } else { BTIF_TRACE_EVENT6("clcc_response: [%d] dir %d state %d mode %d number = %s type = %d", index, dir, state, mode, number, type); xx = sprintf (ag_res.str, "%d,%d,%d,%d,%d", index, dir, state, mode, mpty); if (number) { if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+')) sprintf (&ag_res.str[xx], ",\"+%s\",%d", number, type); else sprintf (&ag_res.str[xx], ",\"%s\",%d", number, type); } } BTA_AgResult (btif_hf_cb.handle, BTA_AG_CLCC_RES, &ag_res); return BT_STATUS_SUCCESS; } return BT_STATUS_FAIL; }
int c_server::set_auto_reconnect(uint32_t interval) { if (0 == interval) { m_auto_reconnect_interval = 0; if (m_reconnect_timer) { REMOVE_TIMER(m_reconnect_timer); } } else { m_auto_reconnect_interval = interval; if (!is_connected()) { connect_timely(); } } return 0; }
/******************************************************************************* ** ** Function device_status_notification ** ** Description Combined device status change notification ** ** Returns bt_status_t ** *******************************************************************************/ static bt_status_t device_status_notification(bthf_network_state_t ntk_state, bthf_service_type_t svc_type, int signal, int batt_chg) { CHECK_BTHF_INIT(); if (is_connected(NULL)) { /* send all indicators to BTA. ** BTA will make sure no duplicates are sent out */ send_indicator_update(BTA_AG_IND_SERVICE, (ntk_state == BTHF_NETWORK_STATE_AVAILABLE) ? 1 : 0); send_indicator_update(BTA_AG_IND_ROAM, (svc_type == BTHF_SERVICE_TYPE_HOME) ? 0 : 1); send_indicator_update(BTA_AG_IND_SIGNAL, signal); send_indicator_update(BTA_AG_IND_BATTCHG, batt_chg); return BT_STATUS_SUCCESS; } return BT_STATUS_SUCCESS; }
/* * A graph is euler if it is connected (that is every vertex with degree > 0 is connected) * AND if all vertices have an even degree. This is also called a euler circuit * A graph is semi euler or euler path/walk if it has all vertices with degree > 0 connected * AND if there are 2 vertices with an odd degree. * Note that if # of odd vertices are > 2 || === 1, then it is not euler * Assumption right is that the graph is undirected, that is in this case, there is a directed edge back from * the destination to the source for the sake of explaining. */ int is_graph_euler(vertex *root) { // Check if the graph is connected int connected = is_connected(root); if (!connected) return 0; // Now if we get here, it means the graph is connected, now we need to decide if // the graph has a euler circuit or a path, count the #vertices with odd degree int odd = 0; while (root != NULL) { int edgecount = get_edge_count(root); odd += edgecount % 2 == 0 ? 0 : 1; root = root->nextvertex; } if (odd > 2) return 0; return odd == 2 ? 1 : 2; }
BOOL net_motion_test::connect_video(USER_HANDLE server_id,int slot) { if(is_connected()) { return FALSE; } m_video = new net_video_test(server_id,slot); if(m_video->start_preview(GetSafeHwnd(),0)) { tMotionRowCols rowcols; memset(&rowcols,0,sizeof(rowcols)); rowcols.slot = slot; if(HW_NET_SET_GetMotionRowCols(server_id,&rowcols) == FALSE) { return FALSE; } m_motion_col = rowcols.cols; m_motion_row = rowcols.rows; m_int_num_in_one_row = 1 + (m_motion_col - 1) / 32; memset(&m_motion_cfg,0,sizeof(m_motion_cfg)); m_motion_cfg.slot = slot; if(HW_NET_SET_GetMotionEx(server_id,&m_motion_cfg) == FALSE) { return FALSE; } hwplay_auto_adjust_size(m_video->handle(),FALSE); return TRUE; }else{ memset(&m_motion_cfg,0,sizeof(m_motion_cfg)); delete m_video; m_video = NULL; return FALSE; } }
static void stomp_process(stomp *stp, stomp_frame *f) { char *verb = stomp_frame_get_verb(f); if(strcmp("DISCONNECT", verb) == 0) { close_stomp(stp); return; } stomp_frame *rf = NULL; if(0 == is_connected(stp)) { rf = handle_connect_frame(stp, f); } else { if(0 == strcmp("SUBSCRIBE", verb)) { rf = handle_subscribe_frame(stp, f); } else if(0 == strcmp("UNSUBSCRIBE", verb)) { rf = handle_unsubscribe_frame(stp, f); } else if(0 == strcmp("SEND", verb)) { rf = handle_send_frame(stp, f); } else { char *body = malloc(strlen("Header is not invalid.") + strlen(verb) + 1); sprintf(body, "Header %s is not invalid.", verb); rf = create_error_frame("invalid verb", body); free(body); } } send_response_frame_and_free(stp, rf); }
int c_server::connect() { if (!is_legal_ip(m_ip) || !is_legal_port(m_port)) { return -1; } m_fd = net_connect_ser(m_ip, m_port, 0); if (is_connected()) { DEBUG_LOG("net_connect_ser: %s:%u, fd: %d", m_ip, m_port, m_fd); if (NULL != m_on_connected_func) { m_on_connected_func(); } return 0; } else { return -1; } }
/******************************************************************************* ** ** Function start_voice_recognition ** ** Description start voice recognition ** ** Returns bt_status_t ** *******************************************************************************/ static bt_status_t start_voice_recognition() { CHECK_BTHF_INIT(); if (is_connected(NULL)) { if (btif_hf_cb.peer_feat & BTA_AG_PEER_FEAT_VREC) { tBTA_AG_RES_DATA ag_res; memset(&ag_res, 0, sizeof(ag_res)); ag_res.state = 1; BTA_AgResult (btif_hf_cb.handle, BTA_AG_BVRA_RES, &ag_res); return BT_STATUS_SUCCESS; } else { return BT_STATUS_UNSUPPORTED; } } return BT_STATUS_NOT_READY; }
static void pickle1_each( const struct brubeck_metric *metric, const char *key, value_t value, void *backend) { struct brubeck_carbon *carbon = (struct brubeck_carbon *)backend; uint8_t key_len = (uint8_t)strlen(key); if (carbon->pickler.pos + PICKLE1_SIZE(key_len) >= PICKLE_BUFFER_SIZE) { pickle1_flush(carbon); } if (!is_connected(carbon)) return; if (!carbon->namespacing || metric->type == BRUBECK_MT_INTERNAL_STATS) { pickle1_push(&carbon->pickler, key, key_len, carbon->backend.tick_time, value); return; } char prefix_key[1024]; uint8_t prefix_key_len = 0; prefix_key_len = carbon_namespace(prefix_key, metric, key, key_len, carbon, true); pickle1_push(&carbon->pickler, prefix_key, prefix_key_len, carbon->backend.tick_time, value); if (IS_COUNTER(metric->type) && carbon->backend.sample_freq != 0) { prefix_key_len = carbon_namespace(prefix_key, metric, key, key_len, carbon, false); value_t normalized_val = value / carbon->backend.sample_freq; pickle1_push(&carbon->pickler, prefix_key, prefix_key_len, carbon->backend.tick_time, normalized_val); } }
bool pipesvr_win32::connect() { /* ensure need to connect */ if (!is_ready() || is_connected()) { return false; } if (!ConnectNamedPipe(pipe_, NULL)) { DWORD error = GetLastError(); switch (error) { case ERROR_PIPE_CONNECTED: /* process on other side of pipe */ return connected_ = true; case ERROR_PIPE_LISTENING: /* other end of pipe not connected */ return false; default: log_error("Failed to connect to pipe with error 0x%x.", error); return false; } } return connected_ = true; }
//_________________________________________________________________________ bool graph_molloy_hash::try_shuffle(int T, int K, int *backup_graph) { // init all int *Kbuff = NULL; bool *visited = NULL; if(K>2) { Kbuff = new int[K]; visited = new bool[n]; for(int i=0; i<n; i++) visited[i]=false; } int *back = backup_graph; if(back==NULL) back=backup(); // perform T edge swap attempts while(T--) random_edge_swap(K, Kbuff, visited); // clean if(visited != NULL) delete[] visited; if(Kbuff != NULL) delete[] Kbuff; // check & restore bool yo = is_connected(); restore(back); if(backup_graph == NULL) delete[] back; return yo; }
/******************************************************************************* ** ** Function connect_audio ** ** Description create an audio connection ** ** Returns bt_status_t ** *******************************************************************************/ static bt_status_t connect_audio( bt_bdaddr_t *bd_addr ) { CHECK_BTHF_CLIENT_SLC_CONNECTED(); if (is_connected(bd_addr)) { if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_CODEC) { BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BCC, 0, 0, NULL); } else { BTA_HfClientAudioOpen(btif_hf_client_cb.handle); } /* Inform the application that the audio connection has been initiated successfully */ btif_transfer_context(btif_in_hf_client_generic_evt, BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); return BT_STATUS_SUCCESS; } return BT_STATUS_FAIL; }
bool restart(bool abort = false) { if (!is_connected()){ error("restart: not connected"); return false; } int disconnect= 0; int node_list= m_nodeid; int restarted= ndb_mgm_restart3(m_handle, 1, &node_list, false, /* initial */ false, /* nostart */ abort, &disconnect); if (restarted != 1){ error("restart: failed to restart node %d, restarted: %d", m_nodeid, restarted); return false; } return true; }
// -1 if unsuccessful, else number of bytes received int TCPSocketConnection::receive_all(char* data, int length) { Timer tmr; int idx = 0; int time = -1; if (_cid < 0 || !is_connected()) return -1; tmr.start(); while (time < _timeout || _blocking) { idx += _wifi->recv(_cid, &data[idx], length - idx); if (idx < 0) return -1; if (idx == length) break; time = tmr.read_ms(); } return (idx == 0) ? -1 : idx; }
// 选择数据库 void ConnectorMySQL::select_db(const char *db, ErrorCode &error_code) { error_code.clear(); if (is_connected()) { mysql_select_db(&mysql_, db); int error = mysql_errno(&mysql_); if (error == 0) { select_db_ = db; } else { error_code.code = error; error_code.message = mysql_error(&mysql_); } } else { throw NotConnected(); } }
// 查询受影响行数 void ConnectorMySQL::affected_rows(const std::string &command, ErrorCode &error_code, std::vector<char> &result) { result.clear(); error_code.clear(); if (is_connected()) { mysql_real_query(&mysql_, command.data(), (unsigned long)command.size()); int error = mysql_errno(&mysql_); if (error != 0) { error_code.code = error; error_code.message = mysql_error(&mysql_); return; } SerializeInterface<MYSQL>::serialize_affected_rows(mysql_, result); } else { throw NotConnected(); } }
BOOL net_motion_test::show_motion(BOOL bshow) { if(!is_connected()) { return FALSE; } if(bshow == m_show_motion) { return FALSE; } if(bshow) { hwplay_register_draw_fun(m_video->handle(),motion_draw,(long)this); }else{ hwplay_register_draw_fun(m_video->handle(),NULL,0); } m_show_motion = bshow; return TRUE; }
bool MsgQueueConnection::ReceiveMessage() { //GetInQueue().ReceiveBlocking(*(reinterpret_cast<message_buf*>(&m_cBuffer))); EReceiveStatus status; while ((status = Receive()) == RS_TIME_OUT) { if (!is_connected()) return false; // The MsgQueueNotifier may have called OnDisconnected } if (status == RS_ERROR) return false; //STTester_PROCESS_BUFFER(); if (m_cBuffer.len==KMaxQueueSlotSize) ///check for multipart msg { message_buf tmpBuf; bool done = false; while (!done) { //Call non-blocking version of Receive(), returns KErrUnderFlow if no msg in queue if (KErrNone==(GetInQueue().Receive(tmpBuf))) { int nBytes = min(tmpBuf.len, KMaxQueueSlotSize); Mem::Copy(m_cBuffer.msg+m_cBuffer.len, tmpBuf.buf, nBytes); m_cBuffer.len += nBytes; if (nBytes<KMaxQueueSlotSize) done = true; } else break; } } return true; }
int jdk_client_socket::write_data_block( const void *buf_, size_t len ) { size_t todo=len; int cnt=0; const unsigned char *buf = (const unsigned char *)buf_; while( is_connected() && todo>0 ) { int c=write_data( buf, todo ); if( c<=0 ) { write_failed(); break; } cnt+=c; buf+=c; todo-=c; } return cnt; }