Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2015
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. /*
  2. * IDK Game Server by Steve Winfield
  3. * https://github.com/WinfieldSteve
  4. */
  5. package org.stevewinfield.suja.idk.communication;
  6.  
  7. import org.jboss.netty.buffer.ChannelBuffer;
  8. import org.jboss.netty.buffer.ChannelBuffers;
  9. import org.stevewinfield.suja.idk.encryption.Base64Encryption;
  10. import org.stevewinfield.suja.idk.encryption.WireEncryption;
  11.  
  12. import java.nio.charset.Charset;
  13.  
  14. public class MessageWriter {
  15. private short headerId;
  16. private ChannelBuffer body;
  17. private int senderId;
  18.  
  19. public Integer getSenderId() {
  20. return senderId;
  21. }
  22.  
  23. public short getId() {
  24. return headerId;
  25. }
  26.  
  27. public MessageWriter(final short outgoingOpCode) {
  28. this.initialize(outgoingOpCode);
  29. }
  30.  
  31. public void setSender(final int senderId) {
  32. this.senderId = senderId;
  33. }
  34.  
  35. public void initialize(final short outgoingOpCode) {
  36. this.headerId = outgoingOpCode;
  37. this.body = ChannelBuffers.dynamicBuffer();
  38. this.push(Base64Encryption.encode(this.headerId));
  39. }
  40.  
  41. public void push(final boolean s) {
  42. this.body.writeByte((byte) (s ? 'I' : 'H'));
  43. }
  44.  
  45. public void push(final byte[] x) {
  46. this.body.writeBytes(x);
  47. }
  48.  
  49. public void push(final int i) {
  50. this.push(WireEncryption.encode(i));
  51. }
  52.  
  53. public void push(final String s) {
  54. this.push(s.replace((char) 2, ' ').replace((char) 1, ' ').replace((char) 0, ' '), true);
  55. }
  56.  
  57. public void push(final String s, final boolean breaks) {
  58. this.push(s.getBytes());
  59. if (breaks) {
  60. this.body.writeByte((byte) 2);
  61. }
  62. }
  63.  
  64. public void push(final ISerialize x) {
  65. x.serialize(this);
  66. }
  67.  
  68. public String getDebugString() {
  69. final ChannelBuffer bodeh = body.duplicate();
  70. return bodeh.toString(Charset.defaultCharset());
  71. }
  72.  
  73. private ChannelBuffer cachedResult;
  74.  
  75. public ChannelBuffer getBytes() {
  76. if (cachedResult == null) {
  77. cachedResult = ChannelBuffers.copiedBuffer(this.body);
  78. cachedResult.writeByte(1);
  79. }
  80. return cachedResult;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement