Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void writeData(SelectionKey key) throws IOException {
- Packet outPacket = null;
- synchronized (pendingPacketQue) {
- for (Packet packet : pendingPacketQue) {
- if (packet.getChannel().keyFor(selector).equals(key)) {
- outPacket = packet;
- break;
- }
- }
- }
- if (outPacket == null) {
- Logger.writeException("Couldn't find out bound packet in list.", LogType.SERVER);
- return;
- }
- SocketChannel connection = (SocketChannel) outPacket.getChannel();
- ObjectOutputStream outStream = new ObjectOutputStream(connection.socket().getOutputStream());
- outStream.writeObject(outPacket);
- outStream.flush();
- outStream.close();
- connection.keyFor(selector).interestOps(SelectionKey.OP_READ);
- }
- private void readData(SelectionKey key) throws IOException, ClassNotFoundException {
- SocketChannel connection = (SocketChannel) key.channel();
- buffer.clear();
- int byteCount;
- try {
- byteCount = connection.read(buffer);
- } catch (IOException e) {
- Logger.writeException("Connenction terminated.", LogType.SERVER);
- connection.close();
- key.cancel();
- return;
- }
- if (byteCount == -1) {
- Logger.writeException("Connection error. Terminating connection.", LogType.SERVER);
- key.channel().close();
- key.cancel();
- return;
- }
- Engine.getInstance().getPacketProcessor().processData(connection, buffer.array(), byteCount);
- }
- public void processData(SocketChannel connection, byte[] data, int count)
- throws IOException, ClassNotFoundException {
- ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
- ObjectInputStream inStream = new ObjectInputStream(byteStream);
- addToQue(inStream.readObject());
- inStream.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement