34
|
1 package remoteeditor.network;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
|
5 import java.nio.channels.SocketChannel;
|
|
6
|
|
7 import remoteeditor.command.REPCommand;
|
|
8
|
|
9 public class REPPacketSend {
|
|
10 SocketChannel socketchannel;
|
|
11
|
|
12 public REPPacketSend(SocketChannel sc){
|
|
13 socketchannel = sc;
|
|
14 }
|
|
15
|
|
16 public ByteBuffer pack(REPCommand command){
|
|
17
|
|
18 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
|
|
19 buffer.clear(); // position = 0
|
|
20 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
|
|
21 buffer.putInt(command.seq); buffer.putInt(command.lineno);
|
|
22 buffer.putInt(command.string.length()*2);
|
|
23 for(int i=0;i<command.string.length();i++) {
|
|
24 buffer.putChar(command.string.charAt(i));
|
|
25 }
|
|
26 buffer.flip(); // limit = current position, position = 0
|
|
27 return buffer;
|
|
28 }
|
|
29
|
|
30 public void send(REPCommand command){
|
|
31 try {
|
|
32 socketchannel.write(pack(command));
|
|
33 } catch (IOException e) {
|
|
34 e.printStackTrace();
|
|
35 }
|
|
36 }
|
|
37 }
|