Example #1
0
  private final String readMSG(final int len)
      throws IOException, InterruptedException, MessagingNetworkException {
    if (len > 65000)
      ServerConnection.throwProtocolViolated("incoming message is too long: " + len + " bytes");
    byte[] b = new byte[len];
    InputStream is = getInputStream();
    synchronized (is) {
      long abortTime =
          System.currentTimeMillis() + 1000 * MSNMessagingNetwork.REQPARAM_SOCKET_TIMEOUT_SECONDS;
      int ofs = 0;

      while (ofs < len) {
        if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException();
        int read = is.read(b, ofs, len - ofs);
        if (read < 0) read = 0;
        ofs += read;
        if (System.currentTimeMillis() > abortTime) throw new IOException("connection timed out");
        /*
        if (len >= buffer.length)
        {
          ...
          return ...;
        }
        int pos = findCRLF();
        if (pos != -1) break;
        fill(is, abortTime);
        */
      }

      String msg = new String(b, 0, len, "UTF-8");
      return msg;
    }
  }
Example #2
0
  public final String readCommand(byte[] b)
      throws IOException, InterruptedException, MessagingNetworkException {
    InputStream is = getInputStream();
    synchronized (is) {
      long abortTime =
          System.currentTimeMillis() + 1000 * MSNMessagingNetwork.REQPARAM_SOCKET_TIMEOUT_SECONDS;
      int ofs = 0;
      boolean d = false;
      for (; ; ) {
        if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException();
        int by = is.read();
        if (by == -1) throw new IOException("unexpected EOF");
        if (by == 10 && d) break;
        d = (by == 13);
        if (ofs < b.length) {
          b[ofs++] = (byte) by;
        }
        if (System.currentTimeMillis() > abortTime) throw new IOException("connection timed out");
        /*
        if (len >= buffer.length)
        {
          ...
          return ...;
        }
        int pos = findCRLF();
        if (pos != -1) break;
        fill(is, abortTime);
        */
      }
      if (b[ofs - 1] == 13) --ofs;

      String line = new String(b, 0, ofs, "ASCII");

      if (StringUtil.startsWith(line, "MSG")) {
        StringTokenizer st = new StringTokenizer(line);
        String len_s = null;
        while (st.hasMoreTokens()) {
          len_s = st.nextToken();
        }
        if (len_s == null) throw new AssertException("len_s is null");
        int len;
        try {
          len = Integer.parseInt(len_s);
        } catch (NumberFormatException ex) {
          ServerConnection.throwProtocolViolated("MSG length must be int");
          len = 0;
        }
        String msg = readMSG(len);
        line = line + "\r\n" + msg;
      }
      if (Defines.DEBUG && CAT.isDebugEnabled()) CAT.debug("S: " + line);
      return line;
    }
  }
 // @Override
 public void makeTable() {
   serverCon.makeTable(userName);
 }
 // @Override
 public void leaveTable() {
   serverCon.leaveTable(userName);
 }
 // @Override
 public void joinTable(int tid) {
   serverCon.joinTable(userName, tid);
 }
 public void disconnect(boolean endProcess) {
   serverCon.disconnect(userName);
   if (endProcess) {
     System.exit(1);
   }
 }
 public void stopObserving(String user, int tid) {
   serverCon.stopObserving(user, tid);
 }
 public void observeTable(String user, int tid) {
   serverCon.observeTable(user, tid);
 }
 // @Override
 public void ready() {
   serverCon.playerReady(userName);
 }
 public void move(String user, int fr, int fc, int tr, int tc) {
   serverCon.move(user, fr, fc, tr, tc);
 }
 public void makeTable(String user) {
   serverCon.makeTable(user);
 }
 public void leaveTable(String user) {
   serverCon.leaveTable(user);
 }
 public void joinTable(String user, int tid) {
   serverCon.joinTable(user, tid);
 }
 /** Game playing methods * */
 public void getTblStatus(String user, int tid) {
   serverCon.getTblStatus(user, tid);
 }
 public void sendMsg_All(String msg) {
   serverCon.msgAll(userName, msg);
 }
 public void sendMsg(String to, String msg) {
   serverCon.msgPlayer(userName, to, msg);
 }
 public void playerReady(String user) {
   serverCon.playerReady(user);
 }
 // @Override
 public void move(int fr, int fc, int tr, int tc) {
   serverCon.move(userName, fr, fc, tr, tc);
 }