Mercurial > hg > RemoteEditor > Eclipse
view src/remoteeditor/network/REPPacketReceive.java @ 151:1768e68ba98e
*** empty log message ***
author | pin |
---|---|
date | Tue, 05 Aug 2008 15:09:39 +0900 |
parents | a78e0429bee2 |
children | 563057fe244e |
line wrap: on
line source
package remoteeditor.network; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import remoteeditor.command.REPCommand; import remoteeditor.command.REPCommandEvent; import remoteeditor.command.REPCommandListener; public class REPPacketReceive implements Runnable{ SocketChannel socketchannel; private int HEADER_SIZE = 24; private REPCommandListener commandlistener; public REPPacketReceive(SocketChannel sc){ socketchannel = sc; } private REPCommand unpack() { //System.out.println("test1"); ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); long len = 0; header.clear(); try { len = socketchannel.read(header); } catch (IOException e1) { e1.printStackTrace(); } // limit = read length if (len !=HEADER_SIZE) { System.out.println("this can't happen"); // this can't happen } header.rewind(); // position = 0 String text = ""; int cmd = header.getInt(); int sid = header.getInt(); int eid = header.getInt(); int seqid = header.getInt(); int lineno = header.getInt(); int textsiz = header.getInt()/2; ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz*2); try { len = socketchannel.read(textBuffer); } catch (IOException e1) { e1.printStackTrace(); } // limit = read length if (len != textsiz * 2) { // this can't happen System.out.println("����"); } textBuffer.rewind(); for(int i=0;i<textsiz;i++) { text +=textBuffer.getChar(); } String string = text; //System.out.println(string); REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); System.out.println("received command: " + repcommand.toString()); return repcommand; } public REPCommand unpackUConv() { ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); long len = 0; header.clear(); try { len = socketchannel.read(header); if(len == -1){ socketchannel.close(); return null; }else if(len == 0){ return null; } } catch (IOException e1) { e1.printStackTrace(); } // limit = read length if (len !=HEADER_SIZE) { System.out.println("てす"); // this can't happen } header.rewind(); // position = 0 int cmd = header.getInt(); int sid = header.getInt(); int eid = header.getInt(); int seqid = header.getInt(); int lineno = header.getInt(); int textsiz = header.getInt(); //int tmptextsiz = header.getInt(); //int textsiz = (tmptextsiz /5) + (tmptextsiz % 5); ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); try { len = socketchannel.read(textBuffer); if(len == -1){ socketchannel.close(); return null; } } catch (IOException e1) { e1.printStackTrace(); } // limit = read length if (len != textsiz) { // this can't happen System.out.println("あと"); } textBuffer.rewind(); //Decode UTF-8 to System Encoding(UTF-16) Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer cb = null; try { cb = decoder.decode(textBuffer); } catch (CharacterCodingException e) { e.printStackTrace(); } cb.rewind(); String string = cb.toString(); textsiz = string.length(); if(textsiz > 2){ System.out.println(string); } //System.out.println("CharBuffer size: " +cb.length()); //System.out.println("textsize: " +textsiz); //System.out.println(cb.toString()); REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); System.out.println("UnPack Packet: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string); System.out.println("received command: " + repcommand.toString()); return repcommand; } public void addCommandListener(REPCommandListener listener){ commandlistener = listener; Thread th = new Thread(this); th.start(); } public void run() { while(socketchannel.isConnected()){ //unpack(); // commandlistener.CommandReceived(new REPCommandEvent(unpack())); commandlistener.CommandReceived(new REPCommandEvent(unpackUConv())); } } }