Advertisement
Guest User

Untitled

a guest
May 4th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. private void writeData(SelectionKey key) throws IOException {
  2. Packet outPacket = null;
  3. synchronized (pendingPacketQue) {
  4. for (Packet packet : pendingPacketQue) {
  5. if (packet.getChannel().keyFor(selector).equals(key)) {
  6. outPacket = packet;
  7. break;
  8. }
  9. }
  10. }
  11. if (outPacket == null) {
  12. Logger.writeException("Couldn't find out bound packet in list.", LogType.SERVER);
  13. return;
  14. }
  15. SocketChannel connection = (SocketChannel) outPacket.getChannel();
  16. ObjectOutputStream outStream = new ObjectOutputStream(connection.socket().getOutputStream());
  17. outStream.writeObject(outPacket);
  18. outStream.flush();
  19. outStream.close();
  20. connection.keyFor(selector).interestOps(SelectionKey.OP_READ);
  21. }
  22.  
  23. private void readData(SelectionKey key) throws IOException, ClassNotFoundException {
  24. SocketChannel connection = (SocketChannel) key.channel();
  25. buffer.clear();
  26. int byteCount;
  27. try {
  28. byteCount = connection.read(buffer);
  29. } catch (IOException e) {
  30. Logger.writeException("Connenction terminated.", LogType.SERVER);
  31. connection.close();
  32. key.cancel();
  33. return;
  34. }
  35. if (byteCount == -1) {
  36. Logger.writeException("Connection error. Terminating connection.", LogType.SERVER);
  37. key.channel().close();
  38. key.cancel();
  39. return;
  40. }
  41. Engine.getInstance().getPacketProcessor().processData(connection, buffer.array(), byteCount);
  42. }
  43.  
  44. public void processData(SocketChannel connection, byte[] data, int count)
  45. throws IOException, ClassNotFoundException {
  46. ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
  47. ObjectInputStream inStream = new ObjectInputStream(byteStream);
  48. addToQue(inStream.readObject());
  49. inStream.close();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement