Java Code Examples for io.netty.channel.socket.DatagramChannel#writeAndFlush()

The following examples show how to use io.netty.channel.socket.DatagramChannel#writeAndFlush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReliablePacketController.java    From riiablo with Apache License 2.0 6 votes vote down vote up
public void sendAck(int channelId, DatagramChannel ch) {
  if (DEBUG_SEND) Log.debug(TAG, "sendAck");

  int ack, ackBits;
  synchronized (receivedPackets) {
    ack = receivedPackets.generateAck();
    ackBits = receivedPackets.generateAckBits(ack);
  }

  ByteBuf packet = ch.alloc().directBuffer(config.packetHeaderSize);
  int headerSize = Packet.writeAck(packet, channelId, ack, ackBits);
  if (headerSize < 0) {
    Log.error(TAG, "failed to write ack");
    ReliableEndpoint.stats.NUM_ACKS_INVALID++;
    return;
  }

  channel.onPacketTransmitted(packet);
  ch.writeAndFlush(packet);
}
 
Example 2
Source File: ReliablePacketController.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public int sendPacket(int channelId, DatagramChannel ch, ByteBuf bb) {
    if (DEBUG_SEND) Log.debug(TAG, "sendPacket " + bb);

    final int packetSize = bb.readableBytes();
    if (packetSize > config.maxPacketSize) {
      Log.error(TAG, "packet is too large to send (%d bytes), max packet size is %d bytes", packetSize, config.maxPacketSize);
      ReliableEndpoint.stats.NUM_PACKETS_TOO_LARGE_TO_SEND++;
      return -1;
    }

    final int sequence = channel.incSequence();
    if (DEBUG_SEND) Log.debug(TAG, "packet sequence set to %d", sequence);

    int ack, ackBits;
    synchronized (receivedPackets) {
      ack = receivedPackets.generateAck();
      ackBits = receivedPackets.generateAckBits(ack);
    }

    SentPacketData sentPacketData = sentPackets.insert(sequence);
    sentPacketData.time = this.time;
    sentPacketData.packetSize = packetSize;
    sentPacketData.acked = false;

    if (packetSize <= config.fragmentThreshold) {
      // regular packet

      ByteBuf header = ch.alloc().buffer(config.packetHeaderSize);
      int headerSize = Packet.writePacketHeader(header, channelId, sequence, ack, ackBits);

      ByteBuf composite = ch.alloc().compositeBuffer(2)
          .addComponent(true, header)
          .addComponent(true, bb);

      channel.onPacketTransmitted(composite);
      ch.writeAndFlush(composite);
      return headerSize;
    } else {
      // fragmented packet

      throw new UnsupportedOperationException();
//      return -1;
    }
  }