corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 using System;
2 using System.Collections.Generic;
3 using System.Net;
4  
5 namespace OpenMetaverse
6 {
7 // this class encapsulates a single packet that
8 // is either sent or received by a UDP socket
9 public class UDPPacketBuffer
10 {
11 /// <summary>Size of the byte array used to store raw packet data</summary>
12 public const int BUFFER_SIZE = 4096;
13 /// <summary>Raw packet data buffer</summary>
14 public readonly byte[] Data;
15 /// <summary>Length of the data to transmit</summary>
16 public int DataLength;
17 /// <summary>EndPoint of the remote host</summary>
18 public EndPoint RemoteEndPoint;
19  
20 /// <summary>
21 /// Create an allocated UDP packet buffer for receiving a packet
22 /// </summary>
23 public UDPPacketBuffer()
24 {
25 Data = new byte[UDPPacketBuffer.BUFFER_SIZE];
26 // Will be modified later by BeginReceiveFrom()
27 RemoteEndPoint = new IPEndPoint(Settings.BIND_ADDR, 0);
28 }
29  
30 /// <summary>
31 /// Create an allocated UDP packet buffer for sending a packet
32 /// </summary>
33 /// <param name="endPoint">EndPoint of the remote host</param>
34 public UDPPacketBuffer(IPEndPoint endPoint)
35 {
36 Data = new byte[UDPPacketBuffer.BUFFER_SIZE];
37 RemoteEndPoint = endPoint;
38 }
39  
40 /// <summary>
41 /// Create an allocated UDP packet buffer for sending a packet
42 /// </summary>
43 /// <param name="endPoint">EndPoint of the remote host</param>
44 /// <param name="bufferSize">Size of the buffer to allocate for packet data</param>
45 public UDPPacketBuffer(IPEndPoint endPoint, int bufferSize)
46 {
47 Data = new byte[bufferSize];
48 RemoteEndPoint = endPoint;
49 }
50 }
51  
52 /// <summary>
53 /// Object pool for packet buffers. This is used to allocate memory for all
54 /// incoming and outgoing packets, and zerocoding buffers for those packets
55 /// </summary>
56 public class PacketBufferPool : ObjectPoolBase<UDPPacketBuffer>
57 {
58 private IPEndPoint EndPoint;
59  
60 /// <summary>
61 /// Initialize the object pool in client mode
62 /// </summary>
63 /// <param name="endPoint">Server to connect to</param>
64 /// <param name="itemsPerSegment"></param>
65 /// <param name="minSegments"></param>
66 public PacketBufferPool(IPEndPoint endPoint, int itemsPerSegment, int minSegments)
67 : base()
68 {
69 EndPoint = endPoint;
70 Initialize(itemsPerSegment, minSegments, true, 1000 * 60 * 5);
71 }
72  
73 /// <summary>
74 /// Initialize the object pool in server mode
75 /// </summary>
76 /// <param name="itemsPerSegment"></param>
77 /// <param name="minSegments"></param>
78 public PacketBufferPool(int itemsPerSegment, int minSegments)
79 : base()
80 {
81 EndPoint = null;
82 Initialize(itemsPerSegment, minSegments, true, 1000 * 60 * 5);
83 }
84  
85 /// <summary>
86 /// Returns a packet buffer with EndPoint set if the buffer is in
87 /// client mode, or with EndPoint set to null in server mode
88 /// </summary>
89 /// <returns>Initialized UDPPacketBuffer object</returns>
90 protected override UDPPacketBuffer GetObjectInstance()
91 {
92 if (EndPoint != null)
93 // Client mode
94 return new UDPPacketBuffer(EndPoint);
95 else
96 // Server mode
97 return new UDPPacketBuffer();
98 }
99 }
100  
101 public static class Pool
102 {
103 public static PacketBufferPool PoolInstance;
104  
105 /// <summary>
106 /// Default constructor
107 /// </summary>
108 static Pool()
109 {
110 PoolInstance = new PacketBufferPool(new IPEndPoint(Settings.BIND_ADDR, 0), 16, 1);
111 }
112  
113 /// <summary>
114 /// Check a packet buffer out of the pool
115 /// </summary>
116 /// <returns>A packet buffer object</returns>
117 public static WrappedObject<UDPPacketBuffer> CheckOut()
118 {
119 return PoolInstance.CheckOut();
120 }
121 }
122 }