⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dslongsockreceiver.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.sms.app;

import org.apache.log4j.Logger;
import java.io.InputStream;
import com.gctech.util.Tools;
import com.gctech.util.MD5;
import java.io.OutputStream;
import java.io.IOException;
import com.gctech.sms.util.ThreadManager;
import com.gctech.sms.msg.MTInfo;
import java.net.Socket;

import com.gctech.sms.msg.MTAck;
import java.io.*;
import com.gctech.sms.app.SubService;
import com.gctech.util.net.SocketService;
import java.util.Map;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: gctech</p>
 * @author 王红宝
 * @version $Id: DSLongSockReceiver.java,v 1.2 2004/05/14 02:30:44 wengjl Exp $
 */

public class DSLongSockReceiver extends SocketService{
  static Map services;
  //超时时间,3分钟
  static final int TIME_OUT = 3*60*1000;
  static final int SUCESS = 0;
  static final int ERROR_DATA_LENGTH = 1;
  static final int WRONG_CLIENT_IP = 2;
  static final int MD5_ERROR = 3;
  static final int SP_NOT_FOUND = 4;
  String spId = "";


  /**
   * 检测消息回复。
   * */
  void activeTestResp(byte[] activeTest) throws IOException {
    logger.debug("activeTest.length"+activeTest.length);
    byte[] activeResp = new byte[60];
    //消息类型
    Tools.short2byte(Constants.ACTIVE_RESP_MSG, activeResp, 32);
    //序列号
    System.arraycopy(activeTest, 4, activeResp, 4, 4);
    //互换source和dest
    System.arraycopy(activeTest, 14, activeResp, 18, 4);
    System.arraycopy(activeTest, 18, activeResp, 14, 4);
    send(activeResp);
  }
  void send(byte[] data) throws IOException {
    OutputStream out = sock.getOutputStream();
    out.write(data);
  }
  /**
   * 订阅消息。
   *
  void subscribe(byte[] head, byte[] subscribeReq){
    try {
      SubscribeTask task = new SubscribeTask(subscribeReq, sock, head);
      ThreadManager.getInstance().execute(task);
    }
    catch (InterruptedException ex) {
      logger.error(ex, ex);
    }
  }*/

  void doService() throws IOException {
    while( sock != null && sock.isConnected() ){
      //读消息头
      InputStream in = sock.getInputStream();
      final byte[] head = new byte[60];
      int headLen = in.read(head);
      logger.debug("before read msg!");
      if ( headLen != 60 ){
        logger.fatal("消息头错误:"+headLen);
        break;
      }
      //读取长度
      int dataLen = Tools.byte2unsignedShort(head, 8);
      //读取消息类型
      int cmdId = Tools.byte2unsignedShort(head, 32);
      if ( dataLen == 0 && cmdId == Constants.ACTIVE_MSG ){
        //检测消息
        activeTestResp(head);
      }else if ( dataLen > 0 && dataLen < 2048 ){
        //读取数据
        final byte[] resp = new byte[dataLen];
        int realDataLen = in.read(resp);
        logger.debug("收到消息长度是:" + realDataLen);
        if ( realDataLen != dataLen ){
          logger.fatal("消息体读取错误,消息长度:"+dataLen+",真实长度:"+realDataLen);
          throw new IOException("data length error:"+realDataLen);
        }
        logger.debug(new String(resp));
        try {
          final SubService service = (SubService) services.get(new Integer(cmdId));
          try {
            ThreadManager.getInstance().execute(new Runnable() {
              public void run() {
                byte[] rt = service.service(head, resp);
                try {
                  send(rt);
                }
                catch (IOException ex) {
                  ex.printStackTrace();
                }
              }
            });
          }
          catch (InterruptedException ex) {
            logger.error(ex, ex);
          }
        }
        catch (NullPointerException ex1) {
          logger.warn("can't found the cmdId:"+cmdId);
        }
      }else{
        throw new IOException("data length error:"+dataLen);
      }
    }
  }

  public DSLongSockReceiver() {

  }
  public void run(){
    String clientIp = "UNKNOWN";
    spId = "ABChina";
    try{
      sock.setSoTimeout(TIME_OUT);
      int status = SUCESS;
      clientIp = sock.getInetAddress().getHostAddress();
      logger.info( " Register "+spId+" from "+clientIp+"\t\t\t\t\t\t[OK]" );
      doService();
    } catch( Throwable e ){
      logger.error(e, e);
    } finally{
      if ( sock != null ){
        try{
          sock.close();
        }
        catch (Throwable ex) {
          logger.error(ex, ex);
        }
        sock = null;
      }
      //SpServer.getInstance().unregister(this);
      logger.info("释放从"+spId+":"+clientIp+"过来的连接 \t\t\t\t\t\t[成功]" ) ;
    }
  }

  static final Logger logger = Logger.getLogger(DSLongSockReceiver.class);
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -