From b86eb130ef8a1b06a34454a47738e0a8ba9b50dd Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 7 Sep 2013 19:54:51 +0700 Subject: XMPPConnection.buildTextFrame; SocketSubscribed.sendByteBuffer, close --- src/com/juick/jabber/ws/SocketSubscribed.java | 24 +++++++ src/com/juick/jabber/ws/WSKeepAlive.java | 32 ++-------- src/com/juick/jabber/ws/XMPPConnection.java | 90 +++++++-------------------- 3 files changed, 49 insertions(+), 97 deletions(-) diff --git a/src/com/juick/jabber/ws/SocketSubscribed.java b/src/com/juick/jabber/ws/SocketSubscribed.java index f865ce6a..6144380c 100644 --- a/src/com/juick/jabber/ws/SocketSubscribed.java +++ b/src/com/juick/jabber/ws/SocketSubscribed.java @@ -1,5 +1,6 @@ package com.juick.jabber.ws; +import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; /** @@ -24,4 +25,27 @@ public class SocketSubscribed { this.VUID = VUID; tsConnected = tsLastData = System.currentTimeMillis(); } + + public boolean sendByteBuffer(ByteBuffer bb) { + boolean ret = false; + bb.rewind(); + try { + sock.write(bb); + ret = true; + } catch (Exception e) { + close(); + } + return ret; + } + + public void close() { + try { + sock.socket().close(); + } catch (Exception e) { + } + try { + sock.close(); + } catch (Exception e) { + } + } } diff --git a/src/com/juick/jabber/ws/WSKeepAlive.java b/src/com/juick/jabber/ws/WSKeepAlive.java index 2e36b100..2deef594 100644 --- a/src/com/juick/jabber/ws/WSKeepAlive.java +++ b/src/com/juick/jabber/ws/WSKeepAlive.java @@ -73,35 +73,11 @@ public class WSKeepAlive implements Runnable { } if (inactive > 180) { - closeBytes.rewind(); - try { - s.sock.write(closeBytes); - } catch (Exception e) { - } finally { - try { - s.sock.socket().close(); - } catch (Exception ex) { - } - try { - s.sock.close(); - } catch (Exception ex) { - } - i.remove(); - } + s.sendByteBuffer(closeBytes); + s.close(); + i.remove(); } else if (inactive > 60) { - pingBytes.rewind(); - try { - s.sock.write(pingBytes); - } catch (Exception e) { - System.err.println("WSKeepAlive ping: " + e); - try { - s.sock.socket().close(); - } catch (Exception ex) { - } - try { - s.sock.close(); - } catch (Exception ex) { - } + if (!s.sendByteBuffer(pingBytes)) { i.remove(); } } diff --git a/src/com/juick/jabber/ws/XMPPConnection.java b/src/com/juick/jabber/ws/XMPPConnection.java index a8af8c44..b85c8bee 100644 --- a/src/com/juick/jabber/ws/XMPPConnection.java +++ b/src/com/juick/jabber/ws/XMPPConnection.java @@ -81,34 +81,13 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. private void onJuickPM(int uid_to, com.juick.Message jmsg) { String json = com.juick.json.Message.toJSON(jmsg).toString(); - ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json); - ByteBuffer bbMsg = ByteBuffer.allocate(10240); - bbMsg.put((byte) 0x81); - if (jsonbytes.limit() <= 125) { - bbMsg.put((byte) jsonbytes.limit()); - } else { - bbMsg.put((byte) 126); - bbMsg.putShort((short) jsonbytes.limit()); - } - bbMsg.put(jsonbytes); - bbMsg.flip(); + ByteBuffer bbMsg = buildTextFrame(json); synchronized (Main.clients) { for (Iterator i = Main.clients.iterator(); i.hasNext();) { SocketSubscribed s = i.next(); if (s.VUID == uid_to) { - bbMsg.rewind(); - try { - s.sock.write(bbMsg); - } catch (Exception e) { - try { - s.sock.socket().close(); - } catch (Exception ex) { - } - try { - s.sock.close(); - } catch (Exception ex) { - } + if (!s.sendByteBuffer(bbMsg)) { i.remove(); } } @@ -118,17 +97,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. private void onJuickMessagePost(com.juick.Message jmsg) { String json = com.juick.json.Message.toJSON(jmsg).toString(); - ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json); - ByteBuffer bbMsg = ByteBuffer.allocate(10240); - bbMsg.put((byte) 0x81); - if (jsonbytes.limit() <= 125) { - bbMsg.put((byte) jsonbytes.limit()); - } else { - bbMsg.put((byte) 126); - bbMsg.putShort((short) jsonbytes.limit()); - } - bbMsg.put(jsonbytes); - bbMsg.flip(); + ByteBuffer bbMsg = buildTextFrame(json); ArrayList uids = new ArrayList(); String query = "SELECT suser_id FROM subscr_users WHERE user_id=" + jmsg.User.UID + " AND suser_id NOT IN (SELECT user_id FROM bl_tags INNER JOIN messages_tags USING(tag_id) WHERE message_id=" + jmsg.MID + ")"; @@ -154,18 +123,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. for (Iterator i = Main.clients.iterator(); i.hasNext();) { SocketSubscribed s = i.next(); if ((jmsg.Privacy >= 0 && (s.allMessages || s.UID == jmsg.User.UID)) || uids.contains(s.VUID)) { - bbMsg.rewind(); - try { - s.sock.write(bbMsg); - } catch (Exception e) { - try { - s.sock.socket().close(); - } catch (Exception ex) { - } - try { - s.sock.close(); - } catch (Exception ex) { - } + if (!s.sendByteBuffer(bbMsg)) { i.remove(); } } @@ -175,17 +133,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. private void onJuickMessageReply(com.juick.Message jmsg) { String json = com.juick.json.Message.toJSON(jmsg).toString(); - ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json); - ByteBuffer bbMsg = ByteBuffer.allocate(10240); - bbMsg.put((byte) 0x81); - if (jsonbytes.limit() <= 125) { - bbMsg.put((byte) jsonbytes.limit()); - } else { - bbMsg.put((byte) 126); - bbMsg.putShort((short) jsonbytes.limit()); - } - bbMsg.put(jsonbytes); - bbMsg.flip(); + ByteBuffer bbMsg = buildTextFrame(json); int privacy = MessagesQueries.getMessagePrivacy(sql, jmsg.MID); @@ -193,22 +141,26 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. for (Iterator i = Main.clients.iterator(); i.hasNext();) { SocketSubscribed s = i.next(); if ((privacy >= 0 && s.allReplies) || s.MID == jmsg.MID) { - bbMsg.rewind(); - try { - s.sock.write(bbMsg); - } catch (Exception e) { - try { - s.sock.socket().close(); - } catch (Exception ex) { - } - try { - s.sock.close(); - } catch (Exception ex) { - } + if (!s.sendByteBuffer(bbMsg)) { i.remove(); } } } } } + + private ByteBuffer buildTextFrame(String json) { + ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json); + ByteBuffer bbMsg = ByteBuffer.allocate(jsonbytes.limit() + 8); + bbMsg.put((byte) 0x81); + if (jsonbytes.limit() <= 125) { + bbMsg.put((byte) jsonbytes.limit()); + } else { + bbMsg.put((byte) 126); + bbMsg.putShort((short) jsonbytes.limit()); + } + bbMsg.put(jsonbytes); + bbMsg.flip(); + return bbMsg; + } } -- cgit v1.2.3