void server_mode() {
	sockaddr_storage_t msgname;
	int server_socket,
		error,
		stream;
	int assoc_num =0;
	struct msghdr inmessage;
	struct iovec iov;
	char *big_buffer;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];

	if ((big_buffer = malloc(REALLY_BIG)) == NULL) {
		printf("malloc failure: %s\n", strerror(errno));
		DUMP_CORE;
	}

	printf("Running in Server Mode...\n");

	memset(&inmessage, 0, sizeof(inmessage));
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen =1;
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);
	inmessage.msg_name = &msgname;
	inmessage.msg_namelen = sizeof (msgname);

	stream = 1;

	server_socket = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	if (server_socket < 0) {
		printf("Socket Failure:  %s.\n", strerror(errno));
		DUMP_CORE;
	}

	error = bind(server_socket, &server_loop.sa, sizeof(server_loop));
	if (error != 0 ) {
		printf("Bind Failure: %s.\n", strerror(errno));
		DUMP_CORE;
	}

	error = listen(server_socket, 1);
	if (error != 0) {
		printf("Listen Failure: %s.\n", strerror(errno));
		DUMP_CORE;
	}
	while (TRUE) {
		error = recvmsg(server_socket, &inmessage, MSG_WAITALL);
		if (error < 0) {
			printf("Receive Failure: %s\n",
			strerror(errno));
		} else {
		if (inmessage.msg_flags & MSG_NOTIFICATION)
			assoc_num = event_received(&inmessage, assoc_num);
		else
			data_received(&inmessage, error, stream, server_socket);
		}
	}
}
void
MulticastDataLink::sample_received(ReceivedDataSample& sample)
{
  switch (sample.header_.message_id_) {
  case TRANSPORT_CONTROL: {
    // Transport control samples are delivered to all sessions
    // regardless of association status:
    {
      ACE_GUARD(ACE_SYNCH_RECURSIVE_MUTEX,
                guard,
                this->session_lock_);

      char* ptr = sample.sample_->rd_ptr();
      for (MulticastSessionMap::iterator it(this->sessions_.begin());
          it != this->sessions_.end(); ++it) {
        it->second->control_received(sample.header_.submessage_id_,
                                    sample.sample_);
        // reset read pointer
        sample.sample_->rd_ptr(ptr);
      }
    }
    if (this->check_fully_association_) {
      this->transport_->check_fully_association ();
      this->check_fully_association_ = false;
    }
  } break;

  case SAMPLE_ACK:
    ack_received(sample);
    break;

  default:
    data_received(sample);
  }
}
Beispiel #3
0
// handle_pcdata() testet, ob im Receive-Buffer Paket-Daten (auch unvollst.) liegen
// und kopiert diese nach 'rxdatapaket' um.
// Bei g�ltigen Paketen, wird 'handle_pc_paket()'
// aufgerufen.
void handle_pcdata(void) {
  int rxbytes, burst_cnt = 5;
  do {
    rxbytes = data_received();
    if (rxbytes > 4) {					// a frame shout be have D0 length and crc
      U16 length = 0;
      if (data_look_byte(0) != FRAMESTARTID) {
	data_flushrx();		// destroy garbage on COM/USB
      } else {
	length = data_look_word(1);
	if (length <= (sizeof(rxdatapacket)-5)) {
	  if (length <= (rxbytes-5)) {
	    data_copyrx(rxdatapacket.data, length+5);
	    // data_flushrx();	// needed for buggy software, obsolete - using timeout
	  } else					// packet still in receiving (on slow serial)
	    length = 0;
	} else {
	  data_flushrx();				// incorrect packet
	  length = 0;				// remove all data from buffer
	} // fi incomplete
      } // esle
      if (length > 0) {
        // packet begins with a valid frame / FRAMESTARTID checked before
	U16 pkt_crc = 0;
	if (status_control&STA_CRCENABLE_MASK) {	// check CRC only, if needed.
	  pkt_crc = crc_ccitt(rxdatapacket.data, length+5);
	} // fi check CRC
	if (pkt_crc==0) handle_pc_paket(length);	// crc correct
      } // fi payload
    } // fi was da
  } while ( (rxbytes > 4) && ((--burst_cnt)>0) );
}
Beispiel #4
0
Datei: udp.c Projekt: maczpc/csf
static void 
udp_socket_event_handler(int fd, short event, void *arg)
{
	ssize_t			rc;
	CONN_STATE		*csp;	

    /* avoid compiler warning when WLOG_* is not defined */
    CSF_UNUSED_ARG(event);
    CSF_UNUSED_ARG(arg);

	csp = (CONN_STATE *)smalloc(sizeof(CONN_STATE));
	csp->ci.addrlen = sizeof(struct sockaddr);

	WLOG_DEBUG("udp_socket_event_handler() called fd: %d, event: %d, arg: %p",
			fd, event, arg);

	rc = recvfrom(fd, recv_data, RECV_DATA_LEN, 0,
				(struct sockaddr *) &(csp->ci.addr), &(csp->ci.addrlen));
	if (rc < 0) {
		WLOG_ERR("recvfrom() error: %s", strerror(errno));
		return;
	}
	csp->ci.type = CSF_UDP;
	csp->ci.fd = fd;
	csp->ci.cp_ops = &udp_ops;
	csp->ref_cnt = 1;

	connection_made(csp);

	data_received(csp, recv_data, rc);
	connection_lost(csp);

    /* reference count for free */
	CS_RELE(csp);
}
Beispiel #5
0
void
MulticastDataLink::sample_received(ReceivedDataSample& sample)
{
  switch (sample.header_.message_id_) {
  case TRANSPORT_CONTROL: {
    // Transport control samples are delivered to all sessions
    // regardless of association status:
    {
      char* const ptr = sample.sample_ ? sample.sample_->rd_ptr() : 0;

      ACE_GUARD(ACE_SYNCH_RECURSIVE_MUTEX,
          guard,
          this->session_lock_);

      const TransportHeader& theader = receive_strategy()->received_header();

      if (!is_active() && sample.header_.submessage_id_ == MULTICAST_SYN &&
          sessions_.find(theader.source_) == sessions_.end()) {
        // We have received a SYN but there is no session (yet) for this source.
        // Depending on the data, we may need to send SYNACK.

        guard.release();
        syn_received_no_session(theader.source_, sample.sample_,
            theader.swap_bytes());
        if (ptr) {
          sample.sample_->rd_ptr(ptr);
        }
        return;
      }

      MulticastSessionMap temp_sessions(sessions_);
      guard.release();

      for (MulticastSessionMap::iterator it(temp_sessions.begin());
          it != temp_sessions.end(); ++it) {
        it->second->control_received(sample.header_.submessage_id_,
            sample.sample_);
        // reset read pointer
        if (ptr) {
          sample.sample_->rd_ptr(ptr);
        }
      }
    }
  } break;

  case SAMPLE_ACK:
    ack_received(sample);
    break;

  default:
    if (!duplicate_data_sample(sample.header_)) {
      data_received(sample);
    }
    break;
  }
}
void process_ready_sockets(int client_socket[], int assoc_num, fd_set *rfds) {

        int i, stream, error;
	struct msghdr inmessage;
	struct iovec iov;
	char *big_buffer;
	char incmsg[CMSG_SPACE(sizeof (sctp_cmsg_data_t))];
	sockaddr_storage_t msgname;

	if ((big_buffer = malloc(REALLY_BIG)) == NULL) {
		printf("malloc failure: %s\n", strerror(errno));
		DUMP_CORE;
	}

        /* Setup inmessage to be able to receive in incomming message */
	memset(&inmessage, 0, sizeof (inmessage));
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen =1;
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof (incmsg);
	inmessage.msg_name = &msgname;
	inmessage.msg_namelen = sizeof (msgname);

	stream = 1;

	for( i = 0; i < assoc_num; i++) {
		if (FD_ISSET(client_socket[i], rfds)) {
				error = recvmsg(client_socket[i], &inmessage,
						MSG_WAITALL);
				if (error < 0)
						printf("Receive Failure: %s\n",
							strerror(errno));
				else {
		/* Test to find the type of message that was read(event/data) */
					if (inmessage.msg_flags &
						MSG_NOTIFICATION)
						 event_received(&inmessage,
								0);

					else
						data_received(&inmessage, error,
							stream,
							client_socket[i]);
			}
		}
	}
}
Beispiel #7
0
DecoderStack::DecoderStack(pv::SigSession &session,
	const srd_decoder *const dec) :
	_session(session),
	_sample_count(0),
	_frame_complete(false),
	_samples_decoded(0)
{
	connect(&_session, SIGNAL(frame_began()),
		this, SLOT(on_new_frame()));
	connect(&_session, SIGNAL(data_received()),
		this, SLOT(on_data_received()));
	connect(&_session, SIGNAL(frame_ended()),
		this, SLOT(on_frame_ended()));

	_stack.push_back(shared_ptr<decode::Decoder>(
		new decode::Decoder(dec)));
}
Beispiel #8
0
DecoderStack::DecoderStack(pv::Session &session,
	const srd_decoder *const dec) :
	session_(session),
	start_time_(0),
	samplerate_(0),
	sample_count_(0),
	frame_complete_(false),
	samples_decoded_(0)
{
	connect(&session_, SIGNAL(frame_began()),
		this, SLOT(on_new_frame()));
	connect(&session_, SIGNAL(data_received()),
		this, SLOT(on_data_received()));
	connect(&session_, SIGNAL(frame_ended()),
		this, SLOT(on_frame_ended()));

	stack_.push_back(shared_ptr<decode::Decoder>(
		new decode::Decoder(dec)));
}
Beispiel #9
0
/*
 * User-facing routine
 * Checks for new frames from PC and sends PC any new CAN bus frames
 * Will echo out any frames from PC that do not match the local node ID 
 * Should be called once per main loop iteration during normal operation
 */
void runSerialCAN(word id) {
    CANframe rxFrame;
    // Check for a new frame from the PC
    if (  readSerialCANframe(&rxFrame) == RX_COMPLETE ) {
        if ( rxFrame.id == id ) {
            CANput(rxFrame.payload);
        } else {
            CANsend(&rxFrame);  // Echo frame from serial onto CAN bus if ID doesn't match
        }
    }
    // Check for a recently sent frame
    if ( data_sent() ) {
        sendSerialCANframe(last_txframe());
    }
    // Check for a recently received frame
    if ( data_received() ) {
        sendSerialCANframe(last_rxframe());
    }
}
Beispiel #10
0
void SocketServer::start() {
	struct sockaddr_storage remoteaddr; // client address
	socklen_t addrlen;
	char remoteIP[INET6_ADDRSTRLEN];
//	sigset_t mask;
//	sigset_t orig_mask;

	// key = connection id
	std::map<int, SocketConnection*> connections;

//	/**
//	 * I'm not sure of this signal handling code, but for now it works:
//	 * I register a signal handler for the process, then in the handler I look if it's a SIGKILL for the thread, then
//	 * flag it as killed.
//	 * The sigmask code, I think, it's for handling correctly a signal while pselect is waiting, then exit from it
//	 */
//	struct sigaction act;
//
//	memset (&act, 0, sizeof(act));
//	act.sa_handler = signal_handler;
//
//	/* should shut down on SIGTERM. */
//	if (sigaction(SIGTERM, &act, 0)) {
//		perror ("sigaction");
//		throw std::exception();
//	}
//
//	sigemptyset (&mask);
//	sigaddset (&mask, SIGPIPE);
//
//	if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
//		perror ("sigprocmask");
//		throw std::exception();
//	}

	// main loop
	for(;;) {
        read_fds = master; // copy it
        if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
            perror("select");
            exit(4);
        }

        int i;
        // run through the existing connections looking for data to read
        for(i = 0; i <= fdmax; i++) {
            if (FD_ISSET(i, &read_fds)) { // we got one!!
            	if (i == listenfd) {
            		// handle new connection
            		addrlen = sizeof remoteaddr;
            		int connfd = accept(listenfd, (struct sockaddr *)&remoteaddr, &addrlen);

            		if (connfd == -1) {
            			perror("accept() error");
            		} else {
            			FD_SET(connfd, &master); // add to master set
						if (connfd > fdmax) {    // keep track of the max
							fdmax = connfd;
						}
						printf("new connection from %s on "
							"socket %d\n",
							inet_ntop(remoteaddr.ss_family,
								get_in_addr((struct sockaddr*)&remoteaddr),
								remoteIP, INET6_ADDRSTRLEN),
							connfd);
            		}

            	} else {
            		// handle data from a client

            		auto found = connections.find(i);
            		if (found == connections.end()) {
            			connections[i] = new SocketConnection(i);
            		}
            		SocketConnection *connection = connections[i];


            		try {
            		    int rcvd;

            			rcvd = connection->read(request_buffer, REQUEST_BUFFER_SIZE);

            			if (rcvd < 0)    // receive error
            			{
            				perror("recv() error");
            				throw std::exception();
            			}
            			else if (rcvd == 0)
            			{
            				fprintf(stderr,"Client disconnected unexpectedly.\n");
            				throw std::exception();
            			}

						if (data_received(connection, request_buffer, rcvd)) {
							connections.erase(i);
							delete(connection);

							FD_CLR(i, &master); // remove from master set
						}
            		} catch (std::exception &e) {
            			std::cerr << "exception handling request: " << e.what() << std::endl;
            			connections.erase(i);
            			connection_closed(connection);
            			delete(connection);
            			FD_CLR(i, &master); // remove from master set
            		}
            	}
            }
        }
	}
}
Beispiel #11
0
    void parcelhandler::register_counter_types()
    {
        HPX_STD_FUNCTION<boost::int64_t()> num_parcel_sends(
            boost::bind(&parcelport::get_parcel_send_count, &pp_));
        HPX_STD_FUNCTION<boost::int64_t()> num_parcel_receives(
            boost::bind(&parcelport::get_parcel_receive_count, &pp_));

        HPX_STD_FUNCTION<boost::int64_t()> num_message_sends(
            boost::bind(&parcelport::get_message_send_count, &pp_));
        HPX_STD_FUNCTION<boost::int64_t()> num_message_receives(
            boost::bind(&parcelport::get_message_receive_count, &pp_));

        HPX_STD_FUNCTION<boost::int64_t()> sending_time(
            boost::bind(&parcelport::get_sending_time, &pp_));
        HPX_STD_FUNCTION<boost::int64_t()> receiving_time(
            boost::bind(&parcelport::get_receiving_time, &pp_));

        HPX_STD_FUNCTION<boost::int64_t()> sending_serialization_time(
            boost::bind(&parcelport::get_sending_serialization_time, &pp_));
        HPX_STD_FUNCTION<boost::int64_t()> receiving_serialization_time(
            boost::bind(&parcelport::get_receiving_serialization_time, &pp_));

        HPX_STD_FUNCTION<boost::int64_t()> data_sent(
            boost::bind(&parcelport::get_data_sent, &pp_));
        HPX_STD_FUNCTION<boost::int64_t()> data_received(
            boost::bind(&parcelport::get_data_received, &pp_));

        HPX_STD_FUNCTION<boost::int64_t()> queue_length(
            boost::bind(&parcelhandler::get_queue_length, this));

        performance_counters::generic_counter_type_data const counter_types[] =
        {
            { "/parcels/count/sent", performance_counters::counter_raw,
              "returns the number of sent parcels for the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, num_parcel_sends, _2),
              &performance_counters::locality_counter_discoverer,
              ""
            },
            { "/parcels/count/received", performance_counters::counter_raw,
              "returns the number of received parcels for the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, num_parcel_receives, _2),
              &performance_counters::locality_counter_discoverer,
              ""
            },
            { "/messages/count/sent", performance_counters::counter_raw,
              "returns the number of sent messages for the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, num_message_sends, _2),
              &performance_counters::locality_counter_discoverer,
              ""
            },
            { "/messages/count/received", performance_counters::counter_raw,
              "returns the number of received messages for the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, num_message_receives, _2),
              &performance_counters::locality_counter_discoverer,
              ""
            },

            { "/time/data/sent", performance_counters::counter_raw,
              "returns the total time between the start of each asynchronous "
              "write and the invocation of the write callback for the referenced "
              "locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, sending_time, _2),
              &performance_counters::locality_counter_discoverer,
              "ns"
            },
            { "/time/data/received", performance_counters::counter_raw,
              "returns the total time between the start of each asynchronous "
              "read and the invocation of the read callback for the referenced "
              "locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, receiving_time, _2),
              &performance_counters::locality_counter_discoverer,
              "ns"
            },
            { "/time/serialize/sent", performance_counters::counter_raw,
              "returns the total time required to serialize all sent parcels "
              "for the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, sending_serialization_time, _2),
              &performance_counters::locality_counter_discoverer,
              "ns"
            },
            { "/time/serialize/received", performance_counters::counter_raw,
              "returns the total time required to de-serialize all received "
              "parcels for the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, receiving_serialization_time, _2),
              &performance_counters::locality_counter_discoverer,
              "ns"
            },

            { "/data/size/sent", performance_counters::counter_raw,
              "returns the amount of parcel data (including headers) sent "
              "by the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, data_sent, _2),
              &performance_counters::locality_counter_discoverer,
              "bytes"
            },
            { "/data/size/received", performance_counters::counter_raw,
              "returns the amount of parcel data (including headers) received "
              "by the referenced locality",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, data_received, _2),
              &performance_counters::locality_counter_discoverer,
              "bytes"
            },

            { "/parcelqueue/length", performance_counters::counter_raw,
              "returns the number current length of the queue of incoming threads",
              HPX_PERFORMANCE_COUNTER_V1,
              boost::bind(&performance_counters::locality_raw_counter_creator,
                  _1, queue_length, _2),
              &performance_counters::locality_counter_discoverer,
              ""
            }
        };
        performance_counters::install_counter_types(
            counter_types, sizeof(counter_types)/sizeof(counter_types[0]));
    }