示例#1
0
 static void insertString(String str, ByteBuffer buf) throws java.io.UnsupportedEncodingException {
   if (str != null && str.length() > 0) {
     buf.putInt(str.length() + 1);
     buf.put(str.getBytes("UTF-8"), 0, str.length());
     buf.put((byte) 0);
   } else { // buffer is null or length 0
     buf.putInt(0);
   }
 }
示例#2
0
 static void insertByteString(byte[] array, ByteBuffer buf)
     throws java.io.UnsupportedEncodingException {
   if (array != null && array.length > 0) {
     buf.putInt(array.length);
     buf.put(array, 0, array.length);
     buf.put((byte) 0);
   } else { // buffer is null or length 0
     buf.putInt(0);
   }
 }
示例#3
0
 // creates the header and pads the content returning a new byte[]
 // in the proper format. The secret is 4bytes (int), the step
 // is 2 bytes (short).
 public static byte[] createBuffer(byte[] content, int secret, short step) {
   short studentNum = 456; // my student number is 1340456
   int paddedSize = content.length;
   while (paddedSize % 4 != 0) {
     paddedSize++;
   }
   ByteBuffer bb = ByteBuffer.allocate(HEADER_SIZE + paddedSize);
   bb.putInt(content.length);
   bb.putInt(secret);
   bb.putShort(step);
   bb.putShort(studentNum);
   bb.put(content);
   return bb.array();
 }
  /**
   * Opens new WebRTC data channel using specified parameters.
   *
   * @param type channel type as defined in control protocol description. Use 0 for "reliable".
   * @param prio channel priority. The higher the number, the lower the priority.
   * @param reliab Reliability Parameter<br>
   *     This field is ignored if a reliable channel is used. If a partial reliable channel with
   *     limited number of retransmissions is used, this field specifies the number of
   *     retransmissions. If a partial reliable channel with limited lifetime is used, this field
   *     specifies the maximum lifetime in milliseconds. The following table summarizes this:<br>
   *     </br>
   *     <p>+------------------------------------------------+------------------+ | Channel Type |
   *     Reliability | | | Parameter |
   *     +------------------------------------------------+------------------+ |
   *     DATA_CHANNEL_RELIABLE | Ignored | | DATA_CHANNEL_RELIABLE_UNORDERED | Ignored | |
   *     DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT | Number of RTX | |
   *     DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT_UNORDERED | Number of RTX | |
   *     DATA_CHANNEL_PARTIAL_RELIABLE_TIMED | Lifetime in ms | |
   *     DATA_CHANNEL_PARTIAL_RELIABLE_TIMED_UNORDERED | Lifetime in ms |
   *     +------------------------------------------------+------------------+
   * @param sid SCTP stream id that will be used by new channel (it must not be already used).
   * @param label text label for the channel.
   * @return new instance of <tt>WebRtcDataStream</tt> that represents opened WebRTC data channel.
   * @throws IOException if IO error occurs.
   */
  public synchronized WebRtcDataStream openChannel(
      int type, int prio, long reliab, int sid, String label) throws IOException {
    if (channels.containsKey(sid)) {
      throw new IOException("Channel on sid: " + sid + " already exists");
    }

    // Label Length & Label
    byte[] labelBytes;
    int labelByteLength;

    if (label == null) {
      labelBytes = null;
      labelByteLength = 0;
    } else {
      labelBytes = label.getBytes("UTF-8");
      labelByteLength = labelBytes.length;
      if (labelByteLength > 0xFFFF) labelByteLength = 0xFFFF;
    }

    // Protocol Length & Protocol
    String protocol = WEBRTC_DATA_CHANNEL_PROTOCOL;
    byte[] protocolBytes;
    int protocolByteLength;

    if (protocol == null) {
      protocolBytes = null;
      protocolByteLength = 0;
    } else {
      protocolBytes = protocol.getBytes("UTF-8");
      protocolByteLength = protocolBytes.length;
      if (protocolByteLength > 0xFFFF) protocolByteLength = 0xFFFF;
    }

    ByteBuffer packet = ByteBuffer.allocate(12 + labelByteLength + protocolByteLength);

    // Message open new channel on current sid
    // Message Type
    packet.put((byte) MSG_OPEN_CHANNEL);
    // Channel Type
    packet.put((byte) type);
    // Priority
    packet.putShort((short) prio);
    // Reliability Parameter
    packet.putInt((int) reliab);
    // Label Length
    packet.putShort((short) labelByteLength);
    // Protocol Length
    packet.putShort((short) protocolByteLength);
    // Label
    if (labelByteLength != 0) {
      packet.put(labelBytes, 0, labelByteLength);
    }
    // Protocol
    if (protocolByteLength != 0) {
      packet.put(protocolBytes, 0, protocolByteLength);
    }

    int sentCount = sctpSocket.send(packet.array(), true, sid, WEB_RTC_PPID_CTRL);

    if (sentCount != packet.capacity()) {
      throw new IOException("Failed to open new chanel on sid: " + sid);
    }

    WebRtcDataStream channel = new WebRtcDataStream(sctpSocket, sid, label, false);

    channels.put(sid, channel);

    return channel;
  }
示例#5
0
 static void insertByteArray(byte[] array, ByteBuffer buf) {
   if (array != null && array.length > 0) {
     buf.putInt(array.length);
     buf.put(array, 0, array.length);
   } else buf.putInt(0);
 }
示例#6
0
 // returns byte array of content meant to be sent in packet b1
 public static byte[] partB(int packet_id, int len) {
   ByteBuffer bb = ByteBuffer.allocate(len + 4);
   bb.putInt(packet_id);
   return bb.array();
 }