static void run_server(mach_port_t portset, mach_port_t notification_port) { struct DummyMsg_t DumMsg, DumMsgReply; int isFinished = 0; while (! isFinished) { bzero(&DumMsg, sizeof DumMsg); bzero(&DumMsgReply, sizeof DumMsgReply); DumMsg.head.msgh_size = sizeof DumMsg; DumMsg.head.msgh_local_port = portset; mach_msg_return_t msgcode = mach_msg(&DumMsg.head, MACH_RCV_MSG, 0, sizeof DumMsg, portset, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); if (msgcode != MACH_MSG_SUCCESS) { if (ERR_FILE) fprintf(ERR_FILE, "error %s in Receive, message will be ignored.\n", mach_error_string((kern_return_t)msgcode)); } else { /* Try handling it from the server */ boolean_t handled = handle_server_message(&DumMsg, &DumMsgReply); if (! handled) { /* Could be a No Senders notification */ if (DumMsg.head.msgh_id == MACH_NOTIFY_NO_SENDERS) { /* Our parent process died, or closed our port, so we should go away */ if (ERR_FILE) fprintf(ERR_FILE, "Parent appears to have closed its port, so we're exiting.\n"); isFinished = 1; } else { if (ERR_FILE) fprintf(ERR_FILE, "Unknown Mach message id %ld\n", (long)DumMsg.head.msgh_id); } } } if (ERR_FILE) fflush(ERR_FILE); } }
int main(int argc, char *argv[]) { connection_info connection; fd_set file_descriptors; if (argc != 4) { fprintf(stderr,"Usage: %s <username> <IP> <port>\n", argv[0]); exit(1); } connect_to_server(&connection, argv[1], argv[2], argv[3]); //keep communicating with server while(true) { FD_ZERO(&file_descriptors); FD_SET(STDIN_FILENO, &file_descriptors); FD_SET(connection.socket, &file_descriptors); fflush(stdin); if(select(connection.socket+1, &file_descriptors, NULL, NULL, NULL) < 0) { perror("Select failed."); exit(1); } if(FD_ISSET(STDIN_FILENO, &file_descriptors)) { handle_user_input(&connection); } if(FD_ISSET(connection.socket, &file_descriptors)) { handle_server_message(&connection); } } close(connection.socket); return 0; }
int ldcs_audit_server_md_cobo_CB(int fd, int nc, void *data) { int rc=0; ldcs_process_data_t *ldcs_process_data = ( ldcs_process_data_t *) data ; ldcs_message_t msg; double starttime = ldcs_get_time(); node_peer_t peer; /* receive msg from cobo network */ rc = read_msg(fd, &peer, &msg); if (rc == -1) return -1; rc = handle_server_message(ldcs_process_data, peer, &msg); ldcs_process_data->server_stat.md_cb.cnt++; ldcs_process_data->server_stat.md_cb.time+=(ldcs_get_time() - starttime); if (msg.data) free(msg.data); return(rc); }