@Test
  public void exceptionInProtocolClosesClientSession() throws IOException {
    i = 0;
    final Protocol protocol =
        new Protocol() {
          public void init(ClientSession session) {}

          public void parseFirst(InputStream message) throws IOException {}

          public void parsePacket(InputStream message) throws IOException {
            i++;
            throw new IOException("Test");
          }
        };

    context.checking(
        new Expectations() {
          {
            oneOf(mockedClient).close();
          }
        });

    prot.setCurrentProtocol(protocol);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 2);
    CData.writeU16(out, 1337);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertEquals(1, i);

    context.assertIsSatisfied();
  }
  @Test
  public void receivePacketsSkipsUnusedData() throws IOException {
    i = 0;
    final Protocol protocol =
        new Protocol() {
          public void init(ClientSession session) {}

          public void parseFirst(InputStream message) throws IOException {}

          public void parsePacket(InputStream message) throws IOException {
            assertEquals(2, message.available());
            if (i == 1) {
              assertEquals(1338, CData.readU16(message));
            }
            i++;
          }
        };
    prot.setCurrentProtocol(protocol);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 2);
    CData.writeU16(out, 1337);
    CData.writeU16(out, 2);
    CData.writeU16(out, 1338);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertEquals(2, i);
  }
 @Test
 public void parseValidHeader() throws IOException {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   CData.writeU16(out, 2);
   CData.writeU16(out, 1337);
   InputStream in = prot.parseHeader(new ByteArrayInputStream(out.toByteArray()));
   assertEquals(2, in.available());
   assertEquals(1337, CData.readU16(in));
   assertTrue(in instanceof MessageInputStream);
 }
 @Test
 public void parseInvalidHeader() throws IOException {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   CData.writeU16(out, 2);
   InputStream in = prot.parseHeader(new ByteArrayInputStream(out.toByteArray()));
   assertNull(in);
 }
  @Test
  public void disconnectsClientUponUnknownProtocol() throws IOException {
    context.checking(
        new Expectations() {
          {
            oneOf(mockedClient).close();
          }
        });

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 1);
    CData.writeByte(out, 123);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    context.assertIsSatisfied();
  }
Example #6
0
 public void parse(PacketType type, InputStream message) throws IOException {
   int stanceId = CData.readByte(message);
   /*int chaseMode = */ CData.readByte(message);
   /*int safeMode = */ CData.readByte(message);
   Stance stance = Stance.BALANCED;
   switch (stanceId) {
     case 1:
       stance = Stance.OFFENSIVE;
       break;
     case 2:
       stance = Stance.BALANCED;
       break;
     case 3:
       stance = Stance.DEFENSIVE;
       break;
   }
   getPlayer().setStance(stance);
 }
  @Test
  public void changesToCorrectProtocol() throws IOException {
    final Protocol protocol = context.mock(Protocol.class);
    final ProtocolProvider provider = context.mock(ProtocolProvider.class);
    context.checking(
        new Expectations() {
          {
            allowing(provider).getProtocol(123);
            will(returnValue(protocol));
            oneOf(protocol).parseFirst(with(any(InputStream.class)));
            oneOf(protocol).init(mockedClient);
          }
        });
    prot.addProtocolProvider(provider);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 1);
    CData.writeByte(out, 123);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertSame(protocol, prot.getCurrentProtocol());
    context.assertIsSatisfied();
  }
Example #8
0
 public void write(OutputStream out) throws IOException {
   CData.writeByte(out, OPBYTE);
   CData.writeString(out, Integer.toString(number) + "\n" + message);
 }
 public void write(OutputStream out) throws IOException {
   CData.writeByte(out, 0xB4);
   CData.writeByte(out, type.getType());
   CData.writeString(out, message);
 }