PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/LoginServer/src/loginserver/network/gameserver/GsConnection.java

http://aionxemu.googlecode.com/
Java | 234 lines | 101 code | 29 blank | 104 comment | 12 complexity | c9e18a3b1f7d0ac9efe871c27b7a671a 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 loginserver.network.gameserver;
  18. import com.aionemu.commons.network.AConnection;
  19. import com.aionemu.commons.network.Dispatcher;
  20. import loginserver.GameServerInfo;
  21. import loginserver.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. * Object representing connection between LoginServer and GameServer.
  30. *
  31. * @author -Nemesiss-
  32. */
  33. public class GsConnection extends AConnection {
  34. /**
  35. * Logger for this class.
  36. */
  37. private static final Logger log = Logger.getLogger(GsConnection.class);
  38. /**
  39. * Possible states of GsConnection
  40. */
  41. public static enum State {
  42. /**
  43. * Means that GameServer just connect, but is not authenticated yet
  44. */
  45. CONNECTED,
  46. /**
  47. * GameServer is authenticated
  48. */
  49. AUTHED
  50. }
  51. /**
  52. * Server Packet "to send" Queue
  53. */
  54. private final Deque<GsServerPacket> sendMsgQueue = new ArrayDeque<GsServerPacket>();
  55. /**
  56. * Current state of this connection
  57. */
  58. private State state;
  59. /**
  60. * GameServerInfo for this GsConnection.
  61. */
  62. private GameServerInfo gameServerInfo = null;
  63. /**
  64. * Constructor.
  65. *
  66. * @param sc
  67. * @param d
  68. * @throws IOException
  69. */
  70. public GsConnection(SocketChannel sc, Dispatcher d) throws IOException {
  71. super(sc, d);
  72. state = State.CONNECTED;
  73. String ip = getIP();
  74. log.info("GS connection from: " + ip);
  75. }
  76. /**
  77. * Called by Dispatcher. ByteBuffer data contains one packet that should be processed.
  78. *
  79. * @param data
  80. * @return True if data was processed correctly, False if some error occurred and connection should be closed NOW.
  81. */
  82. @Override
  83. public boolean processData(ByteBuffer data) {
  84. GsClientPacket pck = GsPacketHandler.handle(data, this);
  85. log.info("recived packet: " + pck);
  86. if (pck != null && pck.read())
  87. ThreadPoolManager.getInstance().executeGsPacket(pck);
  88. return true;
  89. }
  90. /**
  91. * This method will be called by Dispatcher, and will be repeated till return false.
  92. *
  93. * @param data
  94. * @return True if data was written to buffer, False indicating that there are not any more data to write.
  95. */
  96. @Override
  97. protected boolean writeData(ByteBuffer data) {
  98. GsServerPacket packet = sendMsgQueue.pollFirst();
  99. if (packet == null)
  100. return false;
  101. packet.write(this, data);
  102. return true;
  103. }
  104. /**
  105. * This method is called by Dispatcher when connection is ready to be closed.
  106. *
  107. * @return time in ms after witch onDisconnect() method will be called. Always return 0.
  108. */
  109. @Override
  110. protected final long getDisconnectionDelay() {
  111. return 0;
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. @Override
  117. protected final void onDisconnect() {
  118. log.info(this + " disconnected");
  119. if (gameServerInfo != null) {
  120. gameServerInfo.setGsConnection(null);
  121. gameServerInfo.clearAccountsOnGameServer();
  122. gameServerInfo = null;
  123. }
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. @Override
  129. protected final void onServerClose() {
  130. // TODO mb some packet should be send to gameserver before closing?
  131. close(/* packet, */true);
  132. }
  133. /**
  134. * Sends GsServerPacket to this client.
  135. *
  136. * @param bp GsServerPacket to be sent.
  137. */
  138. public final void sendPacket(GsServerPacket bp) {
  139. /**
  140. * Connection is already closed or waiting for last (close packet) to be sent
  141. */
  142. if (isWriteDisabled())
  143. return;
  144. log.info("sending packet: " + bp);
  145. sendMsgQueue.addLast(bp);
  146. enableWriteInterest();
  147. }
  148. /**
  149. * Its guaranted that closePacket will be sent before closing connection, but all past and future packets wont.
  150. * Connection will be closed [by Dispatcher Thread], and onDisconnect() method will be called to clear all other
  151. * things. forced means that server shouldn't wait with removing this connection.
  152. *
  153. * @param closePacket Packet that will be send before closing.
  154. * @param forced have no effect in this implementation.
  155. */
  156. public final void close(GsServerPacket closePacket, boolean forced) {
  157. if (isWriteDisabled())
  158. return;
  159. log.info("sending packet: " + closePacket + " and closing connection after that.");
  160. pendingClose = true;
  161. isForcedClosing = forced;
  162. sendMsgQueue.clear();
  163. sendMsgQueue.addLast(closePacket);
  164. enableWriteInterest();
  165. }
  166. /**
  167. * @return Current state of this connection.
  168. */
  169. public State getState() {
  170. return state;
  171. }
  172. /**
  173. * @param state Set current state of this connection.
  174. */
  175. public void setState(State state) {
  176. this.state = state;
  177. }
  178. /**
  179. * @return GameServerInfo for this GsConnection or null if this GsConnection is not authenticated yet.
  180. */
  181. public GameServerInfo getGameServerInfo() {
  182. return gameServerInfo;
  183. }
  184. /**
  185. * @param gameServerInfo Set GameServerInfo for this GsConnection.
  186. */
  187. public void setGameServerInfo(GameServerInfo gameServerInfo) {
  188. this.gameServerInfo = gameServerInfo;
  189. }
  190. /**
  191. * @return String info about this connection
  192. */
  193. @Override
  194. public String toString() {
  195. StringBuilder sb = new StringBuilder();
  196. sb.append("GameServer [ID:");
  197. if (gameServerInfo != null) {
  198. sb.append(gameServerInfo.getId());
  199. } else {
  200. sb.append("null");
  201. }
  202. sb.append("] ").append(getIP());
  203. return sb.toString();
  204. }
  205. }