c\#socket 프레임 워 크 학습 SocketAsyncEventArgsPool 패키지
11987 단어 socket
public class SocketAsyncEventArgsPool{
//
private List<Int32> usedRecord;
//
private List<Int32> unUsedRecord;
//
private List<SocketAsyncEventArgsMetadata> pool;
//
private int capacity;
//
// private bool dynamic = false;
/** */
private void init() {
this.pool = new List<SocketAsyncEventArgsMetadata>(this.capacity);
this.usedRecord = new List<Int32>(this.capacity);
this.unUsedRecord = new List<Int32>(this.capacity);
for (int i = 0; i < this.capacity; i++) {
this.unUsedRecord.Add(i);
this.pool.Add(SocketAsyncEventArgsMetadata.valueOf(i));
}
}
/////////////////// ////////////////////////
/** **/
public int GetUsedCount()
{
return this.capacity - this.usedRecord.Count;
}
/** SocketAsyncEventArgs */
public SocketAsyncEventArgsMetadata Pop()
{
int index = 0;
lock(this){
if (GetUsedCount() <= 0)
{
extCapacity();
}
index = this.unUsedRecord[0];
this.unUsedRecord.RemoveAt(0);
this.usedRecord.Add(index);
return this.pool[index];
}
}
/** SocketAsyncEventArgs */
public void Push(SocketAsyncEventArgsMetadata args)
{
int index = 0;
lock (this)
{
index = args.GetIndex();
this.unUsedRecord.Add(index);
this.usedRecord.Remove(index);
}
}
/** */
private void extCapacity()
{
int minNewCapacity = 200;
int newCapacity = Math.Min(this.capacity, minNewCapacity);
// minNewCapacity
if (newCapacity > minNewCapacity)
{
newCapacity += minNewCapacity;
}
else {
//
newCapacity = 64;
while (newCapacity < minNewCapacity)
{
newCapacity <<= 1;
}
}
for (int i = this.capacity; i < newCapacity; i++) {
this.unUsedRecord.Add(i);
this.pool.Add(SocketAsyncEventArgsMetadata.valueOf(i));
}
this.capacity = newCapacity;
}
//getter
public int GetCapacity() {
return this.capacity;
}
/** */
public static SocketAsyncEventArgsPool valueOf(int maxCapacity)
{
SocketAsyncEventArgsPool result = new SocketAsyncEventArgsPool();
result.capacity = maxCapacity;
result.init();
return result;
}
} public class SocketAsyncEventArgsMetadata : SocketAsyncEventArgs
{
/** **/
private int index;
private SocketAsyncEventArgs args;
public static SocketAsyncEventArgsMetadata valueOf(int index) {
SocketAsyncEventArgsMetadata result = new SocketAsyncEventArgsMetadata();
result.index = index;
return result;
}
internal int GetIndex()
{
return this.index;
}
} 테스트 클래스:
class TestPool
{
private int count = 200;
public void test() {
SocketAsyncEventArgsPool pool = SocketAsyncEventArgsPool.valueOf(4);
for (int i = 0; i < count; i++) {
Thread th = new Thread(pop);
th.Start(pool);
}
}
private void pop(object msg)
{
((SocketAsyncEventArgsPool)msg).Pop();
}
} 이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
React 구성 요소에서 소켓 이벤트 리스너가 여러 번 실행됩니다.기본적이지만 종종 간과되는 사이드 프로젝트를 하면서 배운 것이 있습니다. 이 프로젝트에는 단순히 두 가지 주요 부분이 포함되어 있습니다. 프런트 엔드: 반응 및 재료 UI 백엔드: Express, Typescript...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.