了解Maclean Liu|向Maclean Liu提问 Oracle ALLSTARS 全明星(群内有多位Oracle高级售后support,N位OCM和ACE) QQ群 # QQ群号:23549328 # 已经升级到 2000人群,空位多多。欢迎有一定基础的Oracle骨友加入,现在入群需要经过Maclean的技术面试,欢迎面试,请加QQ号:47079569 为好友参加面试 2群基础群 适合刚入门的同学,会共享最佳入门实践和资料 QQ群 # QQ群号:171092051 # 已经升级到 500人的超级群,空位多多,无需面试

Oracle网络TNS协议的几个基础类描述(revised)

首先是接口SQLnetDef,定义了参数
public interface SQLnetDef
{

public static final boolean DEBUG = false;
public static final boolean ASSERT = false;
public static final int NSPTCN = 1;
public static final int NSPTAC = 2;
public static final int NSPTAK = 3;
public static final int NSPTRF = 4;
public static final int NSPTRD = 5;
public static final int NSPTDA = 6;
public static final int NSPTNL = 7;
public static final int NSPTAB = 9;
public static final int NSPTRS = 11;
public static final int NSPTMK = 12;
public static final int NSPTAT = 13;
public static final int NSPTCNL = 14;
public static final int NSPTHI = 19;
public static final byte NSPHDLEN = 0;
public static final byte NSPHDPSM = 2;
public static final byte NSPHDTYP = 4;
public static final byte NSPHDFLGS = 5;
public static final byte NSPHDHSM = 6;
public static final byte NSPSIZHD = 8;
public static final byte NO_HEADER_FLAGS = 0;
public static final byte NSPCNVSN = 8;
public static final byte NSPCNLOV = 10;
public static final byte NSPCNOPT = 12;
public static final byte NSPCNSDU = 14;
public static final byte NSPCNTDU = 16;
public static final byte NSPCNNTC = 18;
public static final byte NSPCNTNA = 20;
public static final byte NSPCNONE = 22;
public static final byte NSPCNLEN = 24;
public static final byte NSPCNOFF = 26;
public static final byte NSPCNMXC = 28;
public static final byte NSPCNFL0 = 32;
public static final byte NSPCNFL1 = 33;
public static final byte NSPCNDAT = 34;
public static final int NSPMXCDATA = 230;
public static final int NSINAWANTED = 1;
public static final int NSINAINTCHG = 2;
public static final int NSINADISABLEFORCONNECTION = 4;
public static final int NSINANOSERVICES = 8;
public static final int NSINAREQUIRED = 16;
public static final int NSINAAUTHWANTED = 32;
public static final byte NSPACVSN = 8;
public static final byte NSPACOPT = 10;
public static final byte NSPACSDU = 12;
public static final byte NSPACTDU = 14;
public static final byte NSPACONE = 16;
public static final byte NSPACLEN = 18;
public static final byte NSPACOFF = 20;
public static final byte NSPACFL0 = 22;
public static final byte NSPACFL1 = 23;
public static final byte NSPRFURS = 8;
public static final byte NSPRFSRS = 9;
public static final byte NSPRFLEN = 10;
public static final byte NSPRFDAT = 12;
public static final byte NSPRDLEN = 8;
public static final byte NSPRDDAT = 10;
public static final int NSPDAFLG = 8;
public static final int NSPDADAT = 10;
public static final int NSPDAFZER = 0;
public static final int NSPDAFTKN = 1;
public static final int NSPDAFRCF = 2;
public static final int NSPDAFCFM = 4;
public static final int NSPDAFRSV = 8;
public static final int NSPDAFMOR = 32;
public static final int NSPDAFEOF = 64;
public static final int NSPDAFIMM = 128;
public static final int NSPDAFRTS = 256;
public static final int NSPDAFRNT = 512;
public static final int NSPMKTYP = 8;
public static final int NSPMKODT = 9;
public static final int NSPMKDAT = 10;
public static final int NSPMKTD0 = 0;
public static final int NSPMKTD1 = 1;
public static final byte NIQBMARK = 1;
public static final byte NIQRMARK = 2;
public static final byte NIQIMARK = 3;
public static final int NSPDFSDULN = 2048;
public static final int NSPMXSDULN = 32767;
public static final int NSPMNSDULN = 512;
public static final int NSPDFTDULN = 32767;
public static final int NSPMXTDULN = 32767;
public static final int NSPMNTDULN = 255;
public static final int NSPINSDULN = 255;
public static final String TCP_NODELAY_STR = "TCP.NODELAY";
public static final String TCP_CONNTIMEOUT_STR = "oracle.net.CONNECT_TIMEOUT";
public static final String TCP_READTIMEOUT_STR = "oracle.net.READ_TIMEOUT";
public static final int TCP_NODELAY_OFF = 0;
public static final int TCP_KEEPALIVE_OFF = 1;
public static final int TCP_CONNTIMEOUT_OFF = 2;
public static final int TCP_READTIMEOUT_OFF = 3;
public static final int ORACLE_NET_NTMINOPT = 0;
public static final int ORACLE_NET_READ_TIMEOUT = 1;
public static final int ORACLE_NET_NTMAXOPT = 10;
}
Session 类描述:
package oracle.net.ns;

import java.io.*;
import oracle.net.ano.Ano;
import oracle.net.nt.ConnOption;
import oracle.net.nt.NTAdapter;

// Referenced classes of package oracle.net.ns:
//            NetException, SQLnetDef, NetInputStream, NetOutputStream,
//            ClientProfile

public class SessionAtts
implements SQLnetDef
{

public SessionAtts(int i, int j)
{
sdu = i;
tdu = j;
}

public int getANOFlags()
{
int i = 1;
if(ano != null)
i = ano.getNAFlags();
return i;
}

public InputStream getInputStream()
{
return nsInputStream;
}

public NTAdapter getNTAdapter()
{
return nt;
}

public OutputStream getOutputStream()
{
return nsOutputStream;
}

public int getSDU()
{
return sdu;
}

public int getTDU()
{
return tdu;
}

public void print()
{
System.out.println("Session Attributes: ");
System.out.println("sdu            : " + sdu);
System.out.println("tdu            : " + tdu);
System.out.println("nt             : " + nt);
System.out.println("ntInputStream  : " + ntInputStream);
System.out.println("ntOutputStream : " + ntOutputStream);
System.out.println("nsInputStream  : " + nsInputStream);
System.out.println("nsOutputStream : " + nsOutputStream);
System.out.println("profile        : " + profile);
System.out.println("cOption        : " + cOption);
System.out.println("onBreakReset   : " + onBreakReset);
System.out.println("dataEOF        : " + dataEOF);
System.out.println("connected      : " + connected);
}

public void setSDU(int i)
{
if(i <= 0)
sdu = 2048;
else
if(i > 32767)
sdu = 32767;
else
if(i < 512)
sdu = 512;
else
sdu = i;
}

public void setTDU(int i)
{
if(i <= 0)
tdu = 32767;
else
if(i > 32767)
tdu = 32767;
else
if(i < 255)
tdu = 255;
else
tdu = i;
}

public void turnEncryptionOn(NetInputStream netinputstream, NetOutputStream netoutputstream)
throws NetException
{
if(netinputstream != null && netoutputstream != null)
{
nsInputStream = netinputstream;
nsOutputStream = netoutputstream;
} else
{
throw new NetException(300);
}
}

private int sdu;
private int tdu;
protected NTAdapter nt;
protected InputStream ntInputStream;
protected OutputStream ntOutputStream;
protected NetInputStream nsInputStream;
protected NetOutputStream nsOutputStream;
protected ConnOption cOption;
protected boolean dataEOF;
protected boolean connected;
public boolean onBreakReset;
public ClientProfile profile;
public Ano ano;
public boolean anoEnabled;
public boolean isEncryptionActive;
public boolean isChecksumActive;
public boolean areEncryptionAndChecksumActive;
}
基础Packet类型:
package oracle.net.ns;

import java.io.*;
import oracle.net.nl.RepConversion;

// Referenced classes of package oracle.net.ns:
//            NetException, NetInputStream, SQLnetDef, SessionAtts

public class Packet
implements SQLnetDef
{

public Packet(Packet packet)
{
this(packet.sAtts);
length = packet.length;
type = packet.type;
flags = packet.flags;
dataLen = packet.dataLen;
dataOff = packet.dataOff;
buffer = packet.buffer;
}

public Packet(SessionAtts sessionatts)
{
header = new byte[8];
sAtts = sessionatts;
sdu = sessionatts.getSDU();
tdu = sessionatts.getTDU();
}

public Packet(SessionAtts sessionatts, int i)
{
this(sessionatts);
createBuffer(i);
}

public Packet(SessionAtts sessionatts, int i, int j, int k)
{
this(sessionatts);
createBuffer(i, j, k);
}

protected void createBuffer(int i)
{
buffer = new byte[i];
buffer[0] = (byte)(i / 256);
buffer[1] = (byte)(i % 256);
}

protected void createBuffer(int i, int j, int k)
{
buffer = new byte[i];
buffer[0] = (byte)(i / 256);
buffer[1] = (byte)(i % 256);
buffer[5] = (byte)k;
buffer[4] = (byte)j;
}

protected void dump(byte abyte0[], int i, int j)
{
int k = 0;
System.out.println("Packet dump");
System.out.println("buffer.length=" + abyte0.length);
System.out.println("offset       =" + i);
System.out.println("len          =" + j);
for(int l = i; l < j; l +=  8 )
{
System.out.print("|");
for(int i1 = 0; i1 < 8 && k < j - 1; i1++)
{
k = l + i1;
RepConversion.printInHex(abyte0[k]);
System.out.print(" ");
}

System.out.println("|");
}

System.out.println("finish dump");
}

protected void extractData()
throws IOException, NetException
{
if(dataLen <= 0)
data = new String();
else
if(length > dataOff)
{
data = new String(buffer, 0, dataOff, dataLen);
} else
{
byte abyte0[] = new byte[dataLen];
if(sAtts.nsInputStream.read(abyte0) < 0)
throw new NetException(0);
data = new String(abyte0, 0);
}
}

protected String getData()
{
return data;
}

protected void receive()
throws IOException, NetException
{
int i;
for(i = 0; i < header.length;)
try
{
if((i += sAtts.ntInputStream.read(header, i, header.length - i)) <= 0)
throw new NetException(0);
}
catch(InterruptedIOException _ex)
{
throw new NetException(504);
}

length = header[0] & 0xff;
length <<= 8;
length |= header[1] & 0xff;
type = header[4];
flags = header[5];
if(type > 19)
throw new NetException(204);
if(length > 32767 || length > sdu)
throw new NetException(203);
if(length < 8   )
throw new NetException(207);
buffer[5] = (byte)flags;
buffer[4] = (byte)type;
while(i < length)
try
{
if((i += sAtts.ntInputStream.read(buffer, i, length - i)) <= 0)
throw new NetException(0);
}
catch(InterruptedIOException _ex) { }
}

protected void send()
throws IOException
{
synchronized(sAtts.ntOutputStream)
{
sAtts.ntOutputStream.write(buffer, 0, buffer.length);
}
}

private int buffer2send;
protected int sdu;
protected int tdu;
protected int length;
public int type;
protected int flags;
protected int dataLen;
protected int dataOff;
protected String data;
protected byte buffer[];
protected byte header[];
public SessionAtts sAtts;
}
Connect Packet 连接包描述:
package oracle.net.ns;

import java.io.IOException;
import java.io.PrintStream;
import oracle.net.nt.ConnOption;

// Referenced classes of package oracle.net.ns:
//            Packet, NetOutputStream, SQLnetDef, SessionAtts

public class ConnectPacket extends Packet
implements SQLnetDef
{

public ConnectPacket(SessionAtts sessionatts)
{
super(sessionatts);
super.data = sessionatts.cOption.conn_data.toString();
super.dataLen = super.data != null ? super.data.length() : 0;
connDataOflow = super.dataLen > 230;
int i = connDataOflow ? 34 : 34 + super.dataLen;
createBuffer(i, 1, 0);
super.buffer[8] = 1;
super.buffer[9] = 52;
super.buffer[10] = 1;
super.buffer[11] = 44;
super.buffer[12] = 0;
super.buffer[13] = 0;
super.buffer[14] = (byte)(super.sdu / 256);
super.buffer[15] = (byte)(super.sdu % 256);
super.buffer[16] = (byte)(super.tdu / 256);
super.buffer[17] = (byte)(super.tdu % 256);
super.buffer[18] = 79;
super.buffer[19] = -104;
super.buffer[22] = 0;
super.buffer[23] = 1;
super.buffer[24] = (byte)(super.dataLen / 256);
super.buffer[25] = (byte)(super.dataLen % 256);
super.buffer[27] = 34;
if(!sessionatts.anoEnabled)
super.buffer[32] = super.buffer[33] = 4;
else
super.buffer[32] = super.buffer[33] = (byte)sessionatts.getANOFlags();
if(!connDataOflow && super.dataLen > 0)
super.data.getBytes(0, super.dataLen, super.buffer, 34);
}

protected void send()
throws IOException
{
super.send();
if(connDataOflow)
{
byte abyte0[] = new byte[super.dataLen];
super.data.getBytes(0, super.dataLen, abyte0, 0);
super.sAtts.nsOutputStream.write(abyte0);
super.sAtts.nsOutputStream.flush();
}
}

private boolean connDataOflow;
}
接受包类:
package oracle.net.ns;

import java.io.IOException;
import java.io.PrintStream;

// Referenced classes of package oracle.net.ns:
//            Packet, NetException, SQLnetDef, SessionAtts

public class AcceptPacket extends Packet
implements SQLnetDef
{

public AcceptPacket(Packet packet)
throws IOException, NetException
{
super(packet);
version = super.buffer[8] & 0xff;
version <<= 8;
version |= super.buffer[9] & 0xff;
options = super.buffer[10] & 0xff;
options <<= 8;
options |= super.buffer[11] & 0xff;
sduSize = super.buffer[12] & 0xff;
sduSize <<= 8;
sduSize |= super.buffer[13] & 0xff;
tduSize = super.buffer[14] & 0xff;
tduSize <<= 8;
tduSize |= super.buffer[15] & 0xff;
myHWByteOrder = super.buffer[16] & 0xff;
myHWByteOrder <<= 8;
myHWByteOrder |= super.buffer[17] & 0xff;
super.dataLen = super.buffer[18] & 0xff;
super.dataLen <<= 8;
super.dataLen |= super.buffer[19] & 0xff;
super.dataOff = super.buffer[20] & 0xff;
super.dataOff <<= 8;
super.dataOff |= super.buffer[21] & 0xff;
flag0 = super.buffer[22];
flag1 = super.buffer[23];
extractData();
super.sAtts.setSDU(sduSize);
super.sAtts.setTDU(tduSize);
if(tduSize < sduSize)
super.sAtts.setSDU(tduSize);
}

protected int version;
protected int options;
protected int sduSize;
protected int tduSize;
protected int myHWByteOrder;
protected int flag0;
protected int flag1;
}
Data Packet 数据包类:
package oracle.net.ns;

import java.io.*;

// Referenced classes of package oracle.net.ns:
//            Packet, NetException, SQLnetDef, SessionAtts

public class DataPacket extends Packet
implements SQLnetDef
{

public DataPacket(SessionAtts sessionatts)
{
this(sessionatts, sessionatts.getSDU());
}

public DataPacket(SessionAtts sessionatts, int i)
{
super(sessionatts, i, 6, 0);
isBufferFull = false;
isBufferEmpty = false;
availableBytesToSend = 0;
availableBytesToRead = 0;
initialize(i);
}

protected int getDataFromBuffer(byte abyte0[], int i, int j)
throws NetException
{
int k = super.length - pktOffset > j ? j : super.length - pktOffset;
if(k > 0)
{
System.arraycopy(super.buffer, pktOffset, abyte0, i, k);
pktOffset += k;
isBufferEmpty = pktOffset == super.length;
availableBytesToRead = (super.dataOff + super.dataLen) - pktOffset;
}
return k;
}

protected void initialize(int i)
{
super.dataOff = pktOffset = 10;
super.dataLen = i - super.dataOff;
dataFlags = 0;
}

protected int putDataInBuffer(byte abyte0[], int i, int j)
throws IOException
{
int k = super.buffer.length - pktOffset > j ? j : super.buffer.length - pktOffset;
if(k > 0)
{
System.arraycopy(abyte0, i, super.buffer, pktOffset, k);
pktOffset += k;
isBufferFull = pktOffset == super.buffer.length;
availableBytesToSend = super.dataOff >= pktOffset ? 0 : pktOffset - super.dataOff;
}
return k;
}

protected void receive()
throws IOException, NetException
{
super.receive();
super.dataOff = pktOffset = 10;
super.dataLen = super.length - super.dataOff;
dataFlags = super.buffer[8] & 0xff;
dataFlags <<= 8;
dataFlags |= super.buffer[9] & 0xff;
if((dataFlags & 0x40) != 0)
super.sAtts.dataEOF = true;
if(super.type == 6 && super.dataLen == 0)
super.type = 7;
}

protected void send()
throws IOException
{
send(0);
}

protected void send(int i)
throws IOException
{
super.buffer[8] = (byte)(i / 256);
super.buffer[9] = (byte)(i % 256);
setBufferLength(pktOffset);
synchronized(super.sAtts.ntOutputStream)
{
super.sAtts.ntOutputStream.write(super.buffer, 0, pktOffset);
}
pktOffset = 10;
availableBytesToSend = 0;
isBufferFull = false;
}

protected void setBufferLength(int i)
throws NetException
{
super.buffer[0] = (byte)(i / 256);
super.buffer[1] = (byte)(i % 256);
}

static final boolean DEBUG2 = false;
protected int pktOffset;
protected int dataFlags;
protected boolean isBufferFull;
protected boolean isBufferEmpty;
protected int availableBytesToSend;
protected int availableBytesToRead;
}
以上类描述可以通过反编译jdbc包获得,实际数据结构是通用的即在C或其他语言下也是类似结构,只是实现略有不同。

posted on 2010-11-05 11:01  Oracle和MySQL  阅读(510)  评论(0编辑  收藏  举报

导航