コード例 #1
0
  private void processMsg(byte[] msgbyte) {
    // 得到主题
    HeaderItem hi = HeaderItem.fetchHeaderItem(msgbyte);
    String strtop = hi.getContentStr();
    // System.out.println("recved header="+strtop);
    if (!msgTopic.isMatch(strtop)) {
      return;
    }
    byte[] tmpbytes = HeaderItem.cutHeaderItem(msgbyte, hi);

    // 得到唯一id
    hi = HeaderItem.fetchHeaderItem(tmpbytes);
    String tmpid = hi.getContentStr();
    // System.out.println("recved id="+tmpid);
    Hashtable tmph = (Hashtable) idMsgBuf.get(tmpid);
    if (tmph == null) {
      tmph = new Hashtable();
      idMsgBuf.put(tmpid, tmph);
    }
    tmpbytes = HeaderItem.cutHeaderItem(tmpbytes, hi);

    // 得到顺序号
    hi = HeaderItem.fetchHeaderItem(tmpbytes);
    tmpbytes = HeaderItem.cutHeaderItem(tmpbytes, hi);
    String strord = hi.getContentStr();
    // System.out.println("recved order id="+strord);
    tmph.put(strord, tmpbytes);
    // System.out.println("recved ="+new String(tmpbytes));
    int d = strord.indexOf('_');
    int pknum = Integer.parseInt(strord.substring(0, d));
    if (tmph.size() == pknum) { // 一个主题的信息收齐,通知顶层
      int s = callBack.size();
      byte[] tmprecv = merge(tmph);
      // System.out.println ("Recving-----"+strtop+"["+tmprecv.length+"]") ;
      if (DEBUG) {
        log("UdpBase Recved<<----[" + strtop + "][" + tmprecv.length + "]");
      }

      for (int k = 0; k < s; k++) {
        ((UDPBaseCallback) callBack.elementAt(k)).OnMsg(this, strtop, tmprecv);
      }

      idMsgBuf.remove(tmpid);
    }
  }
コード例 #2
0
  /** 用广播方式发送一条消息 */
  public void send(String topic, byte[] infobuf)
      throws
          UDPBaseException { // System.out.println ("Sending-----["+topic+"]["+infobuf.length+"]") ;
    // log.log("Sending-----["+topic+"]["+infobuf.length+"]") ;
    if (bInitNull) {
      return;
    }

    if (DEBUG) {
      log("UdpBase Sending--->>[" + topic + "][" + infobuf.length + "]");
    }

    if (topic.length() > 150) {
      throw new UDPBaseException("Topic is too long!!");
    }
    if (infobuf == null || infobuf.length == 0) {
      throw new UDPBaseException("Info to be send cannot null!");
    }

    try {
      String tmpid = getOneID();
      // System.out.println ("ONE ID="+tmpid) ;
      int bs = infobuf.length / MAX_PACKET_LENGTH;
      int sy = infobuf.length % MAX_PACKET_LENGTH;
      int pknum = bs + (sy == 0 ? 0 : 1);
      DatagramPacket packet = null;

      HeaderItem topicHeader = new HeaderItem(topic);
      HeaderItem idHeader = new HeaderItem(tmpid);
      byte[] tmpb = null;

      for (int i = 0; i < bs; i++) {
        // if (i>0)//循环发送信息每次发送间加间隔,以提高发送成功率
        Thread.sleep(LOOP_SEND_INTERVAL);
        // 增加顺序头
        HeaderItem orderHeader = new HeaderItem("" + pknum + "_" + i);
        tmpb =
            HeaderItem.appendHeaderItem(
                infobuf, i * MAX_PACKET_LENGTH, MAX_PACKET_LENGTH, orderHeader);
        // 增加唯一id
        tmpb = HeaderItem.appendHeaderItem(tmpb, idHeader);
        // 增加主题头
        tmpb = HeaderItem.appendHeaderItem(tmpb, topicHeader);
        packet = new DatagramPacket(tmpb, 0, tmpb.length, group, RECV_PORT);
        // System.out.println("send package");
        sendSocket.send(packet);
      }

      if (sy > 0) {
        Thread.sleep(LOOP_SEND_INTERVAL);
        // 增加顺序头
        HeaderItem orderHeader = new HeaderItem("" + pknum + "_" + bs);
        tmpb = HeaderItem.appendHeaderItem(infobuf, bs * MAX_PACKET_LENGTH, sy, orderHeader);
        // 增加唯一id
        tmpb = HeaderItem.appendHeaderItem(tmpb, idHeader);
        // 增加主题头
        tmpb = HeaderItem.appendHeaderItem(tmpb, topicHeader);
        packet = new DatagramPacket(tmpb, 0, tmpb.length, group, RECV_PORT);

        // System.out.println("send.."+new String(tmpb));
        sendSocket.send(packet);
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new UDPBaseException("UDPBase send() error=\n" + e.toString());
    }
  }