📄 connectionthread.java
字号:
/* * Movino J2ME Client * Copyright (C) 2007 Johannes Berg * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package org.movino.connection;import java.io.IOException;import org.movino.Options;import org.movino.device.Camera;public class ConnectionThread extends Thread{ private MovinoConnection pendingConnection=null; private MovinoConnection currentConnection=null; private boolean isRunning; public static ConnectionThread currentThread; public ConnectionThread(){ currentThread=this; } public MovinoConnection newConnection(MovinoConnection conn){ pendingConnection = conn; return pendingConnection; } public void run(){ try{ isRunning=true; while(isRunning){ try{ MovinoConnection conn = pendingConnection; if(conn!=null){ pendingConnection=null; closeCurrent(); try{ conn.connect(); currentConnection = conn; } catch(Exception e){ conn.setError(e+": "+e.getMessage()); } } //do connection I/O conn = currentConnection; if(conn!=null){ try { if(conn.isAborted()){ closeCurrent(); } else if(conn.getState()==MovinoConnection.CONNECTION_STATE_OPENED){ conn.doServerHandshake(); } else if(conn.getState()==MovinoConnection.CONNECTION_STATE_HANDSHAKING){ conn.doClientHandshake(); } else if(conn.getState()==MovinoConnection.CONNECTION_STATE_CONNECTED){ if(conn.isIndataAvailable()){ MovinoPacket mp = conn.readPacket(); packetArrived(mp,conn); } conn.flushPackets(); } } catch(IOException e){ conn.setError(e+": "+e.getMessage()); closeCurrent(); } } Thread.sleep(10); } catch (InterruptedException e){} } closeCurrent(); } catch(Throwable t){} } public void packetArrived(MovinoPacket mp, MovinoConnection mc){ if(mp==null) return; switch(mp.getType()){ case MovinoPacket.MOVINO_REPLY_PING: mc.sendPacket(new MovinoPacket(MovinoPacket.MOVINO_DATA_GARBAGE)); break; } } public void shutdownConnections(){ isRunning=false; this.interrupt(); Thread.yield(); long start=System.currentTimeMillis(); while(System.currentTimeMillis()-start<500 && currentConnection!=null){ try { Thread.sleep(20); } catch (InterruptedException e){} } } private void closeCurrent(){ try{ MovinoConnection conn = currentConnection; if(conn!=null){ if(MovinoConnection.currentConnection==currentConnection){ MovinoConnection.currentConnection=null; } conn.writePacket(new MovinoPacket(MovinoPacket.MOVINO_DATA_STREAM_CLOSED)); conn.close(); currentConnection=null; } } catch(IOException e){} } public MovinoConnection getCurrent(){ return currentConnection; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -