PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/GameServer/src/gameserver/network/chatserver/ChatServerConnection.java

http://aionxemu.googlecode.com/
Java | 177 lines | 91 code | 27 blank | 59 comment | 7 complexity | f8e880ce1f5f3ecc4f95d36bcfa9c024 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-2-Clause
  1. /**
  2. * This file is part of Aion X Emu <aionxemu.com>
  3. *
  4. * This is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser Public License
  15. * along with this software. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package gameserver.network.chatserver;
  18. import com.aionemu.commons.network.AConnection;
  19. import com.aionemu.commons.network.Dispatcher;
  20. import gameserver.network.chatserver.serverpackets.SM_CS_AUTH;
  21. import gameserver.utils.ThreadPoolManager;
  22. import org.apache.log4j.Logger;
  23. import java.io.IOException;
  24. import java.nio.ByteBuffer;
  25. import java.nio.channels.SocketChannel;
  26. import java.util.ArrayDeque;
  27. import java.util.Deque;
  28. /**
  29. * @author ATracer
  30. */
  31. public class ChatServerConnection extends AConnection {
  32. private static final Logger log = Logger.getLogger(ChatServerConnection.class);
  33. /**
  34. * Possible states of CsConnection
  35. */
  36. public static enum State {
  37. /**
  38. * chat server just connected
  39. */
  40. CONNECTED,
  41. /**
  42. * chat server is authenticated
  43. */
  44. AUTHED
  45. }
  46. /**
  47. * Server Packet "to send" Queue
  48. */
  49. private final Deque<CsServerPacket> sendMsgQueue = new ArrayDeque<CsServerPacket>();
  50. /**
  51. * Current state of this connection
  52. */
  53. private State state;
  54. private ChatServer chatServer;
  55. private CsPacketHandler csPacketHandler;
  56. /**
  57. * @param sc
  58. * @param d
  59. * @throws IOException
  60. */
  61. public ChatServerConnection(SocketChannel sc, Dispatcher d, CsPacketHandler csPacketHandler) throws IOException {
  62. super(sc, d);
  63. this.chatServer = ChatServer.getInstance();
  64. this.csPacketHandler = csPacketHandler;
  65. state = State.CONNECTED;
  66. log.info("Connected to ChatServer!");
  67. this.sendPacket(new SM_CS_AUTH());
  68. }
  69. @Override
  70. public boolean processData(ByteBuffer data) {
  71. CsClientPacket pck = csPacketHandler.handle(data, this);
  72. log.info("recived packet: " + pck);
  73. /**
  74. * Execute packet only if packet exist (!= null) and read was ok.
  75. */
  76. if (pck != null && pck.read())
  77. ThreadPoolManager.getInstance().executeLsPacket(pck);
  78. return true;
  79. }
  80. @Override
  81. protected final boolean writeData(ByteBuffer data) {
  82. synchronized (guard) {
  83. CsServerPacket packet = sendMsgQueue.pollFirst();
  84. if (packet == null)
  85. return false;
  86. packet.write(this, data);
  87. return true;
  88. }
  89. }
  90. @Override
  91. protected final long getDisconnectionDelay() {
  92. return 0;
  93. }
  94. @Override
  95. protected final void onDisconnect() {
  96. chatServer.chatServerDown();
  97. }
  98. @Override
  99. protected final void onServerClose() {
  100. // TODO send close packet to chat server
  101. close(/* packet, */true);
  102. }
  103. /**
  104. * @param bp
  105. */
  106. public final void sendPacket(CsServerPacket bp) {
  107. synchronized (guard) {
  108. /**
  109. * Connection is already closed or waiting for last (close packet) to be sent
  110. */
  111. if (isWriteDisabled())
  112. return;
  113. log.info("sending packet: " + bp);
  114. sendMsgQueue.addLast(bp);
  115. enableWriteInterest();
  116. }
  117. }
  118. /**
  119. * @param closePacket
  120. * @param forced
  121. */
  122. public final void close(CsServerPacket closePacket, boolean forced) {
  123. synchronized (guard) {
  124. if (isWriteDisabled())
  125. return;
  126. log.info("sending packet: " + closePacket + " and closing connection after that.");
  127. pendingClose = true;
  128. isForcedClosing = forced;
  129. sendMsgQueue.clear();
  130. sendMsgQueue.addLast(closePacket);
  131. enableWriteInterest();
  132. }
  133. }
  134. /**
  135. * @return
  136. */
  137. public State getState() {
  138. return state;
  139. }
  140. /**
  141. * @param state
  142. */
  143. public void setState(State state) {
  144. this.state = state;
  145. }
  146. @Override
  147. public String toString() {
  148. return "ChatServer " + getIP();
  149. }
  150. }