void receive_msg(int newsockfd, char *buffer, char *name){ char *msg; msg = get_mes_from_client(newsockfd, buffer); char *first = get_1st_arg(msg); //char *second = get_2nd_arg(msg); if(strcmp(first, "send") == 0){ char * to_send; to_send = get_mes_from_client(newsockfd, buffer); char *mail_to = get_1st_arg(to_send); char *message = get_2nd_arg(to_send); //create_file(get_1st_arg(to_send), first, get_2nd_arg(to_send)); create_file(mail_to, name, message); receive_msg(newsockfd, buffer, name); //}else if (!strcmp(get_1st_arg(buffer), "readinbox")) { }else if (strcmp(first, "readinbox") == 0) { printf("readinbox"); receive_msg(newsockfd, buffer, name); }else if (strcmp(first, "exit") == 0) { printf("bb"); exit(1); } else{ printf("wrong command"); receive_msg(newsockfd, buffer, name); } //buffer = get_mes_from_client(newsockfd, buffer); receive_msg(newsockfd, buffer, name); }
JNIEXPORT jobject JNICALL Java_com_net_NetClient_receive(JNIEnv *env, jclass obj, jint jfd) { struct packet pack; if (receive_msg(fid, jfd, &pack)) { return NULL; } jclass mcls = (*env)->FindClass(env, "com/net/Message"); jfieldID jid = (*env)->GetFieldID(env, mcls, "id", "I"); jfieldID jifindex = (*env)->GetFieldID(env, mcls, "ifindex", "I"); jfieldID jlen = (*env)->GetFieldID(env, mcls, "len", "S"); jfieldID jsid = (*env)->GetFieldID(env, mcls, "sid", "S"); jfieldID jdid = (*env)->GetFieldID(env, mcls, "did", "S"); jfieldID jenc = (*env)->GetFieldID(env, mcls, "enc", "Z"); jfieldID jdata = (*env)->GetFieldID(env, mcls, "data", "[B"); jobject mobj = (*env)->AllocObject(env, mcls); (*env)->SetIntField(env, mobj, jid, pack.id); (*env)->SetIntField(env, mobj, jifindex, pack.ifindex); (*env)->SetShortField(env, mobj, jlen, pack.len); (*env)->SetShortField(env, mobj, jsid, pack.sid); (*env)->SetShortField(env, mobj, jdid, pack.did); (*env)->SetBooleanField(env, mobj, jenc, pack.enc); jbyteArray jarr = (*env)->NewByteArray(env, pack.len); (*env)->SetByteArrayRegion(env, jarr, 0, pack.len, pack.data); (*env)->SetObjectField(env, mobj, jdata, jarr); return mobj; }
static int prepare_update(struct handler_priv *priv, struct img_type *img) { int ret; char msg[128]; int len; ret = switch_mode(priv->reset.gpiodev, priv->reset.offset, priv->prog.gpiodev, priv->prog.offset, MODE_PROG); if (ret < 0) { return -ENODEV; } DEBUG("Using %s", img->device); priv->fduart = open(img->device, O_RDWR); if (priv->fduart < 0) { ERROR("Cannot open UART %s", img->device); return -ENODEV; } set_uart(priv->fduart); /* No FW data to be sent */ priv->nbytes = 0; write_msg(priv->fduart, "$PROG;"); len = receive_msg(priv->fduart, msg, sizeof(msg), priv->timeout, priv->debug); if (len < 0 || strcmp(msg, "$READY;")) return -EBADMSG; return 0; }
void TeensyUnit::init(){ //----- Begin slow PWM driver ---- spwm_init(1000); //---- initialize I2C accelerometer on Tentacle module --- tentacle_0.init(); tentacle_1.init(); tentacle_2.init(); //===== clear all existing messages ====== unsigned long clearing_counter = 0; while (receive_msg()){ // this prevents the Teensy from being stuck in infinite loop clearing_counter++; if (clearing_counter>100){ break; } } }
int receive_cg_data_u(cg_prob *p) { int r_bufid; r_bufid = receive_msg(p->master, CG_DATA); receive_char_array((char *)&p->par, sizeof(cg_params)); receive_int_array(&p->draw_graph, 1); #ifdef USE_SYM_APPLICATION switch( user_receive_cg_data(&p->user, p->draw_graph) ){ case USER_SUCCESS: case USER_AND_PP: case USER_NO_PP: /* User function terminated without problems. No post-processing. */ case USER_DEFAULT: freebuf(r_bufid); return(TRUE); case USER_ERROR: default: freebuf(r_bufid); /* Unexpected return value. Do something!! */ return(FALSE); } #else freebuf(r_bufid); return(TRUE); #endif }
// When it is an actuator this function will be called void actuator(int pid, int msgid, char *name) { message_t sensor_data; // struct to hold server response while(1) { receive_msg(msgid, (void *)&sensor_data, sizeof(packet_t), (long int) pid, 0); // wait for message from server strcpy(sensor_data.packet.name, (sensor_data.packet.value) ? "ON" : "OFF"); printf( "%s was turned %s at pid: %d\n", name, sensor_data.packet.name, pid ); // print a message to console sensor_data.msg_type = MESSAGE_KEY; sensor_data.packet.pid = pid; // set pid sensor_data.packet.value = 1; // set value to true sensor_data.packet.type = ACTUATOR; // set type to actuator // send back response to server send_msg(msgid, (void *)&sensor_data, sizeof(packet_t), IPC_NOWAIT); } }
static void rdfbot_main() { Box (*my_map)[GRID_HEIGHT]; int x,y; Message m; for (;;) { while (messages()) { receive_msg(&m); if (m.opcode == OP_ACK) { my_map = map_get(); /* * Erase the old RDF bits * * You can get a RDF pattern in the map, then * drive around, and get another WITHOUT clearing * the map, and the X bit will be set at the * intersections. */ for (y = 0; y < GRID_HEIGHT; y++) { for (x = 0; x < GRID_WIDTH; x++) { my_map[x][y].flags &= ~((unsigned int) 0 | ANY_RDF | X_RDF ); } } rdf_map(my_map); printf("\n"); for (y = 0; y < GRID_HEIGHT; y++) { for (x = 0; x < GRID_WIDTH; x++) { if (my_map[x][y].flags & X_RDF) printf("X"); else if (my_map[x][y].flags & RED_RDF) printf("R"); else if (my_map[x][y].flags & GREEN_RDF) printf("G"); else if (my_map[x][y].flags & YELLOW_RDF) printf("Y"); else if (my_map[x][y].flags & INSIDE_MAZE) printf("#"); else printf(" "); } printf("\n"); } } } done(); } }
void Talker::start_thread() { SBL_INFO("RTSP talker thread %d listening to %s:%d", id(), _client_ip, _client_port); Parser parser; Responder responder(this, _tx_buffer, BUFFER_SIZE); Method method = OPTIONS; do { try { MsgType msg_type = receive_msg(); if (msg_type == MSG_RTSP) { SBL_MSG(MSG::SERVER, "RTSP Server thread %d received message length %d:\n%s", id(), _msg_size, _rx_buffer); method = parser.parse(_rx_buffer, _msg_size); int reply_size = responder.reply(parser.data); SBL_MSG(MSG::SERVER, "RTSP Server thread %d reply:\n%s", id(), _tx_buffer); if (!_socket.send(_tx_buffer, reply_size, false)) { SBL_MSG(MSG::SERVER, "RTSP Server thread %d, socket error, exiting thread ...\n", id()); break; } if (method == PLAY) { RTSP_ASSERT(client(), BAD_REQUEST); client()->play(); } } else if (msg_type == MSG_RTCP) { SBL_MSG(MSG::SERVER, "RTSP Server thread %d received RTCP message length %d", id(), _msg_size); if (_rtcp_parser) { RTSP_ASSERT(_msg_size >= 4, BAD_REQUEST); _rtcp_parser->parse(_rx_buffer + 4, _msg_size - 4); } else SBL_WARN("Thread %d, received RTCP message, but RTCP parser is not yet ready", id()); } else method = TEARDOWN; _rx_bytes -= _msg_size; if (_rx_bytes > 0) memmove(_rx_buffer, _rx_buffer + _msg_size, _rx_bytes); } catch (Errcode errcode) { // If we catch Errcode, it may be possible to continue, so reply with error code SBL_MSG(MSG::SERVER, "RTSP talker %d caught error code %d", id(), errcode); int reply_size = responder.reply(parser.data, errcode); SBL_MSG(MSG::SERVER, "RTSP talker thread %d reply:\n%s\n", id(), _tx_buffer); if (!_socket.send(_tx_buffer, reply_size, false)) { SBL_MSG(MSG::SERVER, "Talker %d unable to send reply, exiting...", id()); method = TEARDOWN; } // We throw out any data already received if we had error _rx_bytes = _msg_size = 0; } catch (SBL::Exception& ex) { // if we catch Exception, it is coming from Socket, so can't send anything back // log the error in the log file and terminate the thread; SBL_MSG(MSG::SERVER, "RTSP talker %d exiting, caught exception %s", id(), ex.what()); method = TEARDOWN; } } while (method != TEARDOWN); teardown(); SBL_INFO("RTSP talker %d terminating", id()); delete this; }
/* Write the bits of the array in the remote device */ DLL int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *src) { int rc; int i; int byte_count; int req_length; int bit_check = 0; int pos = 0; uint8_t req[MAX_MESSAGE_LENGTH]; if (nb > MODBUS_MAX_WRITE_BITS) { if (ctx->debug) { fprintf(stderr, "ERROR Writing too many bits (%d > %d)\n", nb, MODBUS_MAX_WRITE_BITS); } errno = EMBMDATA; return -1; } req_length = ctx->backend->build_request_basis(ctx, _FC_WRITE_MULTIPLE_COILS, addr, nb, req); byte_count = (nb / 8) + ((nb % 8) ? 1 : 0); req[req_length++] = byte_count; for (i = 0; i < byte_count; i++) { int bit; bit = 0x01; req[req_length] = 0; while ((bit & 0xFF) && (bit_check++ < nb)) { if (src[pos++]) req[req_length] |= bit; else req[req_length] &=~ bit; bit = bit << 1; } req_length++; } rc = send_msg(ctx, req, req_length); if (rc > 0) { uint8_t rsp[MAX_MESSAGE_LENGTH]; rc = receive_msg(ctx, rsp, MSG_CONFIRMATION); if (rc == -1) return -1; rc = check_confirmation(ctx, req, rsp, rc); } return rc; }
int proto_sctp_read(struct socket_info *si, int* bytes_read) { struct receive_info ri; int len; #ifdef DYN_BUF char* buf; #else static char buf [BUF_SIZE+1]; #endif char *tmp; unsigned int fromlen; struct sctp_sndrcvinfo sinfo; #ifdef DYN_BUF buf=pkg_malloc(BUF_SIZE+1); if (buf==0) { LM_ERR(" could not allocate receive buffer in pkg memory\n"); goto error; } #endif fromlen=sockaddru_len(si->su); len = sctp_recvmsg(si->socket, buf, BUF_SIZE, &ri.src_su.s, &fromlen, &sinfo, 0); if (len==-1) { if (errno==EAGAIN) { LM_DBG("packet with bad checksum received\n"); return 0; } if ((errno==EINTR)||(errno==EWOULDBLOCK)|| (errno==ECONNREFUSED)) return -1; LM_ERR("sctp_recvmsg:[%d] %s\n", errno, strerror(errno)); return -2; } /* we must 0-term the messages, receive_msg expects it */ buf[len]=0; /* no need to save the previous char */ ri.bind_address = si; ri.dst_port = si->port_no; ri.dst_ip = si->address; ri.proto = si->proto; ri.proto_reserved1 = ri.proto_reserved2 = 0; su2ip_addr(&ri.src_ip, &ri.src_su); ri.src_port=su_getport(&ri.src_su); if (ri.src_port==0) { tmp=ip_addr2a(&ri.src_ip); LM_INFO("dropping 0 port packet from %s\n", tmp); return 0; } /* receive_msg must free buf too!*/ receive_msg(buf, len, &ri); return 0; }
void authenticate(int fd, char* user, char* psw) { /* Authentication */ char both[BUFFER_SIZE],temp_buf[RESULT_BUFFER]; sprintf(both, "%s %s:%s\n", LOGIN_CMD, user, psw); if(send(fd,both,BUFFER_SIZE,0) < 0) error("ERROR sending message"); receive_msg(fd,temp_buf); printf("%s", temp_buf); /* Tell if logged or not */ if(strcmp(temp_buf,"Access denied\n")==0) exit(0); }
void serve(int client_fd) { char buffer[100]; bzero(buffer, 100); int bytes = read(client_fd, buffer, 100); if(bytes < 0) { std::cerr << "ERROR: read!" << std::endl; close(client_fd); return; } std::cout << buffer; //I really wanted to use regex for this, but gcc 4.8.4 doesn't support [0-9] std::string receive_msg(buffer); std::string ip_address; std::string port; struct sockaddr_in throwaway; if(receive_msg.substr(0,8) == "register" && inet_pton(AF_INET, receive_msg.substr(9,receive_msg.find(' ', 10) - 9).c_str(), &(throwaway.sin_addr))) { //get ip/port pairs from register message and place into register list ip_address = receive_msg.substr(9,receive_msg.find(' ', 10) - 9); port = receive_msg.substr(receive_msg.find(' ', 10) + 1, receive_msg.find('\r') - receive_msg.find(' ', 10) - 1); register_list.emplace_back(ipport(ip_address,port)); strcpy(buffer,"success\r\n"); send(client_fd, buffer, 100, 0); } else if(strcmp(receive_msg.c_str(), "list-servers\r\n") == 0) { //send list of ip/port pairs to client bzero(buffer, 100); strcpy(buffer, "success\r"); bytes = send(client_fd, buffer, 100, 0); if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; } for(auto i : register_list) { bzero(buffer, 100); strcpy(buffer, i.ip_address.c_str()); strcat(buffer, " "); strcat(buffer, i.port.c_str()); strcat(buffer, "\r"); bytes = send(client_fd, buffer, 100, 0); if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; } } strcpy(buffer, "\n"); bytes = send(client_fd, buffer, 1, 0); if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; } } else { bzero(buffer, 100); strcpy(buffer, "failure\r\n"); bytes = send(client_fd, buffer, 100, 0); if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; } } }
void cg_initialize(cg_prob *p, int master_tid) { #ifndef COMPILE_IN_CG int bytes, msgtag; #if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) int s_bufid, r_bufid, info; #endif #endif #if !defined(COMPILE_IN_TM) || !defined(COMPILE_IN_LP) int r_bufid, s_bufid = 0; #endif /*------------------------------------------------------------------------ * Receive the problem data *------------------------------------------------------------------------*/ #ifdef COMPILE_IN_CG p->master = master_tid; #else /* We only need to do this part if the CG is running as a separate process*/ /* Otherwise, all of this setup is done in the master in the function */ /* pack_cg_data_u()*/ /* set stdout to be line buffered */ setvbuf(stdout, (char *)NULL, _IOLBF, 0); register_process(); r_bufid = receive_msg(ANYONE, MASTER_TID_INFO); bufinfo(r_bufid, &bytes, &msgtag, &p->tree_manager); receive_int_array(&p->master, 1); receive_int_array(&p->proc_index, 1); freebuf(r_bufid); #endif #if !defined(COMPILE_IN_TM) || !defined(COMPILE_IN_LP) || \ !defined(COMPILE_IN_CG) /* This part, we only need to do if we are not running in full serial mode*/ s_bufid = init_send(DataInPlace); send_msg(p->master, REQUEST_FOR_CG_DATA); freebuf(s_bufid); receive_cg_data_u(p); #endif (void) used_time(&p->tt); }
int main(int argc, char *argv[]) { int msgid; // msgid is used to locate the right message queue int pid = getpid(); // get the process id int type; // sensor or actuator message_t sensor_data, server_response; // sensor or actuator if (argc < 4) { fprintf(stderr, "Not enough args, you gave %d, 4 were required!\n", argc); exit(EXIT_FAILURE); } else { // copy the command line arguments to the sensor_data struct strcpy(sensor_data.packet.name, argv[1]); sscanf(argv[2], "%d", &sensor_data.packet.type); sscanf(argv[3], "%d", &sensor_data.packet.threshold); } type = sensor_data.packet.type; // save the type in case of corruption sensor_data.packet.pid = pid; // set the pid sensor_data.msg_type = REGISTER_KEY; // set this as a register message // Make sure queue is there msgid = msgget((key_t)1234, 0666); if (msgid == -1) { fprintf(stderr, "msgget failed with error: %d\n", errno); exit(EXIT_FAILURE); } // send the register message send_msg(msgid, (void *)&sensor_data, sizeof(packet_t), IPC_NOWAIT); // wait for an acknowladgement receive_msg(msgid, (void *)&server_response, sizeof(packet_t), (long int) pid, 0); // if the acknowladgement is false if(!server_response.packet.value) { fprintf(stderr, "the acknowladge failed for proccess: %d\n", pid); exit(EXIT_FAILURE); } printf("Sent:\n"); print_message(&sensor_data); printf("Recieved:\n"); print_message(&server_response); // check to see if this is an actuator or sensor if(type) { actuator(pid, msgid, sensor_data.packet.name); } else { sensor(pid, msgid); } exit(EXIT_SUCCESS); }
// The procedure for the child process. void child(void) { message_t data_received; next_device = 0; int current_device; struct sigaction cleanup; cleanup.sa_handler = cleanup_child; sigemptyset(&cleanup.sa_mask); cleanup.sa_flags = 0; sigaction(SIGTERM, &cleanup, 0); shared_mem = shmat(shmid, (void *) 0, 0); if (shared_mem == (void *) -1) { fprintf(stderr, "shmat failed\n"); exit(EXIT_FAILURE); } printf("Memory attached at %X\n", (long int)shared_mem); for (int i = 0; i < MAX_DEVICES; i++) { devices[i].has_partner = 0; } while(1) { receive_msg(msgid, (void *)&data_received, sizeof(packet_t), RECEIVE_TYPE, 0); // Registration vs Message Section if (data_received.msg_type == REGISTER_KEY) { register_device(&data_received, next_device++); continue; } else if (data_received.msg_type == MESSAGE_KEY) { current_device = find_device(data_received.packet.pid); // Ignore if the device hasn't registered yet if (current_device == -1) { fprintf(stderr, "Could not find device: %d\n", data_received.packet.pid); continue; } } // If the device dosen't have a partner then skip to the next message. if (!devices[current_device].has_partner) { continue; } // Message Recieved From Device if (data_received.packet.type) { // Actuator actuator(&data_received, current_device); } else { // Sensor sensor(&data_received, current_device); } } }
int main(int argc, const char * argv[]) { init_server(); void* received = malloc(2048); time_t send_hb_time = time(0); time_t check_hb_time = time(0); while(1){ receive_msg(received, _size(MSG_LOGIN), LOGIN, &handle_login); receive_msg(received, _size(MSG_LOGIN), LOGOUT, &handle_logout); receive_msg(received, _size(MSG_REQUEST), REQUEST, &handle_request); receive_msg(received, _size(MSG_CHAT_MESSAGE), MESSAGE, &handle_message); receive_msg(received, _size(MSG_ROOM), ROOM, &handle_room_action); receive_msg(received, _size(MSG_SERVER2SERVER), SERVER2SERVER, &handle_server_heartbeat); if (time(0) - send_hb_time > 2) { send_hb_time = time(0); send_heartbeats_to_clients(); } if (time(0) - check_hb_time > 2) { check_hb_time = time(0); check_heartbeat_table(); } usleep(100); } free(received); clean(); return 0; }
int display_map(t_player *p, char is_first_player, char gui) { if (is_first_player) { if (gui) { if (send_msg(p->ids[MSG], T_REFRESH, 0) || receive_msg(p->ids[MSG], T_DONE, 0) == -1) return (FAILURE); } else show_map(p->map, p->m_size); } return (SUCCESS); }
void main(void) { int i; set_tris_b(0x10000000); output_high(PIN_B0); delay_ms(500); output_low(PIN_B0); fprintf(A, "rs232 init...\n\r"); usb_init_cs(); while(1) { if(input(PIN_B7)) { output_high(PIN_B0); usb_task(); while(input(PIN_B7)) { if (usb_enumerated()) { if (usb_kbhit(1)) { receive_msg(); //usb_get_packet(1, rdata, 1); //fprintf(A, "%u", rdata[0]); //switch(rdata[0]) //{ // case 0x01: output_high(PIN_B0); break; // case 0x02: output_low(PIN_B0); break; //} } } } usb_detach(); output_low(PIN_B0); } } }
int receive_msg_and_treat(char *buff, int client_sock, struct sockaddr_in client, t_cinfo *client_info) { while (strcmp(buff, "QUIT")) { if (receive_msg(client_sock, buff, client) == -1) { close(client_sock); return (0); } if (treat_command(buff, client_info) == -1) { close(client_sock); return (-1); } } return (0); }
void TeensyUnit::init(){ //----- Begin slow PWM driver ---- spwm_init(1000); //===== clear all existing messages ====== unsigned long clearing_counter = 0; while (receive_msg()){ // this prevents the Teensy from being stuck in infinite loop clearing_counter++; if (clearing_counter>100){ break; } } }
void XYPenPlotterController::receivePlotterMessages() { if(receive_msg(&rcv_msg, 100)) return; if(rcv_msg.status == PLOTTER_DRAW) { qDebug("Plotter start draw!"); setCurrentState("RUNNING"); } else if(rcv_msg.status == PLOTTER_START) { qDebug("Plotter running... %d%%", rcv_msg.data); // Update progress bar... setProgress(rcv_msg.data); } else if(rcv_msg.status == PLOTTER_UNPAUSE) { setCurrentState("RUNNING"); } else if(rcv_msg.status == PLOTTER_PAUSE) { setCurrentState("PAUSED"); } else if(rcv_msg.status == PLOTTER_HOME) { setCurrentState("STOPPED"); } else if(rcv_msg.status == PLOTTER_STOP) { if(currentState != "STOPPED") setCurrentState("STOPPED"); // Reset progress bar... setProgress(0); } else { qDebug("Something went wrong! Plotter status = 0x%x", rcv_msg.status); } msg.data = 0xff; if(send_msg(&msg)) return; }
/* Reads the data from a remove device and put that data into an array */ static int read_registers(modbus_t *ctx, int function, int addr, int nb, uint16_t *dest) { int rc; int req_length; uint8_t req[_MIN_REQ_LENGTH]; uint8_t rsp[MAX_MESSAGE_LENGTH]; if (nb > MODBUS_MAX_READ_REGISTERS) { if (ctx->debug) { fprintf(stderr, "ERROR Too many registers requested (%d > %d)\n", nb, MODBUS_MAX_READ_REGISTERS); } errno = EMBMDATA; return -1; } req_length = ctx->backend->build_request_basis(ctx, function, addr, nb, req); rc = send_msg(ctx, req, req_length); if (rc > 0) { int offset; int i; rc = receive_msg(ctx, rsp, MSG_CONFIRMATION); if (rc == -1) return -1; rc = check_confirmation(ctx, req, rsp, rc); if (rc == -1) return -1; offset = ctx->backend->header_length; for (i = 0; i < rc; i++) { /* shift reg hi_byte to temp OR with lo_byte */ dest[i] = (rsp[offset + 2 + (i << 1)] << 8) | rsp[offset + 3 + (i << 1)]; } } return rc; }
/* Write the values from the array to the registers of the remote device */ DLL int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *src) { int rc; int i; int req_length; int byte_count; uint8_t req[MAX_MESSAGE_LENGTH]; if (nb > MODBUS_MAX_WRITE_REGISTERS) { if (ctx->debug) { fprintf(stderr, "ERROR Trying to write to too many registers (%d > %d)\n", nb, MODBUS_MAX_WRITE_REGISTERS); } errno = EMBMDATA; return -1; } req_length = ctx->backend->build_request_basis(ctx, _FC_WRITE_MULTIPLE_REGISTERS, addr, nb, req); byte_count = nb * 2; req[req_length++] = byte_count; for (i = 0; i < nb; i++) { req[req_length++] = src[i] >> 8; req[req_length++] = src[i] & 0x00FF; } rc = send_msg(ctx, req, req_length); if (rc > 0) { uint8_t rsp[MAX_MESSAGE_LENGTH]; rc = receive_msg(ctx, rsp, MSG_CONFIRMATION); if (rc == -1) return -1; rc = check_confirmation(ctx, req, rsp, rc); } return rc; }
/* Reads IO status */ static int read_io_status(modbus_t *ctx, int function, int addr, int nb, uint8_t *dest) { int rc; int req_length; uint8_t req[_MIN_REQ_LENGTH]; uint8_t rsp[MAX_MESSAGE_LENGTH]; req_length = ctx->backend->build_request_basis(ctx, function, addr, nb, req); rc = send_msg(ctx, req, req_length); if (rc > 0) { int i, temp, bit; int pos = 0; int offset; int offset_end; rc = receive_msg(ctx, rsp, MSG_CONFIRMATION); if (rc == -1) return -1; rc = check_confirmation(ctx, req, rsp, rc); if (rc == -1) return -1; offset = ctx->backend->header_length + 2; offset_end = offset + rc; for (i = offset; i < offset_end; i++) { /* Shift reg hi_byte to temp */ temp = rsp[i]; for (bit = 0x01; (bit & 0xff) && (pos < nb);) { dest[pos++] = (temp & bit) ? TRUE : FALSE; bit = bit << 1; } } } return rc; }
static void *udp_intertask_interface(void *args_p) { while(1) { MessageDef *receivedMessage; receive_msg(TASK_UDP, &receivedMessage); switch(receivedMessage->messageId) { case UDP_INIT: { UdpInit *init_p; init_p = &receivedMessage->msg.udpInit; udp_create_socket(init_p->port, init_p->address); } break; case UDP_DATA_REQ: { UdpDataReq *udp_data_req_p; struct sockaddr_in peer_addr; udp_data_req_p = &receivedMessage->msg.udpDataReq; memset(&peer_addr, 0, sizeof(struct sockaddr_in)); peer_addr.sin_family = AF_INET; peer_addr.sin_port = htons(udp_data_req_p->peer_port); peer_addr.sin_addr.s_addr = (udp_data_req_p->peer_address); UDP_DEBUG("Sending message of size %u to "IPV4_ADDR" and port %u\n", udp_data_req_p->buffer_length, IPV4_ADDR_FORMAT(udp_data_req_p->peer_address), udp_data_req_p->peer_port); sendto(udp_fd, udp_data_req_p->buffer, udp_data_req_p->buffer_length, 0, (struct sockaddr *)&peer_addr, sizeof(struct sockaddr_in)); } break; default: { UDP_DEBUG("Unknown message ID %d\n", receivedMessage->messageId); } break; } free(receivedMessage); receivedMessage = NULL; } return NULL; }
static void dispatch(const Octstr *filename, void *data) { Octstr *msg_s; Msg *msg; void(*receive_msg)(Msg*) = data; /* debug("", 0, "dispatch(%s,...) called", octstr_get_cstr(filename)); */ msg_s = octstr_read_file(octstr_get_cstr(filename)); if (msg_s == NULL) return; msg = store_msg_unpack(msg_s); octstr_destroy(msg_s); if (msg != NULL) { receive_msg(msg); counter_increase(counter); } else { error(0, "Could not unpack message `%s'", octstr_get_cstr(filename)); } }
int init_player(t_player *p) { int msg; msg = p->t_nbr; p->map = shmat(p->ids[SHM], 0, SHM_R | SHM_W); if ((void *)p->map == (void *)-1) return (my_perror()); if ((p->pos = init_player_pos(p->map, p->t_nbr, p->m_size)) == -1) return (FAILURE); p->map[p->pos] = p->t_nbr; usleep(100000); if (get_nbr_team(p->map, p->m_size) == 1) while (msg == p->t_nbr) msg = receive_msg(p->ids[MSG], T_JOIN, 0); if (msg == -1) return (FAILURE); if (send_msg(p->ids[MSG], T_JOIN, msg)) return (my_perror()); return (SUCCESS); }
/* Write a value to the specified register of the remote device. Used by write_bit and write_register */ static int write_single(modbus_t *ctx, int function, int addr, int value) { int rc; int req_length; uint8_t req[_MIN_REQ_LENGTH]; req_length = ctx->backend->build_request_basis(ctx, function, addr, value, req); rc = send_msg(ctx, req, req_length); if (rc > 0) { /* Used by write_bit and write_register */ uint8_t rsp[_MIN_REQ_LENGTH]; rc = receive_msg(ctx, rsp, MSG_CONFIRMATION); if (rc == -1) return -1; rc = check_confirmation(ctx, req, rsp, rc); } return rc; }
void serve_client(int fd, uint32_t events, user_list* lista) { char *msg = 0; size_t len = 0; if (events & EPOLLIN) { len = receive_msg(&msg, fd); if (len > 0) { if (msg[0] == '2') { printf("%s\n", msg); handle_login(msg, len, fd, lista); } else if (msg[0] == '6') { printf("%s\n", msg); handle_user_list(msg, len, fd, lista); } } //free(msg); } else { close(fd); epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, 0); //TODO remove from user list } }
void wait_for_you_can_die(dg_prob *dgp, FILE *write_to) { int bufid, s_bufid, bytes, msgtag, sender; FREE(dgp->windows); /* invoke the Igd_QuitAll function */ spprint(write_to, "Igd_QuitAll\n"); if (dgp->waiting_to_die == 2 * TRUE) exit(0); while (TRUE){ receive_msg(ANYONE, ANYTHING); bufinfo(bufid, &bytes, &msgtag, &sender); if (msgtag != CTOI_YOU_CAN_DIE){ s_bufid = init_send(DataInPlace); send_msg(sender, ITOC_APPLICATION_KILLED); freebuf(s_bufid); }else{ exit(0); } } }