📄 connection.java~1~
字号:
/**
* <p>Title: 即时通信性能测试</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 刘学
* @version 1.0
*/
package com.lazybug.net;
import com.lazybug.util.*;
import java.io.IOException;
public abstract class Connection
{
// public static int MAX_SIZE_BUFFER; //缓冲区大小
// public static final int MAX_NUM_PAGE = 40; //缓冲区页数
// public static final int MAX_SIZE_PAGE = 0x0200; //没有缓冲大小
protected int pageNum;
protected int pageSize;
protected byte[] buffer; //缓冲区
protected byte[] pagable;
protected int port;
protected int sequence = 65533;
protected int count_alloted; //已分配计数
protected int peak_value; //峰值
public Connection(int nPageNum, int nPageSize)
{
this.pageNum = nPageNum;
this.pageSize = nPageSize;
pagable = new byte[pageNum];
for(int i = 0; i < pageNum; i++ )
{
pagable[i] = 1;
}
}
/**
* 得到整个缓冲区的对象
* @return
*/
public byte[] getBuffer()
{
return buffer;
}
/**
* 缓冲区分配
*/
public synchronized int allocate()
{
for(int i = 0; i < pageNum; i++ )
{
if( pagable[i] == 1)
{
pagable[i] = 0;
count_alloted += 1;
peak_value = count_alloted>peak_value?count_alloted:peak_value;
//System.out.println("分配页【"+i+"】--"+this.toString());
return i;
}
}
return -1;
}
/**
* 释放缓冲区资源
*/
public synchronized void release(int page)
{
if( page >= 0 && page < pageNum)
{
//System.out.println("回收页【"+page+"】--"+this.toString());
pagable[page] = 1;
count_alloted -= 1;
}
}
public abstract int onReceiveFrom(Packet packet) throws IOException;
public abstract int onSendTo(Packet packet) throws IOException;
public abstract int getMaxPageLength();
public abstract int getBufferLength();
/**
* 写字符串到缓冲区
* @param buf
* @param offset
*/
public void writeString(byte[] buf, int offset)
{
for(int i = 0; i < buf.length; i++ )
{
buffer[offset++] = buf[i];
}
}
/**
* 读数据从缓冲区
* @param offset
* @param length
* @return
*/
public byte[] readString(int offset, int length)
{
byte[] buf = new byte[length];
for(int i = 0; i < length; i++)
{
buf[i] = buffer[offset++];
}
return buf;
}
public byte read8(int offset)
{
return buffer[offset];
}
public byte[] read16(int offset)
{
byte[] buf = new byte[2];
buf[0] = buffer[offset];
buf[1] = buffer[offset+1];
return buf;
}
/**
* 读4位字节
* @return
*/
public byte[] read32(int offset)
{
byte[] buf = new byte[4];
buf[0] = buffer[offset];
buf[1] = buffer[offset+1];
buf[2] = buffer[offset+2];
buf[3] = buffer[offset+3];
return buf;
}
public void write8(byte[] buf, int offset)
{
buffer[offset] = buf[0];
}
public void write8(byte buf, int offset)
{
buffer[offset] = buf;
}
public void write16(byte[] buf, int offset)
{
for( int i = 0; i < 2; i++ )
{
int k = buf.length - 1 - i;
buffer[offset+1 - i] = k>=0?buf[k]:0;
}
}
public void write32(byte[] buf, int offset)
{
for( int i = 0; i < 4; i++ )
{
int k = buf.length - 1 - i;
buffer[offset+3 - i] = k>=0?buf[k]:0;
}
}
/**
* 返回序列号
* @return
*/
public synchronized int getSequence()
{
if( sequence==0x10000 ) sequence = 0;
return sequence++;
}
/**
* 查询缓冲区页数
* @return
*/
public int getPageNum()
{
return this.pageNum;
}
/**
* 得到指定页的偏移量
*/
public int getOffset(int page)
{
return page*this.getMaxPageLength();
}
public String toString()
{
return "已分配的缓冲["+count_alloted+"]峰值["+peak_value+"]";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -