Skip to content

Commit

Permalink
1、 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wuw authored and wuw committed Dec 19, 2017
1 parent fe0474f commit edce7b0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
5 changes: 0 additions & 5 deletions Client/bin/bullet
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/bin/sh







# _ooOoo_
# o8888888o
# 88" . "88
Expand Down
5 changes: 4 additions & 1 deletion Client/src/main/java/com/wuweibi/bullet/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class Main {


public static void main(String[] args) throws URISyntaxException, IOException, DeploymentException {
public static void main(String[] args) throws URISyntaxException, IOException, DeploymentException, InterruptedException {


String url = ConfigUtils.getTunnel() +"/"+ ConfigUtils.getDeviceId();
Expand All @@ -36,6 +36,9 @@ public static void main(String[] args) throws URISyntaxException, IOException, D
Session session1 = container.connectToServer(Client.class, new URI(url)); // 连接会话


while (true){
Thread.sleep(3000000L);
}

}

Expand Down
31 changes: 28 additions & 3 deletions Client/src/main/java/com/wuweibi/bullet/client/SocketThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.wuweibi.bullet.protocol.Message;
import com.wuweibi.bullet.protocol.MsgHead;
import com.wuweibi.bullet.protocol.MsgProxyHttp;
import io.netty.buffer.ByteBuf;
import org.apache.commons.io.IOUtils;
import org.java_websocket.client.WebSocketClient;
import org.slf4j.Logger;
Expand Down Expand Up @@ -86,13 +87,21 @@ public void run() {
socketChannel.shutdownOutput();

} catch (IOException e) {
logger.error("", e);
// logger.error("", e);
msg.setContent(getMessage(e.getMessage()));
}
// 处理响应
logger.debug("接收响应内容...");
byte[] bytesout = SocketUtils.receiveData(socketChannel);
byte[] bytesout = new byte[0];
try {
bytesout = SocketUtils.receiveData(socketChannel);
msg.setContent(bytesout);
} catch (IOException e) {
msg.setContent(getMessage(e.getMessage()));

msg.setContent(bytesout);
} catch (NullPointerException e) {
msg.setContent(getMessage("server "+msg.getServerAddr()+":"+msg.getPort()+" invoke faild!"));
}
System.out.println("wlen=" + bytesout.length);

ByteArrayOutputStream os = new ByteArrayOutputStream();
Expand All @@ -108,4 +117,20 @@ public void run() {

}



/**
* 发送消息
*
* @param msg 消息内容
*/
private byte[] getMessage(String msg){
StringBuilder sb = new StringBuilder();
sb.append("HTTP/1.1 200 OK\n");
sb.append("Content-Type:text/html; charset:GBK");
sb.append("\n\n");
sb.append(msg);
return sb.toString().getBytes();
}

}
13 changes: 9 additions & 4 deletions Common/src/main/java/com/wuweibi/bullet/ConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

Expand All @@ -15,24 +17,27 @@
* @create 2017-12-06 下午9:43
**/
public class ConfigUtils {
/** 日志 */
private static Logger logger = LoggerFactory.getLogger(ConfigUtils.class);

static JSONObject CONF ;

static {
String result = "{}";
try {
InputStream inputStream = new FileInputStream("./conf/config.json");
File file = new File("./conf/config.json");
System.out.println(file.getAbsoluteFile());
InputStream inputStream = new FileInputStream(file);

// InputStream inputStream = ConfigUtils.class.getResourceAsStream("/config.json");



result = IOUtils.toString(inputStream, "UTF-8");
logger.debug("{}", result);
} catch (IOException e) {
e.printStackTrace();
logger.error("", e);
}


CONF = JSONObject.parseObject(result);
}

Expand Down
33 changes: 12 additions & 21 deletions Common/src/main/java/com/wuweibi/bullet/SocketUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,24 @@ public static byte[] getInputStreamBytes(Socket socket) throws IOException {
* @return
* @throws IOException
*/
public static byte[] receiveData(SocketChannel socketChannel) {
public static byte[] receiveData(SocketChannel socketChannel) throws IOException {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
byte[] bytes;
int size = 0;
int count = 0;
while ((size = socketChannel.read(buffer)) >= 0) {
buffer.flip();
bytes = new byte[size];
buffer.get(bytes);
baos.write(bytes);
buffer.clear();
count++;
}
} catch (IOException e) {
logger.error("", e);
} finally {
try {
// socketChannel.shutdownInput();
// socketChannel.close();

} catch (Exception ex) {
}
byte[] bytes;
int size = 0;
int count = 0;
while ((size = socketChannel.read(buffer)) >= 0) {
buffer.flip();
bytes = new byte[size];
buffer.get(bytes);
baos.write(bytes);
buffer.clear();
count++;
}

return baos.toByteArray();
}

Expand Down

0 comments on commit edce7b0

Please sign in to comment.