00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package net.alhem.jsockets;
00025
00026 import java.io.IOException;
00027 import java.lang.Byte;
00028 import java.lang.StringBuffer;
00029 import java.net.InetSocketAddress;
00030 import java.nio.ByteBuffer;
00031 import java.nio.channels.SelectionKey;
00032 import java.nio.channels.SocketChannel;
00033
00038 public class TcpSocket extends Socket
00039 {
00040
00042 public TcpSocket(SocketHandler h,int ilen,int olen)
00043 {
00044 super(h);
00045 m_ilen = ilen;
00046 m_ibuf = ByteBuffer.allocate(ilen);
00047 m_olen = olen;
00048 m_obuf = ByteBuffer.allocate(olen);
00049 m_line = new StringBuffer(4096);
00050 }
00051 public TcpSocket(SocketHandler h)
00052 {
00053 this(h, 16384, 30000);
00054 }
00055 public Socket Create()
00056 {
00057 return new TcpSocket(Handler(), m_ilen, m_olen);
00058 }
00059 public void OnInitialOps()
00060 {
00061 if (Connecting())
00062 {
00063 GetKey().interestOps(SelectionKey.OP_CONNECT);
00064 }
00065 else
00066 {
00067 GetKey().interestOps(SelectionKey.OP_READ);
00068 }
00069 }
00070
00071 public void Open(String host,int port)
00072 {
00073 InetSocketAddress isa = new InetSocketAddress(host, port);
00074 try
00075 {
00076 SocketChannel sc = SocketChannel.open();
00077
00078 sc.configureBlocking(false);
00079 if (!sc.connect(isa))
00080 {
00081 SetConnecting();
00082 }
00083 else
00084 {
00085
00086 }
00087 attach(sc);
00088 }
00089 catch (java.io.IOException e)
00090 {
00091 Handler().LogError(this, "Open", 0, e.toString(), SocketHandler.LOG_LEVEL_FATAL);
00092 SetCloseAndDelete();
00093 }
00094 }
00095
00096 public void OnRead()
00097 {
00098 m_ibuf.clear();
00099 try
00100 {
00101 SocketChannel sc = (SocketChannel)GetChannel();
00102 int n = sc.read(m_ibuf);
00103 m_ibuf.flip();
00104 if (n == -1)
00105 {
00106 Handler().LogError(this, "OnRead", 0, "error while reading", SocketHandler.LOG_LEVEL_FATAL);
00107 SetCloseAndDelete();
00108 }
00109 else
00110 if (n == 0)
00111 {
00112 }
00113 else
00114 if (n > 0 && n == m_ibuf.limit() )
00115 {
00116 OnRawData(m_ibuf, n);
00117 }
00118 else
00119 {
00120 Handler().LogError(this, "OnRead", 0, "ibuf.limit != n (buffer cleared)", SocketHandler.LOG_LEVEL_ERROR);
00121 m_ibuf.clear();
00122 }
00123 }
00124 catch (Exception e)
00125 {
00126 Handler().LogError(this, "OnRead", 0, e.toString(), SocketHandler.LOG_LEVEL_FATAL);
00127 SetCloseAndDelete();
00128 }
00129 }
00130
00131 public void OnDelete()
00132 {
00133 SocketChannel sc = (SocketChannel)GetChannel();
00134 try
00135 {
00136 sc.close();
00137 }
00138 catch (IOException e)
00139 {
00140 Handler().LogError(this, "OnDelete", 0, e.toString(), SocketHandler.LOG_LEVEL_ERROR);
00141 }
00142 }
00143
00144 public void ReadLine()
00145 {
00146 for (int i = 0; i < m_ibuf.limit(); ++i)
00147 {
00148 byte b = m_ibuf.get( i );
00149 String aChar = new Character((char)b).toString();
00150 switch (b)
00151 {
00152 case 13:
00153 break;
00154 case 10:
00155 OnLine( m_line.toString() );
00156 m_line.delete(0, m_line.length());
00157 break;
00158 default:
00159 m_line.append(aChar);
00160 }
00161 }
00162 }
00163
00164 public void Send(String str)
00165 {
00166 byte[] bbuf = str.getBytes();
00167 SendBuf(bbuf, bbuf.length);
00168 }
00169
00170 public void SendBuf(byte[] bbuf,int l)
00171 {
00172 int n = 0;
00173 m_obuf.clear();
00174 m_obuf.put(bbuf);
00175 if (n == 0)
00176 {
00177 OnWrite();
00178 }
00179 }
00180
00181 public void OnWrite()
00182 {
00183 m_obuf.flip();
00184 try
00185 {
00186 SocketChannel sc = (SocketChannel)GetChannel();
00187 int n = sc.write(m_obuf);
00188 System.out.println(n + " bytes written");
00189 }
00190 catch (Exception e)
00191 {
00192 Handler().LogError(this, "OnWrite", 0, e.toString(), SocketHandler.LOG_LEVEL_FATAL);
00193 SetCloseAndDelete();
00194 }
00195 }
00196
00197
00198 private int m_ilen;
00199 private int m_olen;
00200 protected ByteBuffer m_ibuf;
00201 protected ByteBuffer m_obuf;
00202 protected StringBuffer m_line;
00203 }