
internal sealed class MySocketAsyncEventArgs : SocketAsyncEventArgs
{
internal string UID;
private string Property;
internal MySocketAsyncEventArgs(string property)
{
this.Property = property;
}
}

1
2 internal sealed class SocketAsyncEventArgsWithId
3 {
4 private string uid;
5 internal string UID
6 {
7 get { return uid; }
8 set {
9 uid = value;
10 ReceiveSAEA.UID = value;
11 SendSAEA.UID = value;
12 }
13 }
14 internal MySocketAsyncEventArgs ReceiveSAEA;
15 internal MySocketAsyncEventArgs SendSAEA;
16
17 internal SocketAsyncEventArgsWithId()
18 {
19 ReceiveSAEA = new MySocketAsyncEventArgs("Receive");
20 SendSAEA = new MySocketAsyncEventArgs("Send");
21 }
22

1
2 internal sealed class SocketAsyncEventArgsPool
3 {
4 internal Stack<SocketAsyncEventArgsWithId> pool;
5 internal IDictionary<string, SocketAsyncEventArgsWithId> busypool;
6 internal SocketAsyncEventArgsPool(Int32 capacity)
7 {
8 this.pool = new Stack<SocketAsyncEventArgsWithId>(capacity);
9 this.busypool = new Dictionary<string, SocketAsyncEventArgsWithId>();
10 }
11 internal Int32 Count
12 {
13 get { return this.pool.Count; }
14 }
15 internal SocketAsyncEventArgsWithId Pop()
16 {
17 lock (this.pool)
18 {
19 SocketAsyncEventArgsWithId argsid = this.pool.Pop();
20 return argsid;
21 }
22 }
23 internal void Push(SocketAsyncEventArgsWithId item)
24 {
25 if (item == null)
26 throw new ArgumentNullException("SocketAsyncEventArgs对象为空");
27 lock (this.pool)
28 {
29 if(busypool.Keys.Count!=0)
30 if(busypool.Keys.Contains(item.UID))
31 busypool.Remove(item.UID);
32 item.UID = null;
33 this.pool.Push(item);
34 }
35 }
36 internal SocketAsyncEventArgsWithId FindByUID(string uid)
37 {
38 SocketAsyncEventArgsWithId SI = null;
39 foreach (string key in busypool.Keys)
40 {
41 if (key == uid)
42 {
43 SI = busypool[uid];
44 break;
45 }
46 }
47 return SI;
48 }
49

1
2 internal sealed class BufferManager
3 {
4 private Byte[] buffer;
5 private Int32 bufferSize;
6 private Int32 numSize;
7 private Int32 currentIndex;
8 private Stack<Int32> freeIndexPool;
9
10 internal BufferManager(Int32 numSize, Int32 bufferSize)//分别为缓冲区的总数和分配给每一个连接的大小
11 {
12 this.bufferSize = bufferSize;
13 this.numSize = numSize;
14 this.currentIndex = 0;
15 this.freeIndexPool = new Stack<Int32>();
16 }
17
18 internal void FreeBuffer(SocketAsyncEventArgs args)
19 {
20 this.freeIndexPool.Push(args.Offset);
21 args.SetBuffer(null, 0, 0);
22 }
23 internal void InitBuffer()
24 {
25 this.buffer = new Byte[this.numSize];
26 }
27 internal Boolean SetBuffer(SocketAsyncEventArgs args)
28 {
29 if (this.freeIndexPool.Count > 0)
30 {
31 args.SetBuffer(this.buffer, this.freeIndexPool.Pop(), this.bufferSize);
32 }
33 else
34 {
35 if ((this.numSize - this.bufferSize) < this.currentIndex)
36 {
37 return false;
38 }
39 args.SetBuffer(this.buffer, this.currentIndex, this.bufferSize);
40 this.currentIndex += this.bufferSize;
41 }
42 return true;
43 }
44

1
2 public class RequestHandler
3 {
4 private string temp = string.Empty;
5 public string[] GetActualString(string input)
6 {
7 return GetActualString(input, null);
8 }
9 private string[] GetActualString(string input, List<string> outputList)
10 {
11 if (outputList == null)
12 outputList = new List<string>();
13 if (!String.IsNullOrEmpty(temp))
14 input = temp + input;
15 string output = "";
16 string pattern = @"(?<=^\[length=)(\d+)(?=\])";
17 int length;
18 if (Regex.IsMatch(input, pattern))
19 {
20 Match m = Regex.Match(input, pattern);
21 length = Convert.ToInt32(m.Groups[0].Value);
22 int startIndex = input.IndexOf(']') + 1;
23 output = input.Substring(startIndex);
24 if (output.Length == length)
25 {
26 outputList.Add(output);
27 temp = "";
28 }
29 else if (output.Length < length)
30 {
31 temp = input;
32 }
33 else if (output.Length > length)
34 {
35 output = output.Substring(0, length);
36 outputList.Add(output);
37 temp = "";
38 input = input.Substring(startIndex + length);
39 GetActualString(input, outputList);
40 }
41 }
42 else
43 {
44 temp = input;
45 }
46 return outputList.ToArray();
47 }
48
相关文章: http://www.cnblogs.com/jeriffe/articles/1407603.html
http://www.cnblogs.com/chuncn/archive/2009/06/22/1508018.html
http://www.cnblogs.com/dabing/archive/2009/07/10/1520586.html
http://www.cnblogs.com/JimmyZhang/archive/2008/09/16/1291854.html
附上源代码:下载源代码 http://files.cnblogs.com/niuchenglei/SocketLib.zip