diff options
Diffstat (limited to '')
-rw-r--r-- | ThirdParty/3Di/LoadBalancer/TcpServer.cs | 98 |
1 files changed, 61 insertions, 37 deletions
diff --git a/ThirdParty/3Di/LoadBalancer/TcpServer.cs b/ThirdParty/3Di/LoadBalancer/TcpServer.cs index 6d45304..9722a1b 100644 --- a/ThirdParty/3Di/LoadBalancer/TcpServer.cs +++ b/ThirdParty/3Di/LoadBalancer/TcpServer.cs | |||
@@ -31,42 +31,58 @@ using System.Net; | |||
31 | using System.Net.Sockets; | 31 | using System.Net.Sockets; |
32 | using System.Threading; | 32 | using System.Threading; |
33 | 33 | ||
34 | namespace OpenSim.ApplicationPlugins.LoadBalancer { | 34 | namespace OpenSim.ApplicationPlugins.LoadBalancer |
35 | 35 | { | |
36 | public class StateObject { | 36 | public class StateObject |
37 | public Socket workSocket = null; | 37 | { |
38 | public const int BufferSize = 2048; | 38 | public const int BufferSize = 2048; |
39 | public byte[] buffer = new byte[BufferSize]; | 39 | public byte[] buffer = new byte[BufferSize]; |
40 | public MemoryStream ms_ptr = new MemoryStream(); | ||
41 | public InternalPacketHeader header = null; | 40 | public InternalPacketHeader header = null; |
41 | public MemoryStream ms_ptr = new MemoryStream(); | ||
42 | public Socket workSocket = null; | ||
42 | } | 43 | } |
43 | 44 | ||
44 | public class AsynchronousSocketListener { | 45 | public class AsynchronousSocketListener |
45 | public static string data = null; | 46 | { |
46 | public static ManualResetEvent allDone = new ManualResetEvent(false); | 47 | public static ManualResetEvent allDone = new ManualResetEvent(false); |
48 | public static string data = null; | ||
49 | |||
50 | #region KIRYU | ||
51 | |||
52 | #region Delegates | ||
47 | 53 | ||
48 | #region KIRYU | ||
49 | public delegate void PacketRecieveHandler(InternalPacketHeader header, byte[] buff); | 54 | public delegate void PacketRecieveHandler(InternalPacketHeader header, byte[] buff); |
55 | |||
56 | #endregion | ||
57 | |||
50 | public static PacketRecieveHandler PacketHandler = null; | 58 | public static PacketRecieveHandler PacketHandler = null; |
51 | #endregion | ||
52 | 59 | ||
53 | public AsynchronousSocketListener() { } | 60 | #endregion |
61 | |||
62 | public AsynchronousSocketListener() | ||
63 | { | ||
64 | } | ||
54 | 65 | ||
55 | public static void StartListening(int port) { | 66 | public static void StartListening(int port) |
67 | { | ||
56 | IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); | 68 | IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); |
57 | IPAddress ipAddress = ipHostInfo.AddressList[0]; | 69 | IPAddress ipAddress = ipHostInfo.AddressList[0]; |
58 | IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); | 70 | IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); |
59 | 71 | ||
60 | Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); | 72 | Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
61 | try { | 73 | try |
74 | { | ||
62 | listener.Bind(localEndPoint); | 75 | listener.Bind(localEndPoint); |
63 | listener.Listen(100); | 76 | listener.Listen(100); |
64 | while (true) { | 77 | while (true) |
78 | { | ||
65 | allDone.Reset(); | 79 | allDone.Reset(); |
66 | listener.BeginAccept( new AsyncCallback(AcceptCallback), listener ); | 80 | listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); |
67 | allDone.WaitOne(); | 81 | allDone.WaitOne(); |
68 | } | 82 | } |
69 | } catch (Exception e) { | 83 | } |
84 | catch (Exception e) | ||
85 | { | ||
70 | Console.WriteLine(e.ToString()); | 86 | Console.WriteLine(e.ToString()); |
71 | } | 87 | } |
72 | /* | 88 | /* |
@@ -75,38 +91,41 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
75 | */ | 91 | */ |
76 | } | 92 | } |
77 | 93 | ||
78 | public static void AcceptCallback(IAsyncResult ar) { | 94 | public static void AcceptCallback(IAsyncResult ar) |
95 | { | ||
79 | allDone.Set(); | 96 | allDone.Set(); |
80 | Socket listener = (Socket) ar.AsyncState; | 97 | Socket listener = (Socket) ar.AsyncState; |
81 | Socket handler = listener.EndAccept(ar); | 98 | Socket handler = listener.EndAccept(ar); |
82 | StateObject state = new StateObject(); | 99 | StateObject state = new StateObject(); |
83 | state.workSocket = handler; | 100 | state.workSocket = handler; |
84 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | 101 | handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); |
85 | } | 102 | } |
86 | 103 | ||
87 | public static void ReadCallback(IAsyncResult ar) { | 104 | public static void ReadCallback(IAsyncResult ar) |
105 | { | ||
88 | StateObject state = (StateObject) ar.AsyncState; | 106 | StateObject state = (StateObject) ar.AsyncState; |
89 | Socket handler = state.workSocket; | 107 | Socket handler = state.workSocket; |
90 | 108 | ||
91 | try | 109 | try |
92 | { | 110 | { |
93 | int bytesRead = handler.EndReceive(ar); | 111 | int bytesRead = handler.EndReceive(ar); |
94 | 112 | ||
95 | //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); | 113 | //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); |
96 | 114 | ||
97 | if (bytesRead > 0) { | 115 | if (bytesRead > 0) |
116 | { | ||
98 | state.ms_ptr.Write(state.buffer, 0, bytesRead); | 117 | state.ms_ptr.Write(state.buffer, 0, bytesRead); |
99 | } | 118 | } |
100 | else | 119 | else |
101 | { | 120 | { |
102 | //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); | 121 | //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); |
103 | return; | 122 | return; |
104 | } | 123 | } |
105 | 124 | ||
106 | long rest_size = state.ms_ptr.Length; | 125 | long rest_size = state.ms_ptr.Length; |
107 | long current_pos = 0; | 126 | long current_pos = 0; |
108 | while (rest_size > TcpClient.internalPacketHeaderSize) { | 127 | while (rest_size > TcpClient.internalPacketHeaderSize) |
109 | 128 | { | |
110 | if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) | 129 | if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) |
111 | { | 130 | { |
112 | //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); | 131 | //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); |
@@ -136,7 +155,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
136 | } | 155 | } |
137 | System.Console.WriteLine(); | 156 | System.Console.WriteLine(); |
138 | */ | 157 | */ |
139 | 158 | ||
140 | state.ms_ptr.Seek(0, SeekOrigin.End); | 159 | state.ms_ptr.Seek(0, SeekOrigin.End); |
141 | // call loadbarancer function | 160 | // call loadbarancer function |
142 | if (PacketHandler != null) | 161 | if (PacketHandler != null) |
@@ -155,20 +174,18 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
155 | rest_size -= read_size; | 174 | rest_size -= read_size; |
156 | current_pos += read_size; | 175 | current_pos += read_size; |
157 | 176 | ||
158 | if (rest_size < TcpClient.internalPacketHeaderSize) { | 177 | if (rest_size < TcpClient.internalPacketHeaderSize) |
159 | 178 | { | |
160 | byte[] rest_bytes = new byte[rest_size]; | 179 | byte[] rest_bytes = new byte[rest_size]; |
161 | state.ms_ptr.Position = read_size; | 180 | state.ms_ptr.Position = read_size; |
162 | state.ms_ptr.Read(rest_bytes, 0, (int)rest_size); | 181 | state.ms_ptr.Read(rest_bytes, 0, (int) rest_size); |
163 | state.ms_ptr.Close(); | 182 | state.ms_ptr.Close(); |
164 | state.ms_ptr = new MemoryStream(); | 183 | state.ms_ptr = new MemoryStream(); |
165 | state.ms_ptr.Write(rest_bytes, 0, (int)rest_size); | 184 | state.ms_ptr.Write(rest_bytes, 0, (int) rest_size); |
166 | break; | 185 | break; |
167 | } | 186 | } |
168 | } | 187 | } |
169 | |||
170 | } // while (true) | 188 | } // while (true) |
171 | |||
172 | } | 189 | } |
173 | catch (Exception) | 190 | catch (Exception) |
174 | { | 191 | { |
@@ -176,19 +193,26 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
176 | //MainLog.Instance.Verbose("TCPSERVER", e.StackTrace); | 193 | //MainLog.Instance.Verbose("TCPSERVER", e.StackTrace); |
177 | } | 194 | } |
178 | 195 | ||
179 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | 196 | handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); |
180 | } | 197 | } |
181 | } | 198 | } |
182 | 199 | ||
183 | public class TcpServer { | 200 | public class TcpServer |
201 | { | ||
184 | private int mPort = 11000; | 202 | private int mPort = 11000; |
185 | public TcpServer() { | 203 | |
204 | public TcpServer() | ||
205 | { | ||
186 | } | 206 | } |
187 | public TcpServer(int port) { | 207 | |
208 | public TcpServer(int port) | ||
209 | { | ||
188 | mPort = port; | 210 | mPort = port; |
189 | } | 211 | } |
190 | public void start() { | 212 | |
213 | public void start() | ||
214 | { | ||
191 | AsynchronousSocketListener.StartListening(mPort); | 215 | AsynchronousSocketListener.StartListening(mPort); |
192 | } | 216 | } |
193 | } | 217 | } |
194 | } | 218 | } \ No newline at end of file |