1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
|
#include <sys/types.h>
#include <assert.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include "threads.h"
#include "fec.h"
#include "log.h"
#include "util.h"
#include "socklib.h"
#include "fifo.h"
#include "produconsum.h"
#include "udpcast.h"
#include "udp-sender.h"
#include "udpc-protoc.h"
#include "statistics.h"
#ifdef USE_SYSLOG
#include <syslog.h>
#endif
#define DEBUG 0
typedef struct slice {
int base; /* base address of slice in buffer */
int sliceNo;
int bytes; /* bytes in slice */
int nextBlock; /* index of next buffer to be transmitted */
volatile enum {
SLICE_FREE, /* free slice, and in the queue of free slices */
SLICE_NEW, /* newly allocated. FEC calculation and first
* transmission */
SLICE_XMITTED, /* transmitted */
SLICE_ACKED, /* acknowledged (if applicable) */
SLICE_PRE_FREE /* no longer used, but not returned to queue */
} state;
char rxmitMap[MAX_SLICE_SIZE / BITS_PER_CHAR];
/* blocks to be retransmitted */
char isXmittedMap[MAX_SLICE_SIZE / BITS_PER_CHAR];
/* blocks which have already been retransmitted during this round*/
int rxmitId; /* used to distinguish among several retransmission
* requests, so that we can easily discard answers to "old"
* requests */
/* This structure is used to keep track of clients who answered, and
* to make the reqack message
*/
struct reqackBm {
struct reqack ra;
char readySet[MAX_CLIENTS / BITS_PER_CHAR]; /* who is already ok? */
} sl_reqack;
char answeredSet[MAX_CLIENTS / BITS_PER_CHAR]; /* who answered at all? */
int nrReady; /* number of participants who are ready */
int nrAnswered; /* number of participants who answered; */
int needRxmit; /* does this need retransmission? */
int lastGoodBlock; /* last good block of slice (i.e. last block having not
* needed retransmission */
int lastReqack; /* last req ack sent (debug) */
#ifdef BB_FEATURE_UDPCAST_FEC
unsigned char *fec_data;
#endif
} *slice_t;
#define QUEUE_SIZE 256
struct returnChannel {
pthread_t thread; /* message receiving thread */
int rcvSock; /* socket on which we receive the messages */
produconsum_t incoming; /* where to enqueue incoming messages */
produconsum_t freeSpace; /* free space */
struct {
int clNo; /* client number */
union message msg; /* its message */
} q[QUEUE_SIZE];
struct net_config *config;
participantsDb_t participantsDb;
};
#define NR_SLICES 2
typedef struct senderState {
struct returnChannel rc;
struct fifo *fifo;
struct net_config *config;
sender_stats_t stats;
int socket;
struct slice slices[NR_SLICES];
produconsum_t free_slices_pc;
unsigned char *fec_data;
pthread_t fec_thread;
produconsum_t fec_data_pc;
} *sender_state_t;
static int getSliceBlocks(struct slice *slice, struct net_config *net_config)
{
return (slice->bytes + net_config->blockSize - 1) / net_config->blockSize;
}
static int isSliceAcked(struct slice *slice)
{
#if DEBUG
flprintf("Is slice %d acked? %d\n", slice->sliceNo,
slice->state);
#endif
if(slice->state == SLICE_ACKED) {
return 1;
} else {
return 0;
}
}
static int isSliceXmitted(struct slice *slice) {
if(slice->state == SLICE_XMITTED) {
return 1;
} else {
return 0;
}
}
static int freeSlice(sender_state_t sendst, struct slice *slice) {
#if DEBUG
int i;
i = slice - sendst->slices;
flprintf("Freeing slice %p %d %d\n", slice, slice->sliceNo, i);
#endif
slice->state = SLICE_PRE_FREE;
while(1) {
int pos = pc_getProducerPosition(sendst->free_slices_pc);
if(sendst->slices[pos].state == SLICE_PRE_FREE)
sendst->slices[pos].state = SLICE_FREE;
else
break;
pc_produce(sendst->free_slices_pc, 1);
}
return 0;
}
static struct slice *makeSlice(sender_state_t sendst, int sliceNo) {
struct net_config *config = sendst->config;
struct fifo *fifo = sendst->fifo;
int i;
struct slice *slice=NULL;
pc_consume(sendst->free_slices_pc, 1);
i = pc_getConsumerPosition(sendst->free_slices_pc);
slice = &sendst->slices[i];
assert(slice->state == SLICE_FREE);
BZERO(*slice);
pc_consumed(sendst->free_slices_pc, 1);
slice->base = pc_getConsumerPosition(sendst->fifo->data);
slice->sliceNo = sliceNo;
slice->bytes = pc_consume(fifo->data, 10*config->blockSize);
/* fixme: use current slice size here */
if(slice->bytes > config->blockSize * config->sliceSize)
slice->bytes = config->blockSize * config->sliceSize;
if(slice->bytes > config->blockSize)
slice->bytes -= slice->bytes % config->blockSize;
pc_consumed(fifo->data, slice->bytes);
slice->nextBlock = 0;
slice->state = SLICE_NEW;
#if 0
flprintf("Made slice %p %d\n", slice, slice->sliceNo);
#endif
BZERO(slice->sl_reqack.readySet);
slice->nrReady = 0;
#ifdef BB_FEATURE_UDPCAST_FEC
slice->fec_data = sendst->fec_data + (i * config->fec_stripes *
config->fec_redundancy *
config->blockSize);
#endif
return slice;
}
static int sendRawData(int sock,
struct net_config *config,
char *header, int headerSize,
unsigned char *data, int dataSize)
{
struct iovec iov[2];
struct msghdr hdr;
int packetSize;
int ret;
iov[0].iov_base = header;
iov[0].iov_len = headerSize;
iov[1].iov_base = data;
iov[1].iov_len = dataSize;
hdr.msg_name = &config->dataMcastAddr;
hdr.msg_namelen = sizeof(struct sockaddr_in);
hdr.msg_iov = iov;
hdr.msg_iovlen = 2;
initMsgHdr(&hdr);
packetSize = dataSize + headerSize;
rgWaitAll(config, sock, config->dataMcastAddr.sin_addr.s_addr, packetSize);
ret = sendmsg(sock, &hdr, 0);
if (ret < 0) {
char ipBuffer[16];
udpc_fatal(1, "Could not broadcast data packet to %s:%d (%s)\n",
getIpString(&config->dataMcastAddr, ipBuffer),
getPort(&config->dataMcastAddr),
strerror(errno));
}
return 0;
}
static int transmitDataBlock(sender_state_t sendst, struct slice *slice, int i)
{
struct fifo *fifo = sendst->fifo;
struct net_config *config = sendst->config;
struct dataBlock msg;
int size;
assert(i < MAX_SLICE_SIZE);
msg.opCode = htons(CMD_DATA);
msg.sliceNo = htonl(slice->sliceNo);
msg.blockNo = htons(i);
msg.reserved = 0;
msg.reserved2 = 0;
msg.bytes = htonl(slice->bytes);
size = slice->bytes - i * config->blockSize;
if(size < 0)
size = 0;
if(size > config->blockSize)
size = config->blockSize;
sendRawData(sendst->socket, config,
(char *) &msg, sizeof(msg),
fifo->dataBuffer +
(slice->base + i * config->blockSize) % fifo->dataBufSize,
size);
return 0;
}
#ifdef BB_FEATURE_UDPCAST_FEC
static int transmitFecBlock(sender_state_t sendst, struct slice *slice, int i)
{
struct net_config *config = sendst->config;
struct fecBlock msg;
/* Do not transmit zero byte FEC blocks if we are not in async mode */
if(slice->bytes == 0 && !(config->flags & FLAG_ASYNC))
return 0;
assert(i < config->fec_redundancy * config->fec_stripes);
msg.opCode = htons(CMD_FEC);
msg.stripes = htons(config->fec_stripes);
msg.sliceNo = htonl(slice->sliceNo);
msg.blockNo = htons(i);
msg.reserved2 = 0;
msg.bytes = htonl(slice->bytes);
sendRawData(sendst->socket, sendst->config,
(char *) &msg, sizeof(msg),
(slice->fec_data + i * config->blockSize), config->blockSize);
return 0;
}
#endif
static int sendSlice(sender_state_t sendst, struct slice *slice,
int retransmitting)
{
struct net_config *config = sendst->config;
int nrBlocks, i, rehello;
#ifdef BB_FEATURE_UDPCAST_FEC
int fecBlocks;
#endif
int retransmissions=0;
if(retransmitting) {
slice->nextBlock = 0;
if(slice->state != SLICE_XMITTED)
return 0;
} else {
if(slice->state != SLICE_NEW)
return 0;
}
nrBlocks = getSliceBlocks(slice, config);
#ifdef BB_FEATURE_UDPCAST_FEC
if((config->flags & FLAG_FEC) && !retransmitting) {
fecBlocks = config->fec_redundancy * config->fec_stripes;
} else {
fecBlocks = 0;
}
#endif
#if DEBUG
if(retransmitting) {
flprintf("%s slice %d from %d to %d (%d bytes) %d\n",
retransmitting ? "Retransmitting" : "Sending",
slice->sliceNo, slice->nextBlock, nrBlocks, slice->bytes,
config->blockSize);
}
#endif
if((sendst->config->flags & FLAG_STREAMING)) {
rehello = nrBlocks - sendst->config->rehelloOffset;
if(rehello < 0)
rehello = 0;
} else {
rehello = -1;
}
/* transmit the data */
for(i = slice->nextBlock; i < nrBlocks
#ifdef BB_FEATURE_UDPCAST_FEC
+ fecBlocks
#endif
; i++) {
if(retransmitting) {
if(!BIT_ISSET(i, slice->rxmitMap) ||
BIT_ISSET(i, slice->isXmittedMap)) {
/* if slice is not in retransmit list, or has _already_
* been retransmitted, skip it */
if(i > slice->lastGoodBlock)
slice->lastGoodBlock = i;
continue;
}
SET_BIT(i, slice->isXmittedMap);
retransmissions++;
#if DEBUG
flprintf("Retransmitting %d.%d\n", slice->sliceNo, i);
#endif
}
if(i == rehello) {
sendHello(sendst->config, sendst->socket, 1);
}
if(i < nrBlocks)
transmitDataBlock(sendst, slice, i);
#ifdef BB_FEATURE_UDPCAST_FEC
else
transmitFecBlock(sendst, slice, i - nrBlocks);
#endif
if(!retransmitting && pc_getWaiting(sendst->rc.incoming)) {
i++;
break;
}
}
if(retransmissions)
senderStatsAddRetransmissions(sendst->stats, retransmissions);
slice->nextBlock = i;
if(i == nrBlocks
#ifdef BB_FEATURE_UDPCAST_FEC
+ fecBlocks
#endif
) {
slice->needRxmit = 0;
if(!retransmitting)
slice->state = SLICE_XMITTED;
#if DEBUG
flprintf("Done: at block %d %d %d\n", i, retransmitting,
slice->state);
#endif
return 2;
}
#if DEBUG
flprintf("Done: at block %d %d %d\n", i, retransmitting,
slice->state);
#endif
return 1;
}
static int ackSlice(struct slice *slice, struct net_config *net_config,
struct fifo *fifo, sender_stats_t stats)
{
if(slice->state == SLICE_ACKED)
/* already acked */
return 0;
if(!(net_config->flags & FLAG_SN)) {
if(net_config->discovery == DSC_DOUBLING) {
net_config->sliceSize += net_config->sliceSize / 4;
if(net_config->sliceSize >= net_config->max_slice_size) {
net_config->sliceSize = net_config->max_slice_size;
net_config->discovery = DSC_REDUCING;
}
udpc_logprintf(udpc_log, "Doubling slice size to %d\n",
net_config->sliceSize);
}
}
slice->state = SLICE_ACKED;
pc_produce(fifo->freeMemQueue, slice->bytes);
/* Statistics */
senderStatsAddBytes(stats, slice->bytes);
if(slice->bytes) {
displaySenderStats(stats,
net_config->blockSize, net_config->sliceSize, 0);
}
/* End Statistics */
return 0;
}
static int sendReqack(struct slice *slice, struct net_config *net_config,
struct fifo *fifo, sender_stats_t stats,
int sock)
{
/* in async mode, just confirm slice... */
if((net_config->flags & FLAG_ASYNC) && slice->bytes != 0) {
ackSlice(slice, net_config, fifo, stats);
return 0;
}
if((net_config->flags & FLAG_ASYNC)
#ifdef BB_FEATURE_UDPCAST_FEC
&&
(net_config->flags & FLAG_FEC)
#endif
) {
return 0;
}
if(!(net_config->flags & FLAG_SN) && slice->rxmitId != 0) {
int nrBlocks;
nrBlocks = getSliceBlocks(slice, net_config);
#if DEBUG
flprintf("nrBlocks=%d lastGoodBlock=%d\n",
nrBlocks, slice->lastGoodBlock);
#endif
if(slice->lastGoodBlock != 0 && slice->lastGoodBlock < nrBlocks) {
net_config->discovery = DSC_REDUCING;
if (slice->lastGoodBlock < net_config->sliceSize / 2) {
net_config->sliceSize = net_config->sliceSize / 2;
} else {
net_config->sliceSize = slice->lastGoodBlock;
}
if(net_config->sliceSize < 32) {
/* a minimum of 32 */
net_config->sliceSize = 32;
}
udpc_logprintf(udpc_log, "Slice size=%d\n", net_config->sliceSize);
}
}
slice->lastGoodBlock = 0;
#if DEBUG
flprintf("Send reqack %d.%d\n", slice->sliceNo, slice->rxmitId);
#endif
slice->sl_reqack.ra.opCode = htons(CMD_REQACK);
slice->sl_reqack.ra.sliceNo = htonl(slice->sliceNo);
slice->sl_reqack.ra.bytes = htonl(slice->bytes);
slice->sl_reqack.ra.reserved = 0;
memcpy((void*)&slice->answeredSet,(void*)&slice->sl_reqack.readySet,
sizeof(slice->answeredSet));
slice->nrAnswered = slice->nrReady;
/* not everybody is ready yet */
slice->needRxmit = 0;
memset(slice->rxmitMap, 0, sizeof(slice->rxmitMap));
memset(slice->isXmittedMap, 0, sizeof(slice->isXmittedMap));
slice->sl_reqack.ra.rxmit = htonl(slice->rxmitId);
rgWaitAll(net_config, sock,
net_config->dataMcastAddr.sin_addr.s_addr,
sizeof(slice->sl_reqack));
#if DEBUG
flprintf("sending reqack for slice %d\n", slice->sliceNo);
#endif
BCAST_DATA(sock, slice->sl_reqack);
return 0;
}
/**
* mark slice as acknowledged, and work on slice size
*/
static int doRetransmissions(sender_state_t sendst,
struct slice *slice)
{
if(slice->state == SLICE_ACKED)
return 0; /* nothing to do */
#if DEBUG
flprintf("Do retransmissions\n");
#endif
/* FIXME: reduce slice size if needed */
if(slice->needRxmit) {
/* do some retransmissions */
sendSlice(sendst, slice, 1);
}
return 0;
}
static void markParticipantAnswered(slice_t slice, int clNo)
{
if(BIT_ISSET(clNo, slice->answeredSet))
/* client already has answered */
return;
slice->nrAnswered++;
SET_BIT(clNo, slice->answeredSet);
}
/**
* Handles ok message
*/
static int handleOk(sender_state_t sendst,
struct slice *slice,
int clNo)
{
if(slice == NULL)
return 0;
if(!udpc_isParticipantValid(sendst->rc.participantsDb, clNo)) {
udpc_flprintf("Invalid participant %d\n", clNo);
return 0;
}
if (BIT_ISSET(clNo, slice->sl_reqack.readySet)) {
/* client is already marked ready */
#if DEBUG
flprintf("client %d is already ready\n", clNo);
#endif
} else {
SET_BIT(clNo, slice->sl_reqack.readySet);
slice->nrReady++;
#if DEBUG
flprintf("client %d replied ok for %p %d ready=%d\n", clNo,
slice, slice->sliceNo, slice->nrReady);
#endif
senderSetAnswered(sendst->stats, clNo);
markParticipantAnswered(slice, clNo);
}
return 0;
}
static int handleRetransmit(sender_state_t sendst,
struct slice *slice,
int clNo, unsigned char *map, int rxmit)
{
unsigned int i;
#if DEBUG
flprintf("Handle retransmit %d @%d\n", slice->sliceNo, clNo);
#endif
if(!udpc_isParticipantValid(sendst->rc.participantsDb, clNo)) {
udpc_flprintf("Invalid participant %d\n", clNo);
return 0;
}
if(slice == NULL)
return 0;
if (rxmit < slice->rxmitId) {
#if 0
flprintf("Late answer\n");
#endif
/* late answer to previous Req Ack */
return 0;
}
#if DEBUG
logprintf(udpc_log,
"Received retransmit request for slice %d from client %d\n",
slice->sliceNo,clNo);
#endif
for(i=0; i <sizeof(slice->rxmitMap) / sizeof(char); i++) {
slice->rxmitMap[i] |= ~map[i];
}
slice->needRxmit = 1;
markParticipantAnswered(slice, clNo);
return 0;
}
static int handleDisconnect1(struct slice *slice, int clNo)
{
if(slice != NULL) {
if (BIT_ISSET(clNo, slice->sl_reqack.readySet)) {
/* avoid counting client both as left and ready */
CLR_BIT(clNo, slice->sl_reqack.readySet);
slice->nrReady--;
}
if (BIT_ISSET(clNo, slice->answeredSet)) {
slice->nrAnswered--;
CLR_BIT(clNo, slice->answeredSet);
}
}
return 0;
}
static int handleDisconnect(participantsDb_t db,
struct slice *slice1,
struct slice *slice2,
int clNo)
{
handleDisconnect1(slice1, clNo);
handleDisconnect1(slice2, clNo);
udpc_removeParticipant(db, clNo);
return 0;
}
static struct slice *findSlice(struct slice *slice1,
struct slice *slice2,
int sliceNo)
{
if(slice1 != NULL && slice1->sliceNo == sliceNo)
return slice1;
if(slice2 != NULL && slice2->sliceNo == sliceNo)
return slice2;
return NULL;
}
static int handleNextMessage(sender_state_t sendst,
struct slice *xmitSlice,
struct slice *rexmitSlice)
{
int pos = pc_getConsumerPosition(sendst->rc.incoming);
union message *msg = &sendst->rc.q[pos].msg;
int clNo = sendst->rc.q[pos].clNo;
#if DEBUG
flprintf("handle next message\n");
#endif
pc_consumeAny(sendst->rc.incoming);
switch(ntohs(msg->opCode)) {
case CMD_OK:
handleOk(sendst,
findSlice(xmitSlice, rexmitSlice, ntohl(msg->ok.sliceNo)),
clNo);
break;
case CMD_DISCONNECT:
handleDisconnect(sendst->rc.participantsDb,
xmitSlice, rexmitSlice, clNo);
break;
case CMD_RETRANSMIT:
#if DEBUG
flprintf("Received retransmittal request for %ld from %d:\n",
(long) xtohl(msg->retransmit.sliceNo), clNo);
#endif
handleRetransmit(sendst,
findSlice(xmitSlice, rexmitSlice,
ntohl(msg->retransmit.sliceNo)),
clNo,
msg->retransmit.map,
msg->retransmit.rxmit);
break;
default:
udpc_flprintf("Bad command %04x\n",
(unsigned short) msg->opCode);
break;
}
pc_consumed(sendst->rc.incoming, 1);
pc_produce(sendst->rc.freeSpace, 1);
return 0;
}
static THREAD_RETURN returnChannelMain(void *args) {
struct returnChannel *returnChannel = (struct returnChannel *) args;
while(1) {
struct sockaddr_in from;
int clNo;
int pos = pc_getConsumerPosition(returnChannel->freeSpace);
pc_consumeAny(returnChannel->freeSpace);
RECV(returnChannel->rcvSock,
returnChannel->q[pos].msg, from,
returnChannel->config->portBase);
clNo = udpc_lookupParticipant(returnChannel->participantsDb, &from);
if (clNo < 0) {
/* packet from unknown provenance */
continue;
}
returnChannel->q[pos].clNo = clNo;
pc_consumed(returnChannel->freeSpace, 1);
pc_produce(returnChannel->incoming, 1);
}
return 0;
}
static void initReturnChannel(struct returnChannel *returnChannel,
struct net_config *config,
int sock) {
returnChannel->config = config;
returnChannel->rcvSock = sock;
returnChannel->freeSpace = pc_makeProduconsum(QUEUE_SIZE,"msg:free-queue");
pc_produce(returnChannel->freeSpace, QUEUE_SIZE);
returnChannel->incoming = pc_makeProduconsum(QUEUE_SIZE,"msg:incoming");
pthread_create(&returnChannel->thread, NULL,
returnChannelMain, returnChannel);
}
static void cancelReturnChannel(struct returnChannel *returnChannel) {
/* No need to worry about the pthread_cond_wait in produconsum, because
* at the point where we enter here (to cancel the thread), we are sure
* that nobody else uses that produconsum any more
*/
pthread_cancel(returnChannel->thread);
pthread_join(returnChannel->thread, NULL);
}
static THREAD_RETURN netSenderMain(void *args0)
{
sender_state_t sendst = (sender_state_t) args0;
struct net_config *config = sendst->config;
struct timeval tv;
struct timespec ts;
int atEnd = 0;
int nrWaited=0;
unsigned long waitAverage=10000; /* Exponential average of last wait times */
struct slice *xmitSlice=NULL; /* slice being transmitted a first time */
struct slice *rexmitSlice=NULL; /* slice being re-transmitted */
int sliceNo = 0;
/* transmit the data */
if(config->default_slice_size == 0) {
#ifdef BB_FEATURE_UDPCAST_FEC
if(config->flags & FLAG_FEC) {
config->sliceSize =
config->fec_stripesize * config->fec_stripes;
} else
#endif
if(config->flags & FLAG_ASYNC)
config->sliceSize = 1024;
else if (sendst->config->flags & FLAG_SN) {
sendst->config->sliceSize = 112;
} else
sendst->config->sliceSize = 130;
sendst->config->discovery = DSC_DOUBLING;
} else {
config->sliceSize = config->default_slice_size;
#ifdef BB_FEATURE_UDPCAST_FEC
if((config->flags & FLAG_FEC) &&
(config->sliceSize > 128 * config->fec_stripes))
config->sliceSize = 128 * config->fec_stripes;
#endif
}
#ifdef BB_FEATURE_UDPCAST_FEC
if( (sendst->config->flags & FLAG_FEC) &&
config->max_slice_size > config->fec_stripes * 128)
config->max_slice_size = config->fec_stripes * 128;
#endif
if(config->sliceSize > config->max_slice_size)
config->sliceSize = config->max_slice_size;
assert(config->sliceSize <= MAX_SLICE_SIZE);
do {
/* first, cleanup rexmit Slice if needed */
if(rexmitSlice != NULL) {
if(rexmitSlice->nrReady ==
udpc_nrParticipants(sendst->rc.participantsDb)){
#if DEBUG
flprintf("slice is ready\n");
#endif
ackSlice(rexmitSlice, sendst->config, sendst->fifo,
sendst->stats);
}
if(isSliceAcked(rexmitSlice)) {
freeSlice(sendst, rexmitSlice);
rexmitSlice = NULL;
}
}
/* then shift xmit slice to rexmit slot, if possible */
if(rexmitSlice == NULL && xmitSlice != NULL &&
isSliceXmitted(xmitSlice)) {
rexmitSlice = xmitSlice;
xmitSlice = NULL;
sendReqack(rexmitSlice, sendst->config, sendst->fifo, sendst->stats,
sendst->socket);
}
/* handle any messages */
if(pc_getWaiting(sendst->rc.incoming)) {
#if DEBUG
flprintf("Before message %d\n",
pc_getWaiting(sendst->rc.incoming));
#endif
handleNextMessage(sendst, xmitSlice, rexmitSlice);
/* restart at beginning of loop: we may have acked the rxmit
* slice, makeing it possible to shift the pipe */
continue;
}
/* do any needed retransmissions */
if(rexmitSlice != NULL && rexmitSlice->needRxmit) {
doRetransmissions(sendst, rexmitSlice);
/* restart at beginning: new messages may have arrived during
* retransmission */
continue;
}
/* if all participants answered, send req ack */
if(rexmitSlice != NULL &&
rexmitSlice->nrAnswered ==
udpc_nrParticipants(sendst->rc.participantsDb)) {
rexmitSlice->rxmitId++;
sendReqack(rexmitSlice, sendst->config, sendst->fifo, sendst->stats,
sendst->socket);
}
if(xmitSlice == NULL && !atEnd) {
#if DEBUG
flprintf("SN=%d\n", sendst->config->flags & FLAG_SN);
#endif
if((sendst->config->flags & FLAG_SN) ||
rexmitSlice == NULL) {
#ifdef BB_FEATURE_UDPCAST_FEC
if(sendst->config->flags & FLAG_FEC) {
int i;
pc_consume(sendst->fec_data_pc, 1);
i = pc_getConsumerPosition(sendst->fec_data_pc);
xmitSlice = &sendst->slices[i];
pc_consumed(sendst->fec_data_pc, 1);
} else
#endif
{
xmitSlice = makeSlice(sendst, sliceNo++);
}
if(xmitSlice->bytes == 0)
atEnd = 1;
}
}
if(xmitSlice != NULL && xmitSlice->state == SLICE_NEW) {
sendSlice(sendst, xmitSlice, 0);
#if DEBUG
flprintf("%d Interrupted at %d/%d\n", xmitSlice->sliceNo,
xmitSlice->nextBlock,
getSliceBlocks(xmitSlice, sendst->config));
#endif
continue;
}
if(atEnd && rexmitSlice == NULL && xmitSlice == NULL)
break;
if(sendst->config->flags & FLAG_ASYNC)
break;
#if DEBUG
flprintf("Waiting for timeout...\n");
#endif
gettimeofday(&tv, 0);
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = (tv.tv_usec + 1.1*waitAverage) * 1000;
#ifdef WINDOWS
/* Windows has a granularity of 1 millisecond in its timer. Take this
* into account here */
#define GRANULARITY 1000000
ts.tv_nsec += 3*GRANULARITY/2;
ts.tv_nsec -= ts.tv_nsec % GRANULARITY;
#endif
#define BILLION 1000000000
while(ts.tv_nsec >= BILLION) {
ts.tv_nsec -= BILLION;
ts.tv_sec++;
}
if(rexmitSlice->rxmitId > 10)
/* after tenth retransmission, wait minimum one second */
ts.tv_sec++;
if(pc_consumeAnyWithTimeout(sendst->rc.incoming, &ts) != 0) {
#if DEBUG
flprintf("Have data\n");
#endif
{
struct timeval tv2;
unsigned long timeout;
gettimeofday(&tv2, 0);
timeout =
(tv2.tv_sec - tv.tv_sec) * 1000000+
tv2.tv_usec - tv.tv_usec;
if(nrWaited)
timeout += waitAverage;
waitAverage += 9; /* compensate against rounding errors */
waitAverage = (0.9 * waitAverage + 0.1 * timeout);
}
nrWaited = 0;
continue;
}
if(rexmitSlice == NULL) {
udpc_flprintf("Weird. Timeout and no rxmit slice");
break;
}
if(nrWaited > 5){
#ifndef WINDOWS
/* on Cygwin, we would get too many of those messages... */
udpc_flprintf("Timeout notAnswered=");
udpc_printNotSet(sendst->rc.participantsDb,
rexmitSlice->answeredSet);
udpc_flprintf(" notReady=");
udpc_printNotSet(sendst->rc.participantsDb, rexmitSlice->sl_reqack.readySet);
udpc_flprintf(" nrAns=%d nrRead=%d nrPart=%d avg=%ld\n",
rexmitSlice->nrAnswered,
rexmitSlice->nrReady,
udpc_nrParticipants(sendst->rc.participantsDb),
waitAverage);
nrWaited=0;
#endif
}
nrWaited++;
if(rexmitSlice->rxmitId > config->retriesUntilDrop) {
int i;
for(i=0; i < MAX_CLIENTS; i++) {
if(udpc_isParticipantValid(sendst->rc.participantsDb, i) &&
!BIT_ISSET(i, rexmitSlice->sl_reqack.readySet)) {
udpc_flprintf("Dropping client #%d because of timeout\n",
i);
#ifdef USE_SYSLOG
syslog(LOG_INFO, "dropped client #%d because of timeout",
i);
#endif
udpc_removeParticipant(sendst->rc.participantsDb, i);
if(nrParticipants(sendst->rc.participantsDb) == 0)
goto exit_main_loop;
}
}
continue;
}
rexmitSlice->rxmitId++;
sendReqack(rexmitSlice, sendst->config, sendst->fifo, sendst->stats,
sendst->socket);
} while(udpc_nrParticipants(sendst->rc.participantsDb)||
(config->flags & FLAG_ASYNC));
exit_main_loop:
cancelReturnChannel(&sendst->rc);
pc_produceEnd(sendst->fifo->freeMemQueue);
return 0;
}
#define ADR(x, bs) (fifo->dataBuffer + \
(slice->base+(x)*bs) % fifo->dataBufSize)
#ifdef BB_FEATURE_UDPCAST_FEC
static void fec_encode_all_stripes(sender_state_t sendst,
struct slice *slice)
{
int stripe;
struct net_config *config = sendst->config;
struct fifo *fifo = sendst->fifo;
int bytes = slice->bytes;
int stripes = config->fec_stripes;
int redundancy = config->fec_redundancy;
int nrBlocks = (bytes + config->blockSize - 1) / config->blockSize;
int leftOver = bytes % config->blockSize;
unsigned char *fec_data = slice->fec_data;
unsigned char *fec_blocks[redundancy];
unsigned char *data_blocks[128];
if(leftOver) {
unsigned char *lastBlock = ADR(nrBlocks - 1, config->blockSize);
memset(lastBlock+leftOver, 0, config->blockSize-leftOver);
}
for(stripe=0; stripe<stripes; stripe++) {
int i,j;
for(i=0; i<redundancy; i++)
fec_blocks[i] = fec_data+config->blockSize*(stripe+i*stripes);
for(i=stripe, j=0; i< nrBlocks; i+=stripes, j++)
data_blocks[j] = ADR(i, config->blockSize);
fec_encode(config->blockSize, data_blocks, j, fec_blocks, redundancy);
}
}
static THREAD_RETURN fecMain(void *args0)
{
sender_state_t sendst = (sender_state_t) args0;
slice_t slice;
int sliceNo = 0;
while(1) {
/* consume free slice */
slice = makeSlice(sendst, sliceNo++);
/* do the fec calculation here */
fec_encode_all_stripes(sendst,slice);
pc_produce(sendst->fec_data_pc, 1);
}
return 0;
}
#endif
int spawnNetSender(struct fifo *fifo,
int sock,
struct net_config *config,
participantsDb_t db,
sender_stats_t stats)
{
int i;
sender_state_t sendst = MALLOC(struct senderState);
sendst->fifo = fifo;
sendst->socket = sock;
sendst->config = config;
sendst->stats = stats;
#ifdef BB_FEATURE_UDPCAST_FEC
if(sendst->config->flags & FLAG_FEC)
sendst->fec_data = xmalloc(NR_SLICES *
config->fec_stripes *
config->fec_redundancy *
config->blockSize);
#endif
sendst->rc.participantsDb = db;
initReturnChannel(&sendst->rc, sendst->config, sendst->socket);
sendst->free_slices_pc = pc_makeProduconsum(NR_SLICES, "free slices");
pc_produce(sendst->free_slices_pc, NR_SLICES);
for(i = 0; i <NR_SLICES; i++)
sendst->slices[i].state = SLICE_FREE;
#ifdef BB_FEATURE_UDPCAST_FEC
if(sendst->config->flags & FLAG_FEC) {
/* Free memory queue is initially full */
fec_init();
sendst->fec_data_pc = pc_makeProduconsum(NR_SLICES, "fec data");
pthread_create(&sendst->fec_thread, NULL, fecMain, sendst);
}
#endif
pthread_create(&fifo->thread, NULL, netSenderMain, sendst);
return 0;
}
|