Showing
2 changed files
with
30 additions
and
13 deletions
| ... | ... | @@ -30,7 +30,6 @@ import org.greenrobot.eventbus.EventBus; |
| 30 | 30 | import java.io.IOException; |
| 31 | 31 | import java.io.InputStream; |
| 32 | 32 | import java.io.OutputStream; |
| 33 | -import java.lang.ref.WeakReference; | |
| 34 | 33 | import java.net.Socket; |
| 35 | 34 | import java.util.Arrays; |
| 36 | 35 | import java.util.concurrent.atomic.AtomicBoolean; |
| ... | ... | @@ -38,7 +37,6 @@ import java.util.concurrent.atomic.AtomicBoolean; |
| 38 | 37 | import static com.qnbar.smc.service.SocketService.JUST_OPEN_DOOR; |
| 39 | 38 | import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR; |
| 40 | 39 | import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR_AND_GET_MOVIE; |
| 41 | -import java.util.concurrent.atomic.AtomicBoolean; | |
| 42 | 40 | |
| 43 | 41 | public class SocketService1 extends BaseService { |
| 44 | 42 | private static final String TAG = "BackService1"; |
| ... | ... | @@ -98,10 +96,10 @@ public class SocketService1 extends BaseService { |
| 98 | 96 | // public String testRoomSn = "R170413034374"; |
| 99 | 97 | public String testRoomSn = "c"; |
| 100 | 98 | |
| 101 | -// private ReadThread mReadThread; | |
| 99 | + // private ReadThread mReadThread; | |
| 102 | 100 | private Gson gson = new Gson(); |
| 103 | 101 | |
| 104 | -// private WeakReference<Socket> mSocket; | |
| 102 | + // private WeakReference<Socket> mSocket; | |
| 105 | 103 | private Socket socket = null; |
| 106 | 104 | StringBuilder readingMsg = new StringBuilder(); |
| 107 | 105 | |
| ... | ... | @@ -115,11 +113,13 @@ public class SocketService1 extends BaseService { |
| 115 | 113 | |
| 116 | 114 | @Override |
| 117 | 115 | public void run() { |
| 118 | - Log.d(TAG, "heart beat"); | |
| 119 | 116 | mHandler.postDelayed(this, HEART_BEAT_RATE); |
| 117 | + LogUtils.d(TAG, "heart beat"); | |
| 120 | 118 | //就发送一个HEART_BEAT_STRING过去 如果发送失败,就重新初始化一个socket |
| 121 | - boolean isSuccess = sendMsg(new Gson().toJson( | |
| 122 | - new SocketSendMsg().contractHeartBeatMsg(testRoomSn)) + END_SYMBOL); | |
| 119 | + final String s = new Gson().toJson( | |
| 120 | + new SocketSendMsg().contractHeartBeatMsg(testRoomSn)); | |
| 121 | + LogUtils.d(TAG, "heart beat get s"); | |
| 122 | + boolean isSuccess = sendMsg(s + END_SYMBOL); | |
| 123 | 123 | if (!isSuccess) { |
| 124 | 124 | LogUtils.d(TAG, "heart beat error restart"); |
| 125 | 125 | // clearConnect(); |
| ... | ... | @@ -208,6 +208,7 @@ public class SocketService1 extends BaseService { |
| 208 | 208 | |
| 209 | 209 | ///begin socket operation |
| 210 | 210 | private synchronized void initSocket() throws IOException {//初始化Socket |
| 211 | + LogUtils.d(TAG, "initSocket!!" + i); | |
| 211 | 212 | if (socket != null && socket.isConnected()) { |
| 212 | 213 | return; |
| 213 | 214 | } |
| ... | ... | @@ -220,15 +221,21 @@ public class SocketService1 extends BaseService { |
| 220 | 221 | }); |
| 221 | 222 | socket = new Socket(serverHost, serverPort); |
| 222 | 223 | readingMsg.setLength(0); |
| 224 | + LogUtils.d(TAG, "initSocket!!" + i++); | |
| 223 | 225 | } |
| 224 | 226 | |
| 227 | + | |
| 228 | + int i = 0; | |
| 229 | + | |
| 225 | 230 | private synchronized boolean sendMsg(String msg) { |
| 231 | + LogUtils.d(TAG, "send msg start**:" + i); | |
| 226 | 232 | if (socket == null || !socket.isConnected()) { |
| 233 | + LogUtils.d(TAG, "send msg end**" + i++); | |
| 227 | 234 | return false; |
| 228 | 235 | } |
| 229 | - if (!msg.contains("20025")) { | |
| 230 | - LogUtils.d(TAG, "send msg:" + msg); | |
| 231 | - } | |
| 236 | +// if (!msg.contains("20025")) { | |
| 237 | + LogUtils.d(TAG, "send msg:" + msg); | |
| 238 | +// } | |
| 232 | 239 | Socket soc = socket; |
| 233 | 240 | try { |
| 234 | 241 | if (!soc.isClosed() && !soc.isOutputShutdown()) { |
| ... | ... | @@ -244,10 +251,12 @@ public class SocketService1 extends BaseService { |
| 244 | 251 | releaseLastSocket(); |
| 245 | 252 | return false; |
| 246 | 253 | } |
| 254 | + LogUtils.d(TAG, "send msg end**" + i++); | |
| 247 | 255 | return true; |
| 248 | 256 | } |
| 249 | 257 | |
| 250 | 258 | private synchronized void releaseLastSocket() { |
| 259 | + LogUtils.d(TAG, "releaseLastSocket@@" + i); | |
| 251 | 260 | try { |
| 252 | 261 | if (null != socket) { |
| 253 | 262 | socket.close(); |
| ... | ... | @@ -256,12 +265,14 @@ public class SocketService1 extends BaseService { |
| 256 | 265 | LogUtils.d(TAG, "error" + e.getMessage()); |
| 257 | 266 | e.printStackTrace(); |
| 258 | 267 | } finally { |
| 268 | + LogUtils.d(TAG, "releaseLastSocket@@" + i); | |
| 259 | 269 | socket = null; |
| 260 | 270 | readingMsg.setLength(0); |
| 261 | 271 | } |
| 262 | 272 | } |
| 263 | 273 | |
| 264 | 274 | private synchronized boolean recvMsg(StringBuilder msg) { |
| 275 | + LogUtils.d(TAG, "recvMsg##" + i); | |
| 265 | 276 | if (null == socket || !socket.isConnected()) { |
| 266 | 277 | return false; |
| 267 | 278 | } |
| ... | ... | @@ -280,22 +291,28 @@ public class SocketService1 extends BaseService { |
| 280 | 291 | msg.setLength(0); |
| 281 | 292 | msg.append(readingMsg.substring(0, pos)); |
| 282 | 293 | readingMsg.delete(0, pos + END_SYMBOL.length()); |
| 294 | + LogUtils.d(TAG, "recvMsg##" + i++); | |
| 283 | 295 | return true; |
| 284 | 296 | } |
| 285 | 297 | } |
| 286 | 298 | } catch (IOException e) { |
| 287 | 299 | e.printStackTrace(); |
| 288 | 300 | releaseLastSocket(); |
| 301 | + LogUtils.d(TAG, "recvMsg false##" + i++); | |
| 289 | 302 | return false; |
| 290 | 303 | } |
| 291 | - | |
| 304 | + LogUtils.d(TAG, "recvMsg##" + i++); | |
| 292 | 305 | return false; |
| 293 | 306 | } |
| 294 | 307 | |
| 295 | 308 | private synchronized void clearConnect() { |
| 309 | + LogUtils.d(TAG, "clearConnect$$" + i); | |
| 310 | + | |
| 296 | 311 | mHandler.removeCallbacks(heartBeatRunnable); |
| 297 | 312 | sendRegister.set(false); |
| 298 | 313 | releaseLastSocket(); |
| 314 | + LogUtils.d(TAG, "recvMsg$$" + i++); | |
| 315 | + | |
| 299 | 316 | } |
| 300 | 317 | ///end socket operation |
| 301 | 318 | |
| ... | ... | @@ -354,7 +371,7 @@ public class SocketService1 extends BaseService { |
| 354 | 371 | LogUtils.d(TAG, "begin to recv msg"); |
| 355 | 372 | |
| 356 | 373 | sleepTime = READ_THREAD_DEFAULT_SLEEP_MTIME; |
| 357 | -recvMsgTag: | |
| 374 | + recvMsgTag: | |
| 358 | 375 | while (recvMsg(message)) { |
| 359 | 376 | sleepTime = sleepTime; |
| 360 | 377 | ... | ... |
| ... | ... | @@ -86,7 +86,7 @@ public class FangTangApplication extends TelinkApplication { |
| 86 | 86 | .setLogHeadSwitch(true)// 设置log头信息开关,默认为开 |
| 87 | 87 | .setLog2FileSwitch(true)// 打印log时是否存到文件的开关,默认关 |
| 88 | 88 | .setDir("/sdcard/ft_log/")// 当自定义路径为空时,写入应用的/cache/log/目录中 |
| 89 | - .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 | |
| 89 | + .setBorderSwitch(false)// 输出日志是否带边框开关,默认开 | |
| 90 | 90 | .setConsoleFilter(LogUtils.V)// log的控制台过滤器,和logcat过滤器同理,默认Verbose |
| 91 | 91 | .setFileFilter(LogUtils.V);// log文件过滤器,和logcat过滤器同理,默认Verbose |
| 92 | 92 | LogUtils.d(builder.toString()); | ... | ... |
Please
register
or
login
to post a comment