summaryrefslogtreecommitdiff
path: root/NET/worlds/network/netPacket.java
blob: bc430a312a54eeb9be894b7247b4045c7005dafb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package NET.worlds.network;

import NET.worlds.console.StatNetMUNode;
import java.io.IOException;

public abstract class netPacket {
   protected ObjID _objID;
   protected int _commandType;

   public netPacket(ObjID id, int cmd) {
      if (id != null) {
         this._objID = id;
      } else {
         this._objID = new ObjID(1);
      }

      this._commandType = cmd;
   }

   public netPacket() {
      this._objID = new ObjID(1);
   }

   public int msgID() {
      return this._commandType;
   }

   int packetSize() {
      return 2 + this._objID.packetSize();
   }

   public String toString(WorldServer serv) {
      return new Integer(this._commandType).toString();
   }

   void send(ServerOutputStream o) throws IOException {
      int packetsize = this.packetSize();
      if (packetsize >= 256 && o.getVersion() <= 24) {
         throw new PacketTooLargeException();
      } else {
         StatNetMUNode netStat = StatNetMUNode.getNode();
         netStat.addBytesSent(packetsize);
         netStat.addPacketsSent(1);

         assert this._commandType > 0;

         if (packetsize >= 128 && o.getVersion() > 24) {
            o.writeByte(128 + packetsize / 256);
            o.writeByte(packetsize & 0xFF);
         } else {
            o.writeByte(packetsize);
         }

         this._objID.send(o);
         o.writeByte(this._commandType);
      }
   }
}