void Master::server_notify(pair <int, int> srv, bool extension) //notify server of server fail/chain extension
{
    Server *prev = Search_Prev_Server(srv, !extension);
    cal_next(srv.first);
    if (prev == NULL)
    {
        printf("Head server fails, the successor automatically becomes the new head\n");
        return;
    }
    else
    {
        
        //printf("previous server is %d\n", prev->GetserverName().second);
        socklen_t len = sizeof(sockaddr_in);
        char send_msg[MAXLINE];
        Server *next = Search_Next_Server(prev);
        if (next == NULL)
        {
            srv.second = -1;
        }
        else
        {
            srv.second = next->GetserverName().second;
        }
        packetize(send_msg, srv, false, extension);
        Sendto(sockfd_udp, send_msg, MAXLINE, 0, (SA*)&(prev->Getsockaddr()), len);
        if (extension)
            printf("New server extended to the tail of chain %d\n%s\n", srv.first, seperator);
        else if (srv.second == -1)
            printf("Tail server fails, server notification sent out to its precessor: 127.0.0.1:%d\n", prev->GetserverName().second);
        else
            printf("Internal server fails, server notification sent out to its precessor: 127.0.0.1:%d, new next server is 127.0.0.1:%d\n", prev->GetserverName().second, srv.second);
    }
}
/*
*
*	THIS FUNCTION WAS FUNCTIONALY REPLACED WITH playwav()
*
*/
int UDPsend(UDPinfo *UI, TCPinfo *TI)
{
	WSABUF filecontents;
	DWORD bytetoSend;
	DWORD flags = 0;
	FILE *fp;
	int byteSent = 0;
	int bufpos = 0;
	int numpackets;


	memset((void *)&UI->WH, 0, sizeof(wavheader));

	

	filecontents.buf = (char *) malloc (BUFSIZE);
	filecontents.buf = UI->WH.soundBuffer;
	filecontents.len = BUFSIZE;
	numpackets = (UI->WH.dataSize / BUFSIZE);
	bytetoSend = (numpackets * SOUND_DATA_SIZE)+(numpackets * 44);// do math for this ...soundbuffer/512 then add header to each of those

	TCPcontrolsend(TI, UI, UI->WH.size);

	while(bytetoSend > byteSent)
	{
		filecontents.buf = packetize(&UI->WH, &bufpos);
		byteSent += sendto(UI->sd, filecontents.buf, BUFSIZE, flags, (struct sockaddr *)&UI->server, UI->server_len); 
		
	}
	return 0;
}
Exemple #3
0
/*
 * Send a message via RTP using the connection information
 * given on UDP socket functions sendto() and recvfrom()
 */
int rtp_send_message(CONN_INFO *connection, MESSAGE* msg){
	/* ---- FIXME ----
	 * The goal of this function is to turn the message buffer
	 * into packets and then, using stop-n-wait RTP protocol,
	 * send the packets and re-send if the response is a NACK.
	 * If the response is an ACK, then you may send the next one
	 */
	 int* packetCount = malloc(sizeof(int));
	 PACKET* packets = packetize(msg->buffer, msg->length, packetCount);
	 int i =0;
	 do{
	    sendto(connection->socket, (void*)(packets+i), sizeof(PACKET), 0 , connection->remote_addr, connection->addrlen);
	    
	    PACKET* response = malloc(sizeof(PACKET));
	    recvfrom(connection->socket, (void*)response, sizeof(PACKET), 0, connection->remote_addr , &(connection->addrlen));
	    
	    if(response->type==ACK){
	      if(packets[i].type==LAST_DATA){
	         break;
	      }
	      i++;
	    }
	    free(response);
	 }while(i< *packetCount);
    
   free(packets);
	 return 0;   
	 
}
Exemple #4
0
static int packetize_annexb(uint64_t rtp_ts, const uint8_t *buf, size_t len,
			    size_t pktsize, videnc_packet_h *pkth, void *arg)
{
	const uint8_t *start = buf;
	const uint8_t *end   = buf + len;
	const uint8_t *r;
	int err = 0;

	r = h265_find_startcode(start, end);

	while (r < end) {
		const uint8_t *r1;
		bool marker;

		/* skip zeros */
		while (!*(r++))
			;

		r1 = h265_find_startcode(r, end);

		marker = (r1 >= end);

		err |= packetize(marker, r, r1-r, pktsize, rtp_ts, pkth, arg);

		r = r1;
	}

	return err;
}
Exemple #5
0
/*
 * Send a message via RTP using the connection information
 * given on UDP socket functions sendto() and recvfrom()
 */
int rtp_send_message(CONN_INFO *connection, MESSAGE*msg){
    /* ---- FIXME ----
     * The goal of this function is to turn the message buffer
     * into packets and then, using stop-n-wait RTP protocol,
     * send the packets and re-send if the response is a NACK.
     * If the response is an ACK, then you may send the next one
     */
    int count = 0;
    PACKET* packets = packetize(msg->buffer, msg->length, &count);
    for (int i = 0; i < count; i++) {
        sendto(connection->socket, (packets + i), sizeof(PACKET), 0, connection->remote_addr, connection->addrlen);
        int ack = 0;
        PACKET *response = (PACKET*)malloc(sizeof(PACKET));
        // Wait until an ACK is received, else resend the packet (Stop & Wait Implementation)
        while (ack == 0) {
            // Receive packet from connection
            recvfrom(connection->socket, response, sizeof(PACKET), 0, connection->remote_addr, &connection->addrlen);
            // Positive ACK packet
            if (response->type == ACK) {
                ack = 1;
            }
            // Negative ACK packet
            else if (response->type == NACK) {
                ack = 2;
                i -= 1;
            }
        }
    }
    return 1;
}
Exemple #6
0
unsigned char *get_hub_query_cmd(size_t byte_count, size_t *buf_size)
{
  unsigned char hub_query_cmd[] = {
    0x01, 0x85, 0xf0, 0x10, 0x8f, 0x30, 0x0e, 0x00, 0x60, 0x82, 0x10, 0xde, 0x01, 0x81, 0x30, 0x8f, 0x30,
    0x0c, 0x00, 0x60, 0x82, 0x10, 0x43, 0x00, 0x80, 0xa2, 0x00, 0x00, 0x81, 0x30, 0x82, 0x10, 0x43, 0x00,
    0x80, 0xa2, 0x00, 0x00, 0x81, 0x30, 0x82, 0x10, 0x43, 0x00, 0x80, 0xa2, 0x00, 0x00, 0x81, 0x30, 0x82,
    0x10, 0x43, 0x00, 0x80, 0xa2, 0x00, 0x00, 0x81, 0x30, 0x82, 0x10, 0x43, 0x00, 0x80, 0xa2, 0x00, 0x00,
    0x81, 0x30, 0x82, 0x10, 0x43, 0x00, 0x80, 0xa2, 0x00, 0x00, 0x81, 0x30, 0x82, 0x10, 0x43, 0x00, 0x80,
    0xa2, 0x00, 0x00, 0x81, 0x30, 0x82, 0x10, 0x43, 0x00, 0xc0, 0xa2, 0x00, 0x00, 0x81, 0x30,
  };

  size_t len = sizeof(hub_query_cmd) / sizeof(*hub_query_cmd);
  *buf_size = packet_size(len);
  return packetize(hub_query_cmd, len, 1);
}
Exemple #7
0
size_t bus_i2c::write_bytes(int addr, unsigned int size,
						uint8_t index, uint8_t* data){

	// Container
	i2c_bus_packet_t packet;

	/*
	 * Packetize
	 */
	packetize(size, index, data, &packet);

	/*
	 * Write and return the size written
	 */
	return write(addr, &packet);
}
Exemple #8
0
void Connection::onRecvCompleted(const asio::error_code& e, std::size_t bytes_transferred)
{
	if ( e )
	{
		m_socket.close();

		onError(e);

		return;
	}

	if ( bytes_transferred > 0 )
	{
		m_recvBuf.write( m_buffer.data(), bytes_transferred );	

		packetize();
	}

	requestRecv();
}
Exemple #9
0
/*
 * Send a message via RTP using the connection information
 * given on UDP socket functions sendto() and recvfrom()
 */
int rtp_send_message(CONN_INFO *connection, MESSAGE *msg) {
	/* ---- FIXME ----
	 * The goal of this function is to turn the message buffer
	 * into packets and then, using stop-n-wait RTP protocol,
	 * send the packets and re-send if the response is a NACK.
	 * If the response is an ACK, then you may send the next one
	*/
	int count = 0;
	int num = 0;
	PACKET* packet = packetize(msg->buffer, msg->length, &count);
	while (num < count) {
		sendto(connection->socket, &(packet[num]), sizeof(PACKET), 0, connection->remote_addr, connection->addrlen);
		PACKET* back = malloc(sizeof(PACKET));
		recvfrom(connection->socket, back, sizeof(PACKET), 0, connection->remote_addr, &connection->addrlen);
		if (back->type == ACK) {
			num++;
		}
	}
	return 0;
}
Exemple #10
0
int h265_encode(struct videnc_state *st, bool update,
		const struct vidframe *frame,
		videnc_packet_h *pkth, void *arg)
{
	x265_picture *pic_in = NULL, pic_out;
	x265_nal *nalv;
	uint32_t i, nalc = 0;
	int n, err = 0;

	if (!st || !frame || !pkth || frame->fmt != VID_FMT_YUV420P)
		return EINVAL;

	if (!st->x265 || !vidsz_cmp(&st->size, &frame->size)) {

		err = open_encoder(st, &frame->size);
		if (err)
			return err;

		st->size = frame->size;
	}

	if (update) {
		debug("h265: encode: picture update was requested\n");
	}

	pic_in = x265_picture_alloc();
	if (!pic_in) {
		warning("h265: x265_picture_alloc failed\n");
		return ENOMEM;
	}

	x265_picture_init(st->param, pic_in);

	pic_in->sliceType  = update ? X265_TYPE_IDR : X265_TYPE_AUTO;
	pic_in->pts        = ++st->pts;      /* XXX: add PTS to API */
	pic_in->colorSpace = X265_CSP_I420;

	for (i=0; i<3; i++) {
		pic_in->planes[i] = frame->data[i];
		pic_in->stride[i] = frame->linesize[i];
	}

	/* NOTE: important to get the PTS of the "out" picture */
	n = x265_encoder_encode(st->x265, &nalv, &nalc, pic_in, &pic_out);
	if (n <= 0)
		goto out;

	for (i=0; i<nalc; i++) {

		x265_nal *nal = &nalv[i];
		uint8_t *p = nal->payload;
		size_t len = nal->sizeBytes;
		bool marker;

#if 1
		debug("h265: encode: %s type=%2d  %s\n",
			  h265_is_keyframe(nal->type) ? "<KEY>" : "     ",
			  nal->type, h265_nalunit_name(nal->type));
#endif

		h265_skip_startcode(&p, &len);

		/* XXX: use pic_out.pts */

		marker = (i+1)==nalc;  /* last NAL */

		err = packetize(marker, p, len, st->pktsize, pkth, arg);
		if (err)
			goto out;
	}

 out:
	if (pic_in)
		x265_picture_free(pic_in);

	return err;
}
Exemple #11
0
int main(int argc, char* argv[])
{
	char* sender_ip;
	int sender_port;
	char* receiver_ip;
	int receiver_port;

	char* filename;
	FILE* file;
	int file_length;	// Length of file in bytes
	char* data;			// Each element is a byte from the file

	if(argc != 6)
	{
		printf("Usage: $./rdps sender_ip sender_port receiver_ip receiver_port sender_file_name\n");
		exit(0);
	}

	sender_ip = argv[1];
	sender_port = atoi(argv[2]);
	receiver_ip = argv[3];
	receiver_port = atoi(argv[4]);
	filename = argv[5];

	// Open file
	if((file = fopen(filename, "r")) == NULL)
	{
		perror("sender: open\n");
		exit(-1);
	}
	// determine file size
	fseek(file, 0, SEEK_END);
	file_length = ftell(file);
	rewind(file);
	printf("file length: %d\n",file_length);
	data = (char*)malloc(sizeof(data)*file_length);
	// Read file byte by byte into data buffer
	if(fread(data, 1, file_length, file) != file_length)
	{
		perror("sender: fread");
		free(data);
		exit(-1);
	}
	fclose(file);

	// Calculate payload size based on file length for files of size less than MAX_PAYLOAD_SIZE
	int payload_size = 0;
	int i;
	/*
	for(i=1; i<=MAX_PAYLOAD_SIZE; i++)
	{
		// found a factor
		if(file_length%i==0)
		{
			// if it's a greater factor
			if(i > payload_size)
				payload_size = i;
		}
	}
	*/
	payload_size = MAX_PAYLOAD_SIZE;
	printf("Payload size: %d\n",payload_size);
	// Calculate number of data packets to be stored
	float n = ((float)file_length) / ((float)payload_size);
	int num_packets = ceil(n);
	printf("Number of data packets: %d\n", num_packets);
	// Init globals
	data_packets = (struct packet**)calloc(num_packets+1, sizeof(struct packet*));
	// print contents of data_packets
	for(i=0; data_packets[i] == 0; i++)
	{
		//printf("data_packets[%d] is 0\n",i);
	}
	if(data_packets[num_packets] != 0){printf("data_packets[%d] is not 0\n",num_packets);}
	window_size = file_length/10;
	printf("window size: %d\n",window_size);

	setup_connection(sender_ip, sender_port, receiver_ip, receiver_port);
	packetize(data, file_length, payload_size);
	initiate_transfer(file_length+payload_size-1);
	return 0;
}