From 47f51893e35823352aa4282b8a847c50d6c4ca0b Mon Sep 17 00:00:00 2001 From: "Mr.Huang" <1123033157@qq.com> Date: Thu, 23 Nov 2017 22:01:53 +0800 Subject: [PATCH] update --- .../java/com/dream/socket/DreamSocket.java | 18 +- .../java/com/dream/socket/DreamTCPSocket.java | 27 +- .../java/com/dream/socket/DreamUDPSocket.java | 22 +- .../codec/{DataProtocol.java => Message.java} | 2 +- .../com/dream/socket/codec/MessageDecode.java | 16 +- .../com/dream/socket/codec/MessageEncode.java | 2 +- .../com/dream/socket/codec/MessageHandle.java | 2 +- .../dream/socket/logger/DefaultLogger.java | 29 + .../java/com/dream/socket/logger/Logger.java | 15 + .../dream/socket/logger/LoggerFactory.java | 16 + .../dream/socket/runnable/HandleRunnable.java | 11 +- .../dream/socket/runnable/SendRunnable.java | 11 +- .../runnable/TCPSocketSendRunnable.java | 9 +- .../runnable/UDPSocketSendRunnable.java | 12 +- .../java/com/dream/socket/StringMessage.java | 19 + .../socket/{Client.java => TCPClient.java} | 63 +- src/test/java/com/dream/socket/TCPServer.java | 28 + src/test/java/com/dream/socket/UDPClient.java | 57 + .../com/dream/socket/protobuf/Protobuf.java | 2581 ----------------- .../java/com/dream/socket/protobuf/Type.java | 43 - .../com/dream/socket/protocol/Packet.java | 18 - .../com/dream/socket/protocol/Protocol.java | 45 - .../dream/socket/{ => test}/TestAddress.java | 2 +- .../socket/{ => test}/TestByteBuffer.java | 2 +- 24 files changed, 271 insertions(+), 2779 deletions(-) rename src/main/java/com/dream/socket/codec/{DataProtocol.java => Message.java} (73%) create mode 100644 src/main/java/com/dream/socket/logger/DefaultLogger.java create mode 100644 src/main/java/com/dream/socket/logger/Logger.java create mode 100644 src/main/java/com/dream/socket/logger/LoggerFactory.java create mode 100644 src/test/java/com/dream/socket/StringMessage.java rename src/test/java/com/dream/socket/{Client.java => TCPClient.java} (77%) create mode 100644 src/test/java/com/dream/socket/TCPServer.java create mode 100644 src/test/java/com/dream/socket/UDPClient.java delete mode 100644 src/test/java/com/dream/socket/protobuf/Protobuf.java delete mode 100644 src/test/java/com/dream/socket/protobuf/Type.java delete mode 100644 src/test/java/com/dream/socket/protocol/Packet.java delete mode 100644 src/test/java/com/dream/socket/protocol/Protocol.java rename src/test/java/com/dream/socket/{ => test}/TestAddress.java (93%) rename src/test/java/com/dream/socket/{ => test}/TestByteBuffer.java (98%) diff --git a/src/main/java/com/dream/socket/DreamSocket.java b/src/main/java/com/dream/socket/DreamSocket.java index 8d2c6d6..f43e070 100644 --- a/src/main/java/com/dream/socket/DreamSocket.java +++ b/src/main/java/com/dream/socket/DreamSocket.java @@ -1,5 +1,8 @@ package com.dream.socket; +import com.dream.socket.logger.Logger; +import com.dream.socket.logger.LoggerFactory; + import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -10,15 +13,18 @@ public abstract class DreamSocket { public final boolean start() { if (running) { - return false; + LoggerFactory.getLogger().info("Socket已连接!"); + return true; } if (pool != null) { if (!pool.isShutdown()) { + LoggerFactory.getLogger().warn("停止线程池"); pool.shutdownNow(); } pool = null; } pool = Executors.newFixedThreadPool(3); + LoggerFactory.getLogger().info("创建线程池"); pool.execute(new StartRunnable()); running = true; return true; @@ -26,12 +32,14 @@ public final boolean start() { public final boolean stop() { if (!running) { + LoggerFactory.getLogger().info("Socket已关闭!"); return true; } running = false; onStop(); if (pool != null && !pool.isShutdown()) { pool.shutdownNow(); + LoggerFactory.getLogger().warn("停止线程池"); } return true; } @@ -54,11 +62,15 @@ public boolean isRunning() { return this.running; } - protected boolean executeRunnable(Runnable runnable){ - if(pool != null && !pool.isShutdown()){ + protected boolean executeRunnable(Runnable runnable) { + if (pool != null && !pool.isShutdown()) { pool.execute(runnable); return true; } return false; } + + public void setLogger(Logger logger) { + LoggerFactory.setLogger(logger); + } } diff --git a/src/main/java/com/dream/socket/DreamTCPSocket.java b/src/main/java/com/dream/socket/DreamTCPSocket.java index 3be23d4..de36c5e 100644 --- a/src/main/java/com/dream/socket/DreamTCPSocket.java +++ b/src/main/java/com/dream/socket/DreamTCPSocket.java @@ -1,9 +1,10 @@ package com.dream.socket; -import com.dream.socket.codec.DataProtocol; +import com.dream.socket.codec.Message; import com.dream.socket.codec.MessageDecode; import com.dream.socket.codec.MessageEncode; import com.dream.socket.codec.MessageHandle; +import com.dream.socket.logger.LoggerFactory; import com.dream.socket.runnable.HandleRunnable; import com.dream.socket.runnable.TCPSocketSendRunnable; @@ -29,7 +30,7 @@ public DreamTCPSocket(String host, int port) { this.mPort = port; } - public void codec(MessageDecode messageDecode, MessageHandle messageHandle, MessageEncode messageEncode) { + public void codec(MessageDecode messageDecode, MessageHandle messageHandle, MessageEncode messageEncode) { this.mHandleRunnable = new HandleRunnable<>(messageHandle); messageDecode.setHandleRunnable(mHandleRunnable); this.mMessageDecode = messageDecode; @@ -45,9 +46,10 @@ protected boolean onStart() { mAddress = new InetSocketAddress(mHost, mPort); } mSocket = new Socket(); + LoggerFactory.getLogger().info("开始连接 -> " + mAddress.toString()); mSocket.connect(mAddress, SOCKET_CONNECT_TIMEOUT); if (mSocket.isConnected()) { - System.out.println("连接成功"); + LoggerFactory.getLogger().info("连接成功"); mSocketSendRunnable.setOutputStream(mSocket.getOutputStream()); executeRunnable(mSocketSendRunnable); executeRunnable(mHandleRunnable); @@ -55,22 +57,24 @@ protected boolean onStart() { InputStream in = mSocket.getInputStream(); int length; while ((length = in.read(bytes)) > 0) { - this.mMessageDecode.put(mAddress, bytes, 0, length); + this.mMessageDecode.decode(mAddress, bytes, 0, length); } + } else { + LoggerFactory.getLogger().error("连接失败!"); } } catch (Exception e) { - e.printStackTrace(); + LoggerFactory.getLogger().error("重连发生异常", e); } finally { try { mSocket = null; if (isRunning()) { this.mHandleRunnable.status(Status.STATUS_FAIL); - System.out.println("6秒后尝试重连"); + LoggerFactory.getLogger().info("6秒后尝试重连"); this.wait(6000); - System.out.println("开始重连"); + LoggerFactory.getLogger().info("开始重连 -> " + mAddress.toString()); } } catch (Exception ie) { - System.out.println("重连发生异常"); + LoggerFactory.getLogger().error("重连发生异常", ie); } } } @@ -78,7 +82,7 @@ protected boolean onStart() { return false; } - public boolean send(T data) { + public boolean send(T data) { if (mSocketSendRunnable != null) { data.mAddress = mAddress; return mSocketSendRunnable.send(data); @@ -89,9 +93,11 @@ public boolean send(T data) { @Override protected boolean onStop() { if (mSocketSendRunnable != null) { + LoggerFactory.getLogger().info("开始结束发送线程..."); mSocketSendRunnable.stop(); } if (this.mHandleRunnable != null) { + LoggerFactory.getLogger().info("开始结束接收线程..."); this.mHandleRunnable.stop(); } if (mSocket != null) { @@ -115,6 +121,7 @@ public boolean isConnected() { private static void shutdownInput(Socket socket) { if (socket != null && !socket.isInputShutdown()) { try { + LoggerFactory.getLogger().info("关闭Socket输入..."); socket.shutdownInput(); } catch (IOException e) { e.printStackTrace(); @@ -125,6 +132,7 @@ private static void shutdownInput(Socket socket) { private static void shutdownOutput(Socket socket) { if (socket != null && !socket.isOutputShutdown()) { try { + LoggerFactory.getLogger().info("关闭Socket输出..."); socket.shutdownOutput(); } catch (IOException e) { e.printStackTrace(); @@ -135,6 +143,7 @@ private static void shutdownOutput(Socket socket) { private static void close(Socket socket) { if (socket != null && !socket.isClosed()) { try { + LoggerFactory.getLogger().info("关闭Socket..."); socket.close(); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/dream/socket/DreamUDPSocket.java b/src/main/java/com/dream/socket/DreamUDPSocket.java index a825568..d9c9ecd 100644 --- a/src/main/java/com/dream/socket/DreamUDPSocket.java +++ b/src/main/java/com/dream/socket/DreamUDPSocket.java @@ -1,9 +1,10 @@ package com.dream.socket; -import com.dream.socket.codec.DataProtocol; +import com.dream.socket.codec.Message; import com.dream.socket.codec.MessageDecode; import com.dream.socket.codec.MessageEncode; import com.dream.socket.codec.MessageHandle; +import com.dream.socket.logger.LoggerFactory; import com.dream.socket.runnable.HandleRunnable; import com.dream.socket.runnable.UDPSocketSendRunnable; @@ -19,7 +20,7 @@ public class DreamUDPSocket extends DreamSocket { private MessageDecode mMessageDecode; private HandleRunnable mHandleRunnable; - public void codec(MessageDecode messageDecode, MessageHandle messageHandle, MessageEncode messageEncode) { + public void codec(MessageDecode messageDecode, MessageHandle messageHandle, MessageEncode messageEncode) { this.mHandleRunnable = new HandleRunnable<>(messageHandle); messageDecode.setHandleRunnable(mHandleRunnable); this.mMessageDecode = messageDecode; @@ -30,9 +31,10 @@ public void codec(MessageDecode messageDecode, Messa public boolean onStart() { synchronized (this) { try { + LoggerFactory.getLogger().info("开始创建UDP管道"); mSocket = new DatagramSocket(); } catch (Exception e) { - e.printStackTrace(); + LoggerFactory.getLogger().error("UDP管道创建失败!", e); return false; } mSocketSendRunnable.setDatagramSocket(mSocket); @@ -44,21 +46,20 @@ public boolean onStart() { while (isRunning()) { packet.setData(buffer, 0, buffer.length); mSocket.receive(packet); - mMessageDecode.put(packet.getSocketAddress(), packet.getData(), packet.getOffset(), packet.getLength()); + mMessageDecode.decode(packet.getSocketAddress(), packet.getData(), packet.getOffset(), packet.getLength()); } } catch (Exception e) { - e.printStackTrace(); + LoggerFactory.getLogger().error("UDP receive执行异常!", e); } finally { try { mSocket = null; if (isRunning()) { this.mHandleRunnable.status(Status.STATUS_FAIL); - System.out.println("6秒后尝试重连"); + LoggerFactory.getLogger().info("6秒后重新创建UDP管道"); this.wait(6000); - System.out.println("开始重连"); } } catch (Exception ie) { - System.out.println("重连发生异常"); + LoggerFactory.getLogger().error("wait奔溃!", ie); } } @@ -69,12 +70,15 @@ public boolean onStart() { @Override public boolean onStop() { if (mSocketSendRunnable != null) { + LoggerFactory.getLogger().info("开始结束发送线程..."); mSocketSendRunnable.stop(); } if (this.mHandleRunnable != null) { + LoggerFactory.getLogger().info("开始结束接收线程..."); this.mHandleRunnable.stop(); } if (mSocket != null) { + LoggerFactory.getLogger().info("关闭UDP管道"); mSocket.close(); mSocket = null; mHandleRunnable.status(Status.STATUS_DISCONNECT); @@ -90,7 +94,7 @@ public boolean isConnected() { return false; } - public boolean send(String host, int port, DataProtocol data) { + public boolean send(String host, int port, Message data) { if (mSocketSendRunnable != null) { SocketAddress address = new InetSocketAddress(host, port); data.mAddress = address; diff --git a/src/main/java/com/dream/socket/codec/DataProtocol.java b/src/main/java/com/dream/socket/codec/Message.java similarity index 73% rename from src/main/java/com/dream/socket/codec/DataProtocol.java rename to src/main/java/com/dream/socket/codec/Message.java index 813d767..1a350ca 100644 --- a/src/main/java/com/dream/socket/codec/DataProtocol.java +++ b/src/main/java/com/dream/socket/codec/Message.java @@ -2,6 +2,6 @@ import java.net.SocketAddress; -public abstract class DataProtocol { +public abstract class Message { public SocketAddress mAddress; } diff --git a/src/main/java/com/dream/socket/codec/MessageDecode.java b/src/main/java/com/dream/socket/codec/MessageDecode.java index 2d2fef7..3b3faa9 100644 --- a/src/main/java/com/dream/socket/codec/MessageDecode.java +++ b/src/main/java/com/dream/socket/codec/MessageDecode.java @@ -1,5 +1,6 @@ package com.dream.socket.codec; +import com.dream.socket.logger.LoggerFactory; import com.dream.socket.runnable.HandleRunnable; import java.net.SocketAddress; @@ -7,9 +8,9 @@ import java.util.HashMap; import java.util.Map; -public abstract class MessageDecode { +public abstract class MessageDecode { - private static int CACHE_BUFFER_LENGTH = 102400; + private static final int CACHE_BUFFER_LENGTH = 102400; private HandleRunnable mHandleRunnable; private Map mAddressByteBufferMap = new HashMap<>(); @@ -17,31 +18,36 @@ public void setHandleRunnable(HandleRunnable handleRunnable) { this.mHandleRunnable = handleRunnable; } - public synchronized void put(SocketAddress address, byte[] array, int offset, int length) { + public synchronized void decode(SocketAddress address, byte[] array, int offset, int length) { ByteBuffer buffer = mAddressByteBufferMap.get(address); if (buffer == null) { buffer = ByteBuffer.allocate(CACHE_BUFFER_LENGTH); mAddressByteBufferMap.put(address, buffer); buffer.flip(); + LoggerFactory.getLogger().info("创建 " + address.toString() + " 数据缓冲区ByteBuffer"); } + LoggerFactory.getLogger().info(String.format("%s 上次未解码 position=%d limit=%d", address.toString(), buffer.position(), buffer.limit())); if (buffer.limit() + length > buffer.capacity()) { //TODO 缓存区已满,丢弃读取的数据 + LoggerFactory.getLogger().warn(address.toString() + " -> decode缓存区已满,读取的数据被丢弃!!!!!"); return; } - buffer.compact(); buffer.put(array, offset, length); buffer.flip(); buffer.mark(); - + LoggerFactory.getLogger().info(String.format("%s 合并未解码 position=%d limit=%d", address.toString(), buffer.position(), buffer.limit())); T data; + LoggerFactory.getLogger().info(address.toString() + " 开始解码数据"); while (buffer.hasRemaining() && ((data = decode(address, buffer)) != null)) { + LoggerFactory.getLogger().info(address.toString() + " 成功解码一条数据"); data.mAddress = address; mHandleRunnable.put(data); buffer.compact(); buffer.flip(); buffer.mark(); } + LoggerFactory.getLogger().info(address.toString() + " 退出解码"); buffer.reset(); } diff --git a/src/main/java/com/dream/socket/codec/MessageEncode.java b/src/main/java/com/dream/socket/codec/MessageEncode.java index f9c1a02..51b15f4 100644 --- a/src/main/java/com/dream/socket/codec/MessageEncode.java +++ b/src/main/java/com/dream/socket/codec/MessageEncode.java @@ -2,7 +2,7 @@ import java.nio.ByteBuffer; -public abstract class MessageEncode { +public abstract class MessageEncode { /** diff --git a/src/main/java/com/dream/socket/codec/MessageHandle.java b/src/main/java/com/dream/socket/codec/MessageHandle.java index d61153c..a7ffa96 100644 --- a/src/main/java/com/dream/socket/codec/MessageHandle.java +++ b/src/main/java/com/dream/socket/codec/MessageHandle.java @@ -1,6 +1,6 @@ package com.dream.socket.codec; -public abstract class MessageHandle { +public abstract class MessageHandle { /** * 连接状态回调 diff --git a/src/main/java/com/dream/socket/logger/DefaultLogger.java b/src/main/java/com/dream/socket/logger/DefaultLogger.java new file mode 100644 index 0000000..4070c85 --- /dev/null +++ b/src/main/java/com/dream/socket/logger/DefaultLogger.java @@ -0,0 +1,29 @@ +package com.dream.socket.logger; + +public class DefaultLogger implements Logger { + @Override + public void debug(String log) { + System.out.println(log); + } + + @Override + public void info(String log) { + System.out.println(log); + } + + @Override + public void warn(String log) { + System.err.println(log); + } + + @Override + public void error(String log) { + System.err.println(log); + } + + @Override + public void error(String log, Throwable throwable) { + System.err.println(log); + throwable.printStackTrace(); + } +} diff --git a/src/main/java/com/dream/socket/logger/Logger.java b/src/main/java/com/dream/socket/logger/Logger.java new file mode 100644 index 0000000..ebf5805 --- /dev/null +++ b/src/main/java/com/dream/socket/logger/Logger.java @@ -0,0 +1,15 @@ +package com.dream.socket.logger; + +public interface Logger { + + void debug(String log); + + void info(String log); + + void warn(String log); + + void error(String log); + + void error(String log, Throwable throwable); + +} diff --git a/src/main/java/com/dream/socket/logger/LoggerFactory.java b/src/main/java/com/dream/socket/logger/LoggerFactory.java new file mode 100644 index 0000000..fa0e4b3 --- /dev/null +++ b/src/main/java/com/dream/socket/logger/LoggerFactory.java @@ -0,0 +1,16 @@ +package com.dream.socket.logger; + +public class LoggerFactory { + + private static Logger mLogger = new DefaultLogger(); + + public static void setLogger(Logger logger) { + if (logger != null) { + mLogger = logger; + } + } + + public static Logger getLogger() { + return mLogger; + } +} diff --git a/src/main/java/com/dream/socket/runnable/HandleRunnable.java b/src/main/java/com/dream/socket/runnable/HandleRunnable.java index 40d2d84..39faa8a 100644 --- a/src/main/java/com/dream/socket/runnable/HandleRunnable.java +++ b/src/main/java/com/dream/socket/runnable/HandleRunnable.java @@ -1,11 +1,13 @@ package com.dream.socket.runnable; -import com.dream.socket.codec.DataProtocol; +import com.dream.socket.Status; +import com.dream.socket.codec.Message; import com.dream.socket.codec.MessageHandle; +import com.dream.socket.logger.LoggerFactory; import java.util.concurrent.LinkedBlockingQueue; -public class HandleRunnable implements Runnable { +public class HandleRunnable implements Runnable { private LinkedBlockingQueue queue = new LinkedBlockingQueue<>(); private MessageHandle handle; @@ -20,8 +22,9 @@ public void run() { synchronized (this) { running = true; queue.clear(); - System.out.println("接收线程 -> 开启"); + LoggerFactory.getLogger().info("开启 -> 接收线程"); try { + handle.onStatus(Status.STATUS_CONNECTED); while (running) { T data = queue.take(); if (!running) { @@ -35,7 +38,7 @@ public void run() { e.printStackTrace(); } } - System.out.println("接收线程 -> 结束"); + LoggerFactory.getLogger().info("结束 -> 接收线程"); } public boolean put(T d) { diff --git a/src/main/java/com/dream/socket/runnable/SendRunnable.java b/src/main/java/com/dream/socket/runnable/SendRunnable.java index 34e38a8..e3c016b 100644 --- a/src/main/java/com/dream/socket/runnable/SendRunnable.java +++ b/src/main/java/com/dream/socket/runnable/SendRunnable.java @@ -1,13 +1,14 @@ package com.dream.socket.runnable; -import com.dream.socket.codec.DataProtocol; +import com.dream.socket.codec.Message; import com.dream.socket.codec.MessageEncode; +import com.dream.socket.logger.LoggerFactory; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.util.concurrent.LinkedBlockingQueue; -public abstract class SendRunnable implements Runnable { +public abstract class SendRunnable implements Runnable { private boolean sending; private MessageEncode encode; @@ -23,7 +24,7 @@ public void run() { synchronized (this) { sending = true; queue.clear(); - System.out.println("发送线程 -> 开启"); + LoggerFactory.getLogger().info("开启 -> 发送线程"); try { while (sending) { T data = queue.take(); @@ -34,14 +35,14 @@ public void run() { encode.encode(data, buffer); buffer.flip(); if (!doSend(data.mAddress, buffer.array(), 0, buffer.limit())) { - System.out.println("数据没有被真正发送出去!"); + LoggerFactory.getLogger().error("数据没有被发送出去!"); } } } catch (Exception e) { e.printStackTrace(); } } - System.out.println("发送线程 -> 结束"); + LoggerFactory.getLogger().info("结束 -> 发送线程"); } public void stop() { diff --git a/src/main/java/com/dream/socket/runnable/TCPSocketSendRunnable.java b/src/main/java/com/dream/socket/runnable/TCPSocketSendRunnable.java index 124e8e1..cca64cf 100644 --- a/src/main/java/com/dream/socket/runnable/TCPSocketSendRunnable.java +++ b/src/main/java/com/dream/socket/runnable/TCPSocketSendRunnable.java @@ -1,12 +1,13 @@ package com.dream.socket.runnable; -import com.dream.socket.codec.DataProtocol; +import com.dream.socket.codec.Message; import com.dream.socket.codec.MessageEncode; +import com.dream.socket.logger.LoggerFactory; import java.io.OutputStream; import java.net.SocketAddress; -public final class TCPSocketSendRunnable extends SendRunnable { +public final class TCPSocketSendRunnable extends SendRunnable { private OutputStream out; @@ -26,10 +27,10 @@ protected boolean doSend(SocketAddress address, byte[] buffer, int offset, int l out.flush(); return true; } catch (Exception e) { - System.err.println("数据发送异常!"); + LoggerFactory.getLogger().error("数据发送异常!", e); } } else { - System.out.println("发送管道为NULL!"); + LoggerFactory.getLogger().error("发送管道OutputStream为NULL!"); } return false; } diff --git a/src/main/java/com/dream/socket/runnable/UDPSocketSendRunnable.java b/src/main/java/com/dream/socket/runnable/UDPSocketSendRunnable.java index 27f0796..88dd35e 100644 --- a/src/main/java/com/dream/socket/runnable/UDPSocketSendRunnable.java +++ b/src/main/java/com/dream/socket/runnable/UDPSocketSendRunnable.java @@ -1,17 +1,19 @@ package com.dream.socket.runnable; -import com.dream.socket.codec.DataProtocol; +import com.dream.socket.codec.Message; import com.dream.socket.codec.MessageEncode; +import com.dream.socket.logger.LoggerFactory; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketAddress; -public class UDPSocketSendRunnable extends SendRunnable { +public class UDPSocketSendRunnable extends SendRunnable { private DatagramSocket mSocket; private DatagramPacket packet; + public UDPSocketSendRunnable(MessageEncode encode) { super(encode); } @@ -24,7 +26,7 @@ public void setDatagramSocket(DatagramSocket socket) { protected boolean doSend(SocketAddress address, byte[] buffer, int offset, int length) { if (mSocket != null) { try { - if(packet == null){ + if (packet == null) { packet = new DatagramPacket(buffer, buffer.length); } packet.setData(buffer, offset, length); @@ -32,8 +34,10 @@ protected boolean doSend(SocketAddress address, byte[] buffer, int offset, int l mSocket.send(packet); return true; } catch (IOException e) { - e.printStackTrace(); + LoggerFactory.getLogger().error("数据发送异常!", e); } + } else { + LoggerFactory.getLogger().error("发送管道DatagramSocket为NULL!"); } return false; } diff --git a/src/test/java/com/dream/socket/StringMessage.java b/src/test/java/com/dream/socket/StringMessage.java new file mode 100644 index 0000000..81a8d71 --- /dev/null +++ b/src/test/java/com/dream/socket/StringMessage.java @@ -0,0 +1,19 @@ +package com.dream.socket; + +import com.dream.socket.codec.Message; + +public class StringMessage extends Message { + private String string; + + public StringMessage(byte[] array) { + string = new String(array); + } + + public StringMessage(byte[] array, int i, int limit) { + string = new String(array, i, limit); + } + + public String getString() { + return string; + } +} diff --git a/src/test/java/com/dream/socket/Client.java b/src/test/java/com/dream/socket/TCPClient.java similarity index 77% rename from src/test/java/com/dream/socket/Client.java rename to src/test/java/com/dream/socket/TCPClient.java index 43819ca..185caf5 100644 --- a/src/test/java/com/dream/socket/Client.java +++ b/src/test/java/com/dream/socket/TCPClient.java @@ -1,6 +1,5 @@ package com.dream.socket; -import com.dream.socket.codec.DataProtocol; import com.dream.socket.codec.MessageDecode; import com.dream.socket.codec.MessageEncode; import com.dream.socket.codec.MessageHandle; @@ -8,62 +7,44 @@ import java.net.SocketAddress; import java.nio.ByteBuffer; -public class Client { +public class TCPClient { - private DreamTCPSocket socket; - - public static final class StringProtocol extends DataProtocol{ - - String string; - - public StringProtocol(byte[] array) { - string = new String(array); - } - - public StringProtocol(byte[] array, int i, int limit) { - string = new String(array, i, limit); - } - - public String getString() { - return string; - } - } public static void main(String[] args) { - DreamUDPSocket socket = new DreamUDPSocket(); - socket.codec(new MessageDecode() { + DreamTCPSocket socket = new DreamTCPSocket("localhost", 6969); + socket.codec(new MessageDecode() { @Override - protected StringProtocol decode(SocketAddress address, ByteBuffer buffer) { + protected StringMessage decode(SocketAddress address, ByteBuffer buffer) { int limit = buffer.limit(); - byte[] array = new byte[limit]; - buffer.get(array); - return new StringProtocol(array); + if(limit < 4){ + return null; + } + int len = buffer.getInt(); + if(buffer.remaining() >= len){ + byte[] array = new byte[len]; + buffer.get(array); + return new StringMessage(array); + } + return null; } - }, new MessageHandle() { + }, new MessageHandle() { @Override public void onStatus(int status) { } @Override - public void onMessage(StringProtocol data) { + public void onMessage(StringMessage data) { System.out.println(data.getString()); } - }, new MessageEncode() { + }, new MessageEncode() { @Override - public void encode(StringProtocol data, ByteBuffer buffer) { + public void encode(StringMessage data, ByteBuffer buffer) { buffer.put(data.getString().getBytes()); } }); socket.start(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - for(int i=1; i<=10; i++){ - socket.send("localhost", 6969, new StringProtocol(("message -> " + i).getBytes())); - } + // StringBuilder writer = new StringBuilder(); // writer.append("GET / HTTP/1.1\r\n"); // writer.append("Host: www.oschina.net\r\n"); @@ -72,12 +53,6 @@ public void encode(StringProtocol data, ByteBuffer buffer) { // writer.append("\r\n"); // socket.send(writer.toString()); // - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - socket.stop(); } // // public void setSocket(DreamTCPSocket socket) { diff --git a/src/test/java/com/dream/socket/TCPServer.java b/src/test/java/com/dream/socket/TCPServer.java new file mode 100644 index 0000000..28a2731 --- /dev/null +++ b/src/test/java/com/dream/socket/TCPServer.java @@ -0,0 +1,28 @@ +package com.dream.socket; + +import java.io.DataOutputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +public class TCPServer { + + public static void main(String[] args) throws Exception { + ServerSocket server = new ServerSocket(6969); + while (true){ + Socket socket = server.accept(); + OutputStream out = socket.getOutputStream(); + DataOutputStream dos = new DataOutputStream(out); + for(int i=1; i<=10; i++){ + byte[] array = ("Message -> " + i).getBytes(); + dos.writeInt(array.length); + dos.flush(); + Thread.sleep(1000); + dos.write(array); + dos.flush(); + } + socket.close(); + } + } + +} diff --git a/src/test/java/com/dream/socket/UDPClient.java b/src/test/java/com/dream/socket/UDPClient.java new file mode 100644 index 0000000..a0fe416 --- /dev/null +++ b/src/test/java/com/dream/socket/UDPClient.java @@ -0,0 +1,57 @@ +package com.dream.socket; + +import com.dream.socket.codec.MessageDecode; +import com.dream.socket.codec.MessageEncode; +import com.dream.socket.codec.MessageHandle; + +import java.net.SocketAddress; +import java.nio.ByteBuffer; + +public class UDPClient { + + public static void main(String[] args) { + DreamUDPSocket socket = new DreamUDPSocket(); + socket.codec(new MessageDecode() { + @Override + protected StringMessage decode(SocketAddress address, ByteBuffer buffer) { + int limit = buffer.limit(); + byte[] array = new byte[limit]; + buffer.get(array); + return new StringMessage(array); + } + }, new MessageHandle() { + @Override + public void onStatus(int status) { + + } + + @Override + public void onMessage(StringMessage data) { + System.out.println(data.getString()); + } + }, new MessageEncode() { + @Override + public void encode(StringMessage data, ByteBuffer buffer) { + buffer.put(data.getString().getBytes()); + } + }); + socket.start(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int i = 1; i <= 10; i++) { + socket.send("localhost", 6969, new StringMessage(("message 6969 -> " + i).getBytes())); +// socket.send("localhost", 6960, new StringMessage(("message 6960 -> " + i).getBytes())); +// socket.send("localhost", 6961, new StringMessage(("message 6961 -> " + i).getBytes())); +// socket.send("localhost", 6962, new StringMessage(("message 6962 -> " + i).getBytes())); + } + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + socket.stop(); + } +} diff --git a/src/test/java/com/dream/socket/protobuf/Protobuf.java b/src/test/java/com/dream/socket/protobuf/Protobuf.java deleted file mode 100644 index 067d302..0000000 --- a/src/test/java/com/dream/socket/protobuf/Protobuf.java +++ /dev/null @@ -1,2581 +0,0 @@ -package com.dream.socket.protobuf;// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: protobuf.proto - -public final class Protobuf { - private Protobuf() {} - public static void registerAllExtensions( - com.google.protobuf.ExtensionRegistryLite registry) { - } - - public static void registerAllExtensions( - com.google.protobuf.ExtensionRegistry registry) { - registerAllExtensions( - (com.google.protobuf.ExtensionRegistryLite) registry); - } - public interface BodyOrBuilder extends - // @@protoc_insertion_point(interface_extends:Body) - com.google.protobuf.MessageOrBuilder { - - /** - *
-     *消息ID
-     * 
- * - * string id = 1; - */ - String getId(); - /** - *
-     *消息ID
-     * 
- * - * string id = 1; - */ - com.google.protobuf.ByteString - getIdBytes(); - - /** - *
-     *消息类型
-     * 
- * - * int32 type = 2; - */ - int getType(); - - /** - *
-     *发送者ID
-     * 
- * - * int32 sender = 3; - */ - int getSender(); - - /** - *
-     *内容,更具类型转换成相应类型
-     * 
- * - * bytes content = 4; - */ - com.google.protobuf.ByteString getContent(); - } - /** - * Protobuf type {@code Body} - */ - public static final class Body extends - com.google.protobuf.GeneratedMessageV3 implements - // @@protoc_insertion_point(message_implements:Body) - BodyOrBuilder { - // Use Body.newBuilder() to construct. - private Body(com.google.protobuf.GeneratedMessageV3.Builder builder) { - super(builder); - } - private Body() { - id_ = ""; - type_ = 0; - sender_ = 0; - content_ = com.google.protobuf.ByteString.EMPTY; - } - - @Override - public final com.google.protobuf.UnknownFieldSet - getUnknownFields() { - return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); - } - private Body( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - this(); - int mutable_bitField0_ = 0; - try { - boolean done = false; - while (!done) { - int tag = input.readTag(); - switch (tag) { - case 0: - done = true; - break; - default: { - if (!input.skipField(tag)) { - done = true; - } - break; - } - case 10: { - String s = input.readStringRequireUtf8(); - - id_ = s; - break; - } - case 16: { - - type_ = input.readInt32(); - break; - } - case 24: { - - sender_ = input.readInt32(); - break; - } - case 34: { - - content_ = input.readBytes(); - break; - } - } - } - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(this); - } catch (java.io.IOException e) { - throw new com.google.protobuf.InvalidProtocolBufferException( - e).setUnfinishedMessage(this); - } finally { - makeExtensionsImmutable(); - } - } - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Body_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Body_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Body.class, Builder.class); - } - - public static final int ID_FIELD_NUMBER = 1; - private volatile Object id_; - /** - *
-     *消息ID
-     * 
- * - * string id = 1; - */ - public String getId() { - Object ref = id_; - if (ref instanceof String) { - return (String) ref; - } else { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - String s = bs.toStringUtf8(); - id_ = s; - return s; - } - } - /** - *
-     *消息ID
-     * 
- * - * string id = 1; - */ - public com.google.protobuf.ByteString - getIdBytes() { - Object ref = id_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (String) ref); - id_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - - public static final int TYPE_FIELD_NUMBER = 2; - private int type_; - /** - *
-     *消息类型
-     * 
- * - * int32 type = 2; - */ - public int getType() { - return type_; - } - - public static final int SENDER_FIELD_NUMBER = 3; - private int sender_; - /** - *
-     *发送者ID
-     * 
- * - * int32 sender = 3; - */ - public int getSender() { - return sender_; - } - - public static final int CONTENT_FIELD_NUMBER = 4; - private com.google.protobuf.ByteString content_; - /** - *
-     *内容,更具类型转换成相应类型
-     * 
- * - * bytes content = 4; - */ - public com.google.protobuf.ByteString getContent() { - return content_; - } - - private byte memoizedIsInitialized = -1; - public final boolean isInitialized() { - byte isInitialized = memoizedIsInitialized; - if (isInitialized == 1) return true; - if (isInitialized == 0) return false; - - memoizedIsInitialized = 1; - return true; - } - - public void writeTo(com.google.protobuf.CodedOutputStream output) - throws java.io.IOException { - if (!getIdBytes().isEmpty()) { - com.google.protobuf.GeneratedMessageV3.writeString(output, 1, id_); - } - if (type_ != 0) { - output.writeInt32(2, type_); - } - if (sender_ != 0) { - output.writeInt32(3, sender_); - } - if (!content_.isEmpty()) { - output.writeBytes(4, content_); - } - } - - public int getSerializedSize() { - int size = memoizedSize; - if (size != -1) return size; - - size = 0; - if (!getIdBytes().isEmpty()) { - size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, id_); - } - if (type_ != 0) { - size += com.google.protobuf.CodedOutputStream - .computeInt32Size(2, type_); - } - if (sender_ != 0) { - size += com.google.protobuf.CodedOutputStream - .computeInt32Size(3, sender_); - } - if (!content_.isEmpty()) { - size += com.google.protobuf.CodedOutputStream - .computeBytesSize(4, content_); - } - memoizedSize = size; - return size; - } - - private static final long serialVersionUID = 0L; - @Override - public boolean equals(final Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof Body)) { - return super.equals(obj); - } - Body other = (Body) obj; - - boolean result = true; - result = result && getId() - .equals(other.getId()); - result = result && (getType() - == other.getType()); - result = result && (getSender() - == other.getSender()); - result = result && getContent() - .equals(other.getContent()); - return result; - } - - @Override - public int hashCode() { - if (memoizedHashCode != 0) { - return memoizedHashCode; - } - int hash = 41; - hash = (19 * hash) + getDescriptor().hashCode(); - hash = (37 * hash) + ID_FIELD_NUMBER; - hash = (53 * hash) + getId().hashCode(); - hash = (37 * hash) + TYPE_FIELD_NUMBER; - hash = (53 * hash) + getType(); - hash = (37 * hash) + SENDER_FIELD_NUMBER; - hash = (53 * hash) + getSender(); - hash = (37 * hash) + CONTENT_FIELD_NUMBER; - hash = (53 * hash) + getContent().hashCode(); - hash = (29 * hash) + unknownFields.hashCode(); - memoizedHashCode = hash; - return hash; - } - - public static Body parseFrom( - java.nio.ByteBuffer data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Body parseFrom( - java.nio.ByteBuffer data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Body parseFrom( - com.google.protobuf.ByteString data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Body parseFrom( - com.google.protobuf.ByteString data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Body parseFrom(byte[] data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Body parseFrom( - byte[] data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Body parseFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Body parseFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - public static Body parseDelimitedFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input); - } - public static Body parseDelimitedFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input, extensionRegistry); - } - public static Body parseFrom( - com.google.protobuf.CodedInputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Body parseFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - - public Builder newBuilderForType() { return newBuilder(); } - public static Builder newBuilder() { - return DEFAULT_INSTANCE.toBuilder(); - } - public static Builder newBuilder(Body prototype) { - return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); - } - public Builder toBuilder() { - return this == DEFAULT_INSTANCE - ? new Builder() : new Builder().mergeFrom(this); - } - - @Override - protected Builder newBuilderForType( - BuilderParent parent) { - Builder builder = new Builder(parent); - return builder; - } - /** - * Protobuf type {@code Body} - */ - public static final class Builder extends - com.google.protobuf.GeneratedMessageV3.Builder implements - // @@protoc_insertion_point(builder_implements:Body) - BodyOrBuilder { - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Body_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Body_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Body.class, Builder.class); - } - - // Construct using Protobuf.Body.newBuilder() - private Builder() { - maybeForceBuilderInitialization(); - } - - private Builder( - BuilderParent parent) { - super(parent); - maybeForceBuilderInitialization(); - } - private void maybeForceBuilderInitialization() { - if (com.google.protobuf.GeneratedMessageV3 - .alwaysUseFieldBuilders) { - } - } - public Builder clear() { - super.clear(); - id_ = ""; - - type_ = 0; - - sender_ = 0; - - content_ = com.google.protobuf.ByteString.EMPTY; - - return this; - } - - public com.google.protobuf.Descriptors.Descriptor - getDescriptorForType() { - return Protobuf.internal_static_Body_descriptor; - } - - public Body getDefaultInstanceForType() { - return Body.getDefaultInstance(); - } - - public Body build() { - Body result = buildPartial(); - if (!result.isInitialized()) { - throw newUninitializedMessageException(result); - } - return result; - } - - public Body buildPartial() { - Body result = new Body(this); - result.id_ = id_; - result.type_ = type_; - result.sender_ = sender_; - result.content_ = content_; - onBuilt(); - return result; - } - - public Builder clone() { - return (Builder) super.clone(); - } - public Builder setField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.setField(field, value); - } - public Builder clearField( - com.google.protobuf.Descriptors.FieldDescriptor field) { - return (Builder) super.clearField(field); - } - public Builder clearOneof( - com.google.protobuf.Descriptors.OneofDescriptor oneof) { - return (Builder) super.clearOneof(oneof); - } - public Builder setRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - int index, Object value) { - return (Builder) super.setRepeatedField(field, index, value); - } - public Builder addRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.addRepeatedField(field, value); - } - public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof Body) { - return mergeFrom((Body)other); - } else { - super.mergeFrom(other); - return this; - } - } - - public Builder mergeFrom(Body other) { - if (other == Body.getDefaultInstance()) return this; - if (!other.getId().isEmpty()) { - id_ = other.id_; - onChanged(); - } - if (other.getType() != 0) { - setType(other.getType()); - } - if (other.getSender() != 0) { - setSender(other.getSender()); - } - if (other.getContent() != com.google.protobuf.ByteString.EMPTY) { - setContent(other.getContent()); - } - onChanged(); - return this; - } - - public final boolean isInitialized() { - return true; - } - - public Builder mergeFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - Body parsedMessage = null; - try { - parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - parsedMessage = (Body) e.getUnfinishedMessage(); - throw e.unwrapIOException(); - } finally { - if (parsedMessage != null) { - mergeFrom(parsedMessage); - } - } - return this; - } - - private Object id_ = ""; - /** - *
-       *消息ID
-       * 
- * - * string id = 1; - */ - public String getId() { - Object ref = id_; - if (!(ref instanceof String)) { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - String s = bs.toStringUtf8(); - id_ = s; - return s; - } else { - return (String) ref; - } - } - /** - *
-       *消息ID
-       * 
- * - * string id = 1; - */ - public com.google.protobuf.ByteString - getIdBytes() { - Object ref = id_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (String) ref); - id_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - /** - *
-       *消息ID
-       * 
- * - * string id = 1; - */ - public Builder setId( - String value) { - if (value == null) { - throw new NullPointerException(); - } - - id_ = value; - onChanged(); - return this; - } - /** - *
-       *消息ID
-       * 
- * - * string id = 1; - */ - public Builder clearId() { - - id_ = getDefaultInstance().getId(); - onChanged(); - return this; - } - /** - *
-       *消息ID
-       * 
- * - * string id = 1; - */ - public Builder setIdBytes( - com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - checkByteStringIsUtf8(value); - - id_ = value; - onChanged(); - return this; - } - - private int type_ ; - /** - *
-       *消息类型
-       * 
- * - * int32 type = 2; - */ - public int getType() { - return type_; - } - /** - *
-       *消息类型
-       * 
- * - * int32 type = 2; - */ - public Builder setType(int value) { - - type_ = value; - onChanged(); - return this; - } - /** - *
-       *消息类型
-       * 
- * - * int32 type = 2; - */ - public Builder clearType() { - - type_ = 0; - onChanged(); - return this; - } - - private int sender_ ; - /** - *
-       *发送者ID
-       * 
- * - * int32 sender = 3; - */ - public int getSender() { - return sender_; - } - /** - *
-       *发送者ID
-       * 
- * - * int32 sender = 3; - */ - public Builder setSender(int value) { - - sender_ = value; - onChanged(); - return this; - } - /** - *
-       *发送者ID
-       * 
- * - * int32 sender = 3; - */ - public Builder clearSender() { - - sender_ = 0; - onChanged(); - return this; - } - - private com.google.protobuf.ByteString content_ = com.google.protobuf.ByteString.EMPTY; - /** - *
-       *内容,更具类型转换成相应类型
-       * 
- * - * bytes content = 4; - */ - public com.google.protobuf.ByteString getContent() { - return content_; - } - /** - *
-       *内容,更具类型转换成相应类型
-       * 
- * - * bytes content = 4; - */ - public Builder setContent(com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - - content_ = value; - onChanged(); - return this; - } - /** - *
-       *内容,更具类型转换成相应类型
-       * 
- * - * bytes content = 4; - */ - public Builder clearContent() { - - content_ = getDefaultInstance().getContent(); - onChanged(); - return this; - } - public final Builder setUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - public final Builder mergeUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - - // @@protoc_insertion_point(builder_scope:Body) - } - - // @@protoc_insertion_point(class_scope:Body) - private static final Body DEFAULT_INSTANCE; - static { - DEFAULT_INSTANCE = new Body(); - } - - public static Body getDefaultInstance() { - return DEFAULT_INSTANCE; - } - - private static final com.google.protobuf.Parser - PARSER = new com.google.protobuf.AbstractParser() { - public Body parsePartialFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return new Body(input, extensionRegistry); - } - }; - - public static com.google.protobuf.Parser parser() { - return PARSER; - } - - @Override - public com.google.protobuf.Parser getParserForType() { - return PARSER; - } - - public Body getDefaultInstanceForType() { - return DEFAULT_INSTANCE; - } - - } - - public interface LoginOrBuilder extends - // @@protoc_insertion_point(interface_extends:Login) - com.google.protobuf.MessageOrBuilder { - - /** - * string toke = 1; - */ - String getToke(); - /** - * string toke = 1; - */ - com.google.protobuf.ByteString - getTokeBytes(); - } - /** - * Protobuf type {@code Login} - */ - public static final class Login extends - com.google.protobuf.GeneratedMessageV3 implements - // @@protoc_insertion_point(message_implements:Login) - LoginOrBuilder { - // Use Login.newBuilder() to construct. - private Login(com.google.protobuf.GeneratedMessageV3.Builder builder) { - super(builder); - } - private Login() { - toke_ = ""; - } - - @Override - public final com.google.protobuf.UnknownFieldSet - getUnknownFields() { - return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); - } - private Login( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - this(); - int mutable_bitField0_ = 0; - try { - boolean done = false; - while (!done) { - int tag = input.readTag(); - switch (tag) { - case 0: - done = true; - break; - default: { - if (!input.skipField(tag)) { - done = true; - } - break; - } - case 10: { - String s = input.readStringRequireUtf8(); - - toke_ = s; - break; - } - } - } - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(this); - } catch (java.io.IOException e) { - throw new com.google.protobuf.InvalidProtocolBufferException( - e).setUnfinishedMessage(this); - } finally { - makeExtensionsImmutable(); - } - } - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Login_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Login_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Login.class, Builder.class); - } - - public static final int TOKE_FIELD_NUMBER = 1; - private volatile Object toke_; - /** - * string toke = 1; - */ - public String getToke() { - Object ref = toke_; - if (ref instanceof String) { - return (String) ref; - } else { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - String s = bs.toStringUtf8(); - toke_ = s; - return s; - } - } - /** - * string toke = 1; - */ - public com.google.protobuf.ByteString - getTokeBytes() { - Object ref = toke_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (String) ref); - toke_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - - private byte memoizedIsInitialized = -1; - public final boolean isInitialized() { - byte isInitialized = memoizedIsInitialized; - if (isInitialized == 1) return true; - if (isInitialized == 0) return false; - - memoizedIsInitialized = 1; - return true; - } - - public void writeTo(com.google.protobuf.CodedOutputStream output) - throws java.io.IOException { - if (!getTokeBytes().isEmpty()) { - com.google.protobuf.GeneratedMessageV3.writeString(output, 1, toke_); - } - } - - public int getSerializedSize() { - int size = memoizedSize; - if (size != -1) return size; - - size = 0; - if (!getTokeBytes().isEmpty()) { - size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, toke_); - } - memoizedSize = size; - return size; - } - - private static final long serialVersionUID = 0L; - @Override - public boolean equals(final Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof Login)) { - return super.equals(obj); - } - Login other = (Login) obj; - - boolean result = true; - result = result && getToke() - .equals(other.getToke()); - return result; - } - - @Override - public int hashCode() { - if (memoizedHashCode != 0) { - return memoizedHashCode; - } - int hash = 41; - hash = (19 * hash) + getDescriptor().hashCode(); - hash = (37 * hash) + TOKE_FIELD_NUMBER; - hash = (53 * hash) + getToke().hashCode(); - hash = (29 * hash) + unknownFields.hashCode(); - memoizedHashCode = hash; - return hash; - } - - public static Login parseFrom( - java.nio.ByteBuffer data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Login parseFrom( - java.nio.ByteBuffer data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Login parseFrom( - com.google.protobuf.ByteString data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Login parseFrom( - com.google.protobuf.ByteString data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Login parseFrom(byte[] data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Login parseFrom( - byte[] data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Login parseFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Login parseFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - public static Login parseDelimitedFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input); - } - public static Login parseDelimitedFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input, extensionRegistry); - } - public static Login parseFrom( - com.google.protobuf.CodedInputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Login parseFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - - public Builder newBuilderForType() { return newBuilder(); } - public static Builder newBuilder() { - return DEFAULT_INSTANCE.toBuilder(); - } - public static Builder newBuilder(Login prototype) { - return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); - } - public Builder toBuilder() { - return this == DEFAULT_INSTANCE - ? new Builder() : new Builder().mergeFrom(this); - } - - @Override - protected Builder newBuilderForType( - BuilderParent parent) { - Builder builder = new Builder(parent); - return builder; - } - /** - * Protobuf type {@code Login} - */ - public static final class Builder extends - com.google.protobuf.GeneratedMessageV3.Builder implements - // @@protoc_insertion_point(builder_implements:Login) - LoginOrBuilder { - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Login_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Login_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Login.class, Builder.class); - } - - // Construct using Protobuf.Login.newBuilder() - private Builder() { - maybeForceBuilderInitialization(); - } - - private Builder( - BuilderParent parent) { - super(parent); - maybeForceBuilderInitialization(); - } - private void maybeForceBuilderInitialization() { - if (com.google.protobuf.GeneratedMessageV3 - .alwaysUseFieldBuilders) { - } - } - public Builder clear() { - super.clear(); - toke_ = ""; - - return this; - } - - public com.google.protobuf.Descriptors.Descriptor - getDescriptorForType() { - return Protobuf.internal_static_Login_descriptor; - } - - public Login getDefaultInstanceForType() { - return Login.getDefaultInstance(); - } - - public Login build() { - Login result = buildPartial(); - if (!result.isInitialized()) { - throw newUninitializedMessageException(result); - } - return result; - } - - public Login buildPartial() { - Login result = new Login(this); - result.toke_ = toke_; - onBuilt(); - return result; - } - - public Builder clone() { - return (Builder) super.clone(); - } - public Builder setField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.setField(field, value); - } - public Builder clearField( - com.google.protobuf.Descriptors.FieldDescriptor field) { - return (Builder) super.clearField(field); - } - public Builder clearOneof( - com.google.protobuf.Descriptors.OneofDescriptor oneof) { - return (Builder) super.clearOneof(oneof); - } - public Builder setRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - int index, Object value) { - return (Builder) super.setRepeatedField(field, index, value); - } - public Builder addRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.addRepeatedField(field, value); - } - public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof Login) { - return mergeFrom((Login)other); - } else { - super.mergeFrom(other); - return this; - } - } - - public Builder mergeFrom(Login other) { - if (other == Login.getDefaultInstance()) return this; - if (!other.getToke().isEmpty()) { - toke_ = other.toke_; - onChanged(); - } - onChanged(); - return this; - } - - public final boolean isInitialized() { - return true; - } - - public Builder mergeFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - Login parsedMessage = null; - try { - parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - parsedMessage = (Login) e.getUnfinishedMessage(); - throw e.unwrapIOException(); - } finally { - if (parsedMessage != null) { - mergeFrom(parsedMessage); - } - } - return this; - } - - private Object toke_ = ""; - /** - * string toke = 1; - */ - public String getToke() { - Object ref = toke_; - if (!(ref instanceof String)) { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - String s = bs.toStringUtf8(); - toke_ = s; - return s; - } else { - return (String) ref; - } - } - /** - * string toke = 1; - */ - public com.google.protobuf.ByteString - getTokeBytes() { - Object ref = toke_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (String) ref); - toke_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - /** - * string toke = 1; - */ - public Builder setToke( - String value) { - if (value == null) { - throw new NullPointerException(); - } - - toke_ = value; - onChanged(); - return this; - } - /** - * string toke = 1; - */ - public Builder clearToke() { - - toke_ = getDefaultInstance().getToke(); - onChanged(); - return this; - } - /** - * string toke = 1; - */ - public Builder setTokeBytes( - com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - checkByteStringIsUtf8(value); - - toke_ = value; - onChanged(); - return this; - } - public final Builder setUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - public final Builder mergeUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - - // @@protoc_insertion_point(builder_scope:Login) - } - - // @@protoc_insertion_point(class_scope:Login) - private static final Login DEFAULT_INSTANCE; - static { - DEFAULT_INSTANCE = new Login(); - } - - public static Login getDefaultInstance() { - return DEFAULT_INSTANCE; - } - - private static final com.google.protobuf.Parser - PARSER = new com.google.protobuf.AbstractParser() { - public Login parsePartialFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return new Login(input, extensionRegistry); - } - }; - - public static com.google.protobuf.Parser parser() { - return PARSER; - } - - @Override - public com.google.protobuf.Parser getParserForType() { - return PARSER; - } - - public Login getDefaultInstanceForType() { - return DEFAULT_INSTANCE; - } - - } - - public interface ResponseOrBuilder extends - // @@protoc_insertion_point(interface_extends:Response) - com.google.protobuf.MessageOrBuilder { - - /** - *
-     *相应状态码
-     * 
- * - * int32 code = 1; - */ - int getCode(); - - /** - *
-     *数据
-     * 
- * - * bytes data = 2; - */ - com.google.protobuf.ByteString getData(); - } - /** - * Protobuf type {@code Response} - */ - public static final class Response extends - com.google.protobuf.GeneratedMessageV3 implements - // @@protoc_insertion_point(message_implements:Response) - ResponseOrBuilder { - // Use Response.newBuilder() to construct. - private Response(com.google.protobuf.GeneratedMessageV3.Builder builder) { - super(builder); - } - private Response() { - code_ = 0; - data_ = com.google.protobuf.ByteString.EMPTY; - } - - @Override - public final com.google.protobuf.UnknownFieldSet - getUnknownFields() { - return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); - } - private Response( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - this(); - int mutable_bitField0_ = 0; - try { - boolean done = false; - while (!done) { - int tag = input.readTag(); - switch (tag) { - case 0: - done = true; - break; - default: { - if (!input.skipField(tag)) { - done = true; - } - break; - } - case 8: { - - code_ = input.readInt32(); - break; - } - case 18: { - - data_ = input.readBytes(); - break; - } - } - } - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(this); - } catch (java.io.IOException e) { - throw new com.google.protobuf.InvalidProtocolBufferException( - e).setUnfinishedMessage(this); - } finally { - makeExtensionsImmutable(); - } - } - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Response_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Response_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Response.class, Builder.class); - } - - public static final int CODE_FIELD_NUMBER = 1; - private int code_; - /** - *
-     *相应状态码
-     * 
- * - * int32 code = 1; - */ - public int getCode() { - return code_; - } - - public static final int DATA_FIELD_NUMBER = 2; - private com.google.protobuf.ByteString data_; - /** - *
-     *数据
-     * 
- * - * bytes data = 2; - */ - public com.google.protobuf.ByteString getData() { - return data_; - } - - private byte memoizedIsInitialized = -1; - public final boolean isInitialized() { - byte isInitialized = memoizedIsInitialized; - if (isInitialized == 1) return true; - if (isInitialized == 0) return false; - - memoizedIsInitialized = 1; - return true; - } - - public void writeTo(com.google.protobuf.CodedOutputStream output) - throws java.io.IOException { - if (code_ != 0) { - output.writeInt32(1, code_); - } - if (!data_.isEmpty()) { - output.writeBytes(2, data_); - } - } - - public int getSerializedSize() { - int size = memoizedSize; - if (size != -1) return size; - - size = 0; - if (code_ != 0) { - size += com.google.protobuf.CodedOutputStream - .computeInt32Size(1, code_); - } - if (!data_.isEmpty()) { - size += com.google.protobuf.CodedOutputStream - .computeBytesSize(2, data_); - } - memoizedSize = size; - return size; - } - - private static final long serialVersionUID = 0L; - @Override - public boolean equals(final Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof Response)) { - return super.equals(obj); - } - Response other = (Response) obj; - - boolean result = true; - result = result && (getCode() - == other.getCode()); - result = result && getData() - .equals(other.getData()); - return result; - } - - @Override - public int hashCode() { - if (memoizedHashCode != 0) { - return memoizedHashCode; - } - int hash = 41; - hash = (19 * hash) + getDescriptor().hashCode(); - hash = (37 * hash) + CODE_FIELD_NUMBER; - hash = (53 * hash) + getCode(); - hash = (37 * hash) + DATA_FIELD_NUMBER; - hash = (53 * hash) + getData().hashCode(); - hash = (29 * hash) + unknownFields.hashCode(); - memoizedHashCode = hash; - return hash; - } - - public static Response parseFrom( - java.nio.ByteBuffer data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Response parseFrom( - java.nio.ByteBuffer data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Response parseFrom( - com.google.protobuf.ByteString data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Response parseFrom( - com.google.protobuf.ByteString data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Response parseFrom(byte[] data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Response parseFrom( - byte[] data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Response parseFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Response parseFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - public static Response parseDelimitedFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input); - } - public static Response parseDelimitedFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input, extensionRegistry); - } - public static Response parseFrom( - com.google.protobuf.CodedInputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Response parseFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - - public Builder newBuilderForType() { return newBuilder(); } - public static Builder newBuilder() { - return DEFAULT_INSTANCE.toBuilder(); - } - public static Builder newBuilder(Response prototype) { - return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); - } - public Builder toBuilder() { - return this == DEFAULT_INSTANCE - ? new Builder() : new Builder().mergeFrom(this); - } - - @Override - protected Builder newBuilderForType( - BuilderParent parent) { - Builder builder = new Builder(parent); - return builder; - } - /** - * Protobuf type {@code Response} - */ - public static final class Builder extends - com.google.protobuf.GeneratedMessageV3.Builder implements - // @@protoc_insertion_point(builder_implements:Response) - ResponseOrBuilder { - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Response_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Response_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Response.class, Builder.class); - } - - // Construct using Protobuf.Response.newBuilder() - private Builder() { - maybeForceBuilderInitialization(); - } - - private Builder( - BuilderParent parent) { - super(parent); - maybeForceBuilderInitialization(); - } - private void maybeForceBuilderInitialization() { - if (com.google.protobuf.GeneratedMessageV3 - .alwaysUseFieldBuilders) { - } - } - public Builder clear() { - super.clear(); - code_ = 0; - - data_ = com.google.protobuf.ByteString.EMPTY; - - return this; - } - - public com.google.protobuf.Descriptors.Descriptor - getDescriptorForType() { - return Protobuf.internal_static_Response_descriptor; - } - - public Response getDefaultInstanceForType() { - return Response.getDefaultInstance(); - } - - public Response build() { - Response result = buildPartial(); - if (!result.isInitialized()) { - throw newUninitializedMessageException(result); - } - return result; - } - - public Response buildPartial() { - Response result = new Response(this); - result.code_ = code_; - result.data_ = data_; - onBuilt(); - return result; - } - - public Builder clone() { - return (Builder) super.clone(); - } - public Builder setField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.setField(field, value); - } - public Builder clearField( - com.google.protobuf.Descriptors.FieldDescriptor field) { - return (Builder) super.clearField(field); - } - public Builder clearOneof( - com.google.protobuf.Descriptors.OneofDescriptor oneof) { - return (Builder) super.clearOneof(oneof); - } - public Builder setRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - int index, Object value) { - return (Builder) super.setRepeatedField(field, index, value); - } - public Builder addRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.addRepeatedField(field, value); - } - public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof Response) { - return mergeFrom((Response)other); - } else { - super.mergeFrom(other); - return this; - } - } - - public Builder mergeFrom(Response other) { - if (other == Response.getDefaultInstance()) return this; - if (other.getCode() != 0) { - setCode(other.getCode()); - } - if (other.getData() != com.google.protobuf.ByteString.EMPTY) { - setData(other.getData()); - } - onChanged(); - return this; - } - - public final boolean isInitialized() { - return true; - } - - public Builder mergeFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - Response parsedMessage = null; - try { - parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - parsedMessage = (Response) e.getUnfinishedMessage(); - throw e.unwrapIOException(); - } finally { - if (parsedMessage != null) { - mergeFrom(parsedMessage); - } - } - return this; - } - - private int code_ ; - /** - *
-       *相应状态码
-       * 
- * - * int32 code = 1; - */ - public int getCode() { - return code_; - } - /** - *
-       *相应状态码
-       * 
- * - * int32 code = 1; - */ - public Builder setCode(int value) { - - code_ = value; - onChanged(); - return this; - } - /** - *
-       *相应状态码
-       * 
- * - * int32 code = 1; - */ - public Builder clearCode() { - - code_ = 0; - onChanged(); - return this; - } - - private com.google.protobuf.ByteString data_ = com.google.protobuf.ByteString.EMPTY; - /** - *
-       *数据
-       * 
- * - * bytes data = 2; - */ - public com.google.protobuf.ByteString getData() { - return data_; - } - /** - *
-       *数据
-       * 
- * - * bytes data = 2; - */ - public Builder setData(com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - - data_ = value; - onChanged(); - return this; - } - /** - *
-       *数据
-       * 
- * - * bytes data = 2; - */ - public Builder clearData() { - - data_ = getDefaultInstance().getData(); - onChanged(); - return this; - } - public final Builder setUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - public final Builder mergeUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - - // @@protoc_insertion_point(builder_scope:Response) - } - - // @@protoc_insertion_point(class_scope:Response) - private static final Response DEFAULT_INSTANCE; - static { - DEFAULT_INSTANCE = new Response(); - } - - public static Response getDefaultInstance() { - return DEFAULT_INSTANCE; - } - - private static final com.google.protobuf.Parser - PARSER = new com.google.protobuf.AbstractParser() { - public Response parsePartialFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return new Response(input, extensionRegistry); - } - }; - - public static com.google.protobuf.Parser parser() { - return PARSER; - } - - @Override - public com.google.protobuf.Parser getParserForType() { - return PARSER; - } - - public Response getDefaultInstanceForType() { - return DEFAULT_INSTANCE; - } - - } - - public interface MessageOrBuilder extends - // @@protoc_insertion_point(interface_extends:Message) - com.google.protobuf.MessageOrBuilder { - - /** - *
-     *接收者
-     * 
- * - * int32 receiver = 1; - */ - int getReceiver(); - - /** - *
-     *聊天消息类型
-     * 
- * - * int32 type = 2; - */ - int getType(); - - /** - *
-     *内容
-     * 
- * - * bytes content = 3; - */ - com.google.protobuf.ByteString getContent(); - } - /** - * Protobuf type {@code Message} - */ - public static final class Message extends - com.google.protobuf.GeneratedMessageV3 implements - // @@protoc_insertion_point(message_implements:Message) - MessageOrBuilder { - // Use Message.newBuilder() to construct. - private Message(com.google.protobuf.GeneratedMessageV3.Builder builder) { - super(builder); - } - private Message() { - receiver_ = 0; - type_ = 0; - content_ = com.google.protobuf.ByteString.EMPTY; - } - - @Override - public final com.google.protobuf.UnknownFieldSet - getUnknownFields() { - return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); - } - private Message( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - this(); - int mutable_bitField0_ = 0; - try { - boolean done = false; - while (!done) { - int tag = input.readTag(); - switch (tag) { - case 0: - done = true; - break; - default: { - if (!input.skipField(tag)) { - done = true; - } - break; - } - case 8: { - - receiver_ = input.readInt32(); - break; - } - case 16: { - - type_ = input.readInt32(); - break; - } - case 26: { - - content_ = input.readBytes(); - break; - } - } - } - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(this); - } catch (java.io.IOException e) { - throw new com.google.protobuf.InvalidProtocolBufferException( - e).setUnfinishedMessage(this); - } finally { - makeExtensionsImmutable(); - } - } - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Message_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Message_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Message.class, Builder.class); - } - - public static final int RECEIVER_FIELD_NUMBER = 1; - private int receiver_; - /** - *
-     *接收者
-     * 
- * - * int32 receiver = 1; - */ - public int getReceiver() { - return receiver_; - } - - public static final int TYPE_FIELD_NUMBER = 2; - private int type_; - /** - *
-     *聊天消息类型
-     * 
- * - * int32 type = 2; - */ - public int getType() { - return type_; - } - - public static final int CONTENT_FIELD_NUMBER = 3; - private com.google.protobuf.ByteString content_; - /** - *
-     *内容
-     * 
- * - * bytes content = 3; - */ - public com.google.protobuf.ByteString getContent() { - return content_; - } - - private byte memoizedIsInitialized = -1; - public final boolean isInitialized() { - byte isInitialized = memoizedIsInitialized; - if (isInitialized == 1) return true; - if (isInitialized == 0) return false; - - memoizedIsInitialized = 1; - return true; - } - - public void writeTo(com.google.protobuf.CodedOutputStream output) - throws java.io.IOException { - if (receiver_ != 0) { - output.writeInt32(1, receiver_); - } - if (type_ != 0) { - output.writeInt32(2, type_); - } - if (!content_.isEmpty()) { - output.writeBytes(3, content_); - } - } - - public int getSerializedSize() { - int size = memoizedSize; - if (size != -1) return size; - - size = 0; - if (receiver_ != 0) { - size += com.google.protobuf.CodedOutputStream - .computeInt32Size(1, receiver_); - } - if (type_ != 0) { - size += com.google.protobuf.CodedOutputStream - .computeInt32Size(2, type_); - } - if (!content_.isEmpty()) { - size += com.google.protobuf.CodedOutputStream - .computeBytesSize(3, content_); - } - memoizedSize = size; - return size; - } - - private static final long serialVersionUID = 0L; - @Override - public boolean equals(final Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof Message)) { - return super.equals(obj); - } - Message other = (Message) obj; - - boolean result = true; - result = result && (getReceiver() - == other.getReceiver()); - result = result && (getType() - == other.getType()); - result = result && getContent() - .equals(other.getContent()); - return result; - } - - @Override - public int hashCode() { - if (memoizedHashCode != 0) { - return memoizedHashCode; - } - int hash = 41; - hash = (19 * hash) + getDescriptor().hashCode(); - hash = (37 * hash) + RECEIVER_FIELD_NUMBER; - hash = (53 * hash) + getReceiver(); - hash = (37 * hash) + TYPE_FIELD_NUMBER; - hash = (53 * hash) + getType(); - hash = (37 * hash) + CONTENT_FIELD_NUMBER; - hash = (53 * hash) + getContent().hashCode(); - hash = (29 * hash) + unknownFields.hashCode(); - memoizedHashCode = hash; - return hash; - } - - public static Message parseFrom( - java.nio.ByteBuffer data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Message parseFrom( - java.nio.ByteBuffer data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Message parseFrom( - com.google.protobuf.ByteString data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Message parseFrom( - com.google.protobuf.ByteString data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Message parseFrom(byte[] data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static Message parseFrom( - byte[] data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static Message parseFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Message parseFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - public static Message parseDelimitedFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input); - } - public static Message parseDelimitedFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input, extensionRegistry); - } - public static Message parseFrom( - com.google.protobuf.CodedInputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static Message parseFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - - public Builder newBuilderForType() { return newBuilder(); } - public static Builder newBuilder() { - return DEFAULT_INSTANCE.toBuilder(); - } - public static Builder newBuilder(Message prototype) { - return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); - } - public Builder toBuilder() { - return this == DEFAULT_INSTANCE - ? new Builder() : new Builder().mergeFrom(this); - } - - @Override - protected Builder newBuilderForType( - BuilderParent parent) { - Builder builder = new Builder(parent); - return builder; - } - /** - * Protobuf type {@code Message} - */ - public static final class Builder extends - com.google.protobuf.GeneratedMessageV3.Builder implements - // @@protoc_insertion_point(builder_implements:Message) - MessageOrBuilder { - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return Protobuf.internal_static_Message_descriptor; - } - - protected FieldAccessorTable - internalGetFieldAccessorTable() { - return Protobuf.internal_static_Message_fieldAccessorTable - .ensureFieldAccessorsInitialized( - Message.class, Builder.class); - } - - // Construct using Protobuf.Message.newBuilder() - private Builder() { - maybeForceBuilderInitialization(); - } - - private Builder( - BuilderParent parent) { - super(parent); - maybeForceBuilderInitialization(); - } - private void maybeForceBuilderInitialization() { - if (com.google.protobuf.GeneratedMessageV3 - .alwaysUseFieldBuilders) { - } - } - public Builder clear() { - super.clear(); - receiver_ = 0; - - type_ = 0; - - content_ = com.google.protobuf.ByteString.EMPTY; - - return this; - } - - public com.google.protobuf.Descriptors.Descriptor - getDescriptorForType() { - return Protobuf.internal_static_Message_descriptor; - } - - public Message getDefaultInstanceForType() { - return Message.getDefaultInstance(); - } - - public Message build() { - Message result = buildPartial(); - if (!result.isInitialized()) { - throw newUninitializedMessageException(result); - } - return result; - } - - public Message buildPartial() { - Message result = new Message(this); - result.receiver_ = receiver_; - result.type_ = type_; - result.content_ = content_; - onBuilt(); - return result; - } - - public Builder clone() { - return (Builder) super.clone(); - } - public Builder setField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.setField(field, value); - } - public Builder clearField( - com.google.protobuf.Descriptors.FieldDescriptor field) { - return (Builder) super.clearField(field); - } - public Builder clearOneof( - com.google.protobuf.Descriptors.OneofDescriptor oneof) { - return (Builder) super.clearOneof(oneof); - } - public Builder setRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - int index, Object value) { - return (Builder) super.setRepeatedField(field, index, value); - } - public Builder addRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - Object value) { - return (Builder) super.addRepeatedField(field, value); - } - public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof Message) { - return mergeFrom((Message)other); - } else { - super.mergeFrom(other); - return this; - } - } - - public Builder mergeFrom(Message other) { - if (other == Message.getDefaultInstance()) return this; - if (other.getReceiver() != 0) { - setReceiver(other.getReceiver()); - } - if (other.getType() != 0) { - setType(other.getType()); - } - if (other.getContent() != com.google.protobuf.ByteString.EMPTY) { - setContent(other.getContent()); - } - onChanged(); - return this; - } - - public final boolean isInitialized() { - return true; - } - - public Builder mergeFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - Message parsedMessage = null; - try { - parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - parsedMessage = (Message) e.getUnfinishedMessage(); - throw e.unwrapIOException(); - } finally { - if (parsedMessage != null) { - mergeFrom(parsedMessage); - } - } - return this; - } - - private int receiver_ ; - /** - *
-       *接收者
-       * 
- * - * int32 receiver = 1; - */ - public int getReceiver() { - return receiver_; - } - /** - *
-       *接收者
-       * 
- * - * int32 receiver = 1; - */ - public Builder setReceiver(int value) { - - receiver_ = value; - onChanged(); - return this; - } - /** - *
-       *接收者
-       * 
- * - * int32 receiver = 1; - */ - public Builder clearReceiver() { - - receiver_ = 0; - onChanged(); - return this; - } - - private int type_ ; - /** - *
-       *聊天消息类型
-       * 
- * - * int32 type = 2; - */ - public int getType() { - return type_; - } - /** - *
-       *聊天消息类型
-       * 
- * - * int32 type = 2; - */ - public Builder setType(int value) { - - type_ = value; - onChanged(); - return this; - } - /** - *
-       *聊天消息类型
-       * 
- * - * int32 type = 2; - */ - public Builder clearType() { - - type_ = 0; - onChanged(); - return this; - } - - private com.google.protobuf.ByteString content_ = com.google.protobuf.ByteString.EMPTY; - /** - *
-       *内容
-       * 
- * - * bytes content = 3; - */ - public com.google.protobuf.ByteString getContent() { - return content_; - } - /** - *
-       *内容
-       * 
- * - * bytes content = 3; - */ - public Builder setContent(com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - - content_ = value; - onChanged(); - return this; - } - /** - *
-       *内容
-       * 
- * - * bytes content = 3; - */ - public Builder clearContent() { - - content_ = getDefaultInstance().getContent(); - onChanged(); - return this; - } - public final Builder setUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - public final Builder mergeUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return this; - } - - - // @@protoc_insertion_point(builder_scope:Message) - } - - // @@protoc_insertion_point(class_scope:Message) - private static final Message DEFAULT_INSTANCE; - static { - DEFAULT_INSTANCE = new Message(); - } - - public static Message getDefaultInstance() { - return DEFAULT_INSTANCE; - } - - private static final com.google.protobuf.Parser - PARSER = new com.google.protobuf.AbstractParser() { - public Message parsePartialFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return new Message(input, extensionRegistry); - } - }; - - public static com.google.protobuf.Parser parser() { - return PARSER; - } - - @Override - public com.google.protobuf.Parser getParserForType() { - return PARSER; - } - - public Message getDefaultInstanceForType() { - return DEFAULT_INSTANCE; - } - - } - - private static final com.google.protobuf.Descriptors.Descriptor - internal_static_Body_descriptor; - private static final - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internal_static_Body_fieldAccessorTable; - private static final com.google.protobuf.Descriptors.Descriptor - internal_static_Login_descriptor; - private static final - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internal_static_Login_fieldAccessorTable; - private static final com.google.protobuf.Descriptors.Descriptor - internal_static_Response_descriptor; - private static final - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internal_static_Response_fieldAccessorTable; - private static final com.google.protobuf.Descriptors.Descriptor - internal_static_Message_descriptor; - private static final - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internal_static_Message_fieldAccessorTable; - - public static com.google.protobuf.Descriptors.FileDescriptor - getDescriptor() { - return descriptor; - } - private static com.google.protobuf.Descriptors.FileDescriptor - descriptor; - static { - String[] descriptorData = { - "\n\016protobuf.proto\"A\n\004Body\022\n\n\002id\030\001 \001(\t\022\014\n\004" + - "type\030\002 \001(\005\022\016\n\006sender\030\003 \001(\005\022\017\n\007content\030\004 " + - "\001(\014\"\025\n\005Login\022\014\n\004toke\030\001 \001(\t\"&\n\010Response\022\014" + - "\n\004code\030\001 \001(\005\022\014\n\004data\030\002 \001(\014\":\n\007Message\022\020\n" + - "\010receiver\030\001 \001(\005\022\014\n\004type\030\002 \001(\005\022\017\n\007content" + - "\030\003 \001(\014b\006proto3" - }; - com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = - new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { - public com.google.protobuf.ExtensionRegistry assignDescriptors( - com.google.protobuf.Descriptors.FileDescriptor root) { - descriptor = root; - return null; - } - }; - com.google.protobuf.Descriptors.FileDescriptor - .internalBuildGeneratedFileFrom(descriptorData, - new com.google.protobuf.Descriptors.FileDescriptor[] { - }, assigner); - internal_static_Body_descriptor = - getDescriptor().getMessageTypes().get(0); - internal_static_Body_fieldAccessorTable = new - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( - internal_static_Body_descriptor, - new String[] { "Id", "Type", "Sender", "Content", }); - internal_static_Login_descriptor = - getDescriptor().getMessageTypes().get(1); - internal_static_Login_fieldAccessorTable = new - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( - internal_static_Login_descriptor, - new String[] { "Toke", }); - internal_static_Response_descriptor = - getDescriptor().getMessageTypes().get(2); - internal_static_Response_fieldAccessorTable = new - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( - internal_static_Response_descriptor, - new String[] { "Code", "Data", }); - internal_static_Message_descriptor = - getDescriptor().getMessageTypes().get(3); - internal_static_Message_fieldAccessorTable = new - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( - internal_static_Message_descriptor, - new String[] { "Receiver", "Type", "Content", }); - } - - // @@protoc_insertion_point(outer_class_scope) -} diff --git a/src/test/java/com/dream/socket/protobuf/Type.java b/src/test/java/com/dream/socket/protobuf/Type.java deleted file mode 100644 index e080da3..0000000 --- a/src/test/java/com/dream/socket/protobuf/Type.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.dream.socket.protobuf; - -/** - * 消息类型 - */ -public interface Type { - - /** - * 应答 - */ - byte BODY_ACK = 0; - - /** - * 登录 - */ - byte BODY_MESSAGE = 10; - - /** - * 登录 - */ - byte BODY_LOGIN = 100; - - /** - * 退出 - */ - byte BODY_LOGOUT = 101; - - /** - * 推送消息 - */ - byte BODY_PUSH = 100; - - /** - * 单向消息(单聊消息) - */ - byte MESSAGE_SINGLE = 1; - - /** - * 多向消息(群消息) - */ - byte MESSAGE_GROUP = 10; - -} diff --git a/src/test/java/com/dream/socket/protocol/Packet.java b/src/test/java/com/dream/socket/protocol/Packet.java deleted file mode 100644 index b1555e1..0000000 --- a/src/test/java/com/dream/socket/protocol/Packet.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.dream.socket.protocol; - -public class Packet extends Protocol { - - /** - * 当前消息内容 - */ - public byte[] body; - - public Packet(){ - - } - - public Packet(byte[] body){ - this.body = body; - } - -} diff --git a/src/test/java/com/dream/socket/protocol/Protocol.java b/src/test/java/com/dream/socket/protocol/Protocol.java deleted file mode 100644 index 678a187..0000000 --- a/src/test/java/com/dream/socket/protocol/Protocol.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.dream.socket.protocol; - -public class Protocol { - - //{起始标记 -byte -1} - //{协议版本 -byte -1} - //{包总长度 -int -4} - //{包头保留 -byte[24] -24} - //{包头校验 -byte -1} - //{包体内容 -byte[n] -n} - //{结束标记 -byte -1} - - /** - * 包头长度(算上最后一个结束标记) - */ - public static final int HEADER_LENGTH = 32; - - /** - * 协议版本 - */ - public static final byte VERSION = 1; - - /** - * 起始标记 - */ - public static final byte START_TAG = '<'; - - /** - * 校验字符 - */ - public static final byte VERIFY_TAG = '-'; - - /** - * 结束标记 - */ - public static final byte END_TAG = '>'; - - /** - * 保留位置 - */ - public static final byte[] RETAIN = new byte[24]; - - public int version; - -} \ No newline at end of file diff --git a/src/test/java/com/dream/socket/TestAddress.java b/src/test/java/com/dream/socket/test/TestAddress.java similarity index 93% rename from src/test/java/com/dream/socket/TestAddress.java rename to src/test/java/com/dream/socket/test/TestAddress.java index ce1ed54..b1f774a 100644 --- a/src/test/java/com/dream/socket/TestAddress.java +++ b/src/test/java/com/dream/socket/test/TestAddress.java @@ -1,4 +1,4 @@ -package com.dream.socket; +package com.dream.socket.test; import java.net.InetSocketAddress; diff --git a/src/test/java/com/dream/socket/TestByteBuffer.java b/src/test/java/com/dream/socket/test/TestByteBuffer.java similarity index 98% rename from src/test/java/com/dream/socket/TestByteBuffer.java rename to src/test/java/com/dream/socket/test/TestByteBuffer.java index 9dd5bdc..e0350b4 100644 --- a/src/test/java/com/dream/socket/TestByteBuffer.java +++ b/src/test/java/com/dream/socket/test/TestByteBuffer.java @@ -1,4 +1,4 @@ -package com.dream.socket; +package com.dream.socket.test; import java.io.OutputStream; import java.net.ServerSocket;