Ejemplo n.º 1
0
 private void initBuffers() {
   String pBufType = this.getProtocol().getPacketBufferType();
   Class<?> clazz = null;
   try {
     clazz = Class.forName("com.l2j.packetsamurai.session.packetbuffer." + pBufType);
     if (clazz == null) {
       PacketSamurai.getUserInterface()
           .log(
               "Wrong PacketBuffer type in "
                   + this.getProtocol().getFileName()
                   + " defaulting to DefaultPacketBuffer");
       _csPacketBuffer = new DefaultPacketBuffer();
       _scPacketBuffer = new DefaultPacketBuffer();
       return;
     }
     _csPacketBuffer = (PacketBuffer) clazz.newInstance();
     _scPacketBuffer = (PacketBuffer) clazz.newInstance();
   } catch (Exception e) {
     PacketSamurai.getUserInterface()
         .log(
             "Wrong PacketBuffer type in "
                 + this.getProtocol().getFileName()
                 + " defaulting to DefaultPacketBuffer");
     _csPacketBuffer = new DefaultPacketBuffer();
     _scPacketBuffer = new DefaultPacketBuffer();
   }
   _csPacketBuffer.setProtocol(this.getProtocol());
   _scPacketBuffer.setProtocol(this.getProtocol());
 }
Ejemplo n.º 2
0
  public void receivePacket(TCPPacket p, boolean fromServer, long time) {
    int size;
    if (fromServer) {
      // sequence packets from server
      if (p.data.length > 0) {
        _scTCPPacketBuffer.add(p);
      }

      // ack on the other side
      _csTCPPacketBuffer.ack(p);
      // process pos ack
      for (TCPPacket packet : _csTCPPacketBuffer.getSequencedPackets()) {
        _csPacketBuffer.addData(packet.data);
        while ((size = _csPacketBuffer.nextAvaliablePacket()) > 0) {
          byte[] header = new byte[2];
          byte[] packetData = new byte[size];
          _csPacketBuffer.getNextPacket(header, packetData);
          // System.out.println("TCPSession : receivePacket : New Full Packet (ClientPacket)
          // size="+size+" time="+time);
          this.addPacket(packetData, !fromServer, time);
        }
      }
      _csTCPPacketBuffer.flush();
    } else // from client
    {
      // sequence packets from server
      if (p.data.length > 0) {
        _csTCPPacketBuffer.add(p);
      }

      // ack on the other side
      _scTCPPacketBuffer.ack(p);
      // process pos ack
      for (TCPPacket packet : _scTCPPacketBuffer.getSequencedPackets()) {

        if (packet.data.length > 0) {
          _scPacketBuffer.addData(packet.data);
          while ((size = _scPacketBuffer.nextAvaliablePacket()) > 0) {
            byte[] header = new byte[2];
            byte[] packetData = new byte[size];
            _scPacketBuffer.getNextPacket(header, packetData);
            // System.out.println("TCPSession : receivePacket : New Full Packet (ServerPacket)
            // size="+size+" time="+time);
            this.addPacket(packetData, !fromServer, time);
          }
        }

        // Connection end?
        if (packet.fin || packet.rst) {
          PacketSamurai.getUserInterface()
              .log(
                  "[S -> C] TCP Sequencing Buffer Report: Pending: "
                      + _scTCPPacketBuffer.getPendingSequencePackets());
          PacketSamurai.getUserInterface()
              .log(
                  "[S -> C] TCP Data Buffer Report: Pending: "
                      + _scPacketBuffer.nextAvaliablePacket());
          PacketSamurai.getUserInterface()
              .log(
                  "[C -> S] TCP Sequencing Buffer Report: Pending: "
                      + _csTCPPacketBuffer.getPendingSequencePackets());
          PacketSamurai.getUserInterface()
              .log(
                  "[C -> S] TCP Data Buffer Report: Pending: "
                      + _csPacketBuffer.nextAvaliablePacket());
          PacketSamurai.getUserInterface()
              .log("Connection ended. (FIN: " + packet.fin + " - RST: " + packet.rst + ")");
          this.saveSession();
          this.close();
          return;
        }
      }
      _scTCPPacketBuffer.flush();
    }
  }