Boolean RTPInterface::sendRTPorRTCPPacketOverTCP(u_int8_t* packet, unsigned packetSize,
int socketNum, unsigned char streamChannelId) {
#ifdef DEBUG_SEND
fprintf(stderr, "sendRTPorRTCPPacketOverTCP: %d bytes over channel %d (socket %d)\n",
packetSize, streamChannelId, socketNum); fflush(stderr);
#endif
// Send a RTP/RTCP packet over TCP, using the encoding defined in RFC 2326, section 10.12:
// $
// (If the initial "send()" of '$' succeeds, then we force the subsequent "send()" for
// the data to succeed, even if we have to do so with a blocking "send()".)
do {
u_int8_t framingHeader[4];
framingHeader[0] = '$';
framingHeader[1] = streamChannelId;
framingHeader[2] = (u_int8_t) ((packetSize&0xFF00)>>8);
framingHeader[3] = (u_int8_t) (packetSize&0xFF);
if (!sendDataOverTCP(socketNum, framingHeader, 4, False)) break;
if (!sendDataOverTCP(socketNum, packet, packetSize, True)) break;
#ifdef DEBUG_SEND
fprintf(stderr, "sendRTPorRTCPPacketOverTCP: completed\n"); fflush(stderr);
#endif
return True;
} while (0);
#ifdef DEBUG_SEND
fprintf(stderr, "sendRTPorRTCPPacketOverTCP: failed! (errno %d)\n", envir().getErrno()); fflush(stderr);
#endif
return False;
}
Boolean RTPInterface::sendDataOverTCP(int socketNum, u_int8_t const* data, unsigned dataSize, Boolean forceSendToSucceed) {
if (send(socketNum, (char const*)data, dataSize, 0/*flags*/) != (int)dataSize) {
// The TCP send() failed.
if (forceSendToSucceed && envir().getErrno() == EAGAIN) {
// The OS's TCP send buffer has filled up (because the stream's bitrate has exceeded the capacity of the TCP connection!).
// Force this data write to succeed, by blocking if necessary until it does:
#ifdef DEBUG_SEND
fprintf(stderr, "sendDataOverTCP: resending %d-byte send (blocking)\n", dataSize); fflush(stderr);
#endif
makeSocketBlocking(socketNum);
Boolean sendSuccess = send(socketNum, (char const*)data, dataSize, 0/*flags*/) == (int)dataSize;
makeSocketNonBlocking(socketNum);
return sendSuccess;
}
return False;
}
return True;
}
从上面代码看, 第一次没发完整的话第二次又会重新以阻塞方式全部发送一次, 这样会造成对方可能收到多余的字节, 或者导致对方收码流错乱?艹