diff options
author | Jeff Ames | 2008-01-12 01:35:25 +0000 |
---|---|---|
committer | Jeff Ames | 2008-01-12 01:35:25 +0000 |
commit | 5d7e120d56873d91141695ddf7aebd88e4619dce (patch) | |
tree | 79e5ae7fcb518dfc726e447a029744b05204001c /OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs | |
parent | And one last time - removed duplicate exception dump :) (diff) | |
download | opensim-SC-5d7e120d56873d91141695ddf7aebd88e4619dce.zip opensim-SC-5d7e120d56873d91141695ddf7aebd88e4619dce.tar.gz opensim-SC-5d7e120d56873d91141695ddf7aebd88e4619dce.tar.bz2 opensim-SC-5d7e120d56873d91141695ddf7aebd88e4619dce.tar.xz |
Set svn:eol-style.
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs | 286 |
1 files changed, 143 insertions, 143 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs b/OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs index f8ec7b5..8296bea 100644 --- a/OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs +++ b/OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs | |||
@@ -1,144 +1,144 @@ | |||
1 | using System; | 1 | using System; |
2 | using System.Collections.Generic; | 2 | using System.Collections.Generic; |
3 | using System.Diagnostics; | 3 | using System.Diagnostics; |
4 | using System.Text; | 4 | using System.Text; |
5 | using OpenSim.Region.ScriptEngine.Common.TRPC; | 5 | using OpenSim.Region.ScriptEngine.Common.TRPC; |
6 | 6 | ||
7 | namespace OpenSim.Region.ScriptEngine.Common | 7 | namespace OpenSim.Region.ScriptEngine.Common |
8 | { | 8 | { |
9 | public class TRPC_Remote | 9 | public class TRPC_Remote |
10 | { | 10 | { |
11 | public readonly int MaxQueueSize = 1024 * 10; | 11 | public readonly int MaxQueueSize = 1024 * 10; |
12 | public readonly TCPCommon.ServerAndClientInterface TCPS; | 12 | public readonly TCPCommon.ServerAndClientInterface TCPS; |
13 | 13 | ||
14 | public delegate void ReceiveCommandDelegate(int ID, string Command, params object[] p); | 14 | public delegate void ReceiveCommandDelegate(int ID, string Command, params object[] p); |
15 | public event ReceiveCommandDelegate ReceiveCommand; | 15 | public event ReceiveCommandDelegate ReceiveCommand; |
16 | 16 | ||
17 | // TODO: Maybe we should move queue into TCPSocket so we won't have to keep one queue instance per connection | 17 | // TODO: Maybe we should move queue into TCPSocket so we won't have to keep one queue instance per connection |
18 | private System.Collections.Generic.Dictionary<int, InQueueStruct> InQueue = new Dictionary<int, InQueueStruct>(); | 18 | private System.Collections.Generic.Dictionary<int, InQueueStruct> InQueue = new Dictionary<int, InQueueStruct>(); |
19 | private class InQueueStruct | 19 | private class InQueueStruct |
20 | { | 20 | { |
21 | public byte[] Queue; | 21 | public byte[] Queue; |
22 | public int QueueSize; | 22 | public int QueueSize; |
23 | public object QueueLockObject = new object(); | 23 | public object QueueLockObject = new object(); |
24 | } | 24 | } |
25 | 25 | ||
26 | public TRPC_Remote(TCPCommon.ServerAndClientInterface TCPClientOrServer) | 26 | public TRPC_Remote(TCPCommon.ServerAndClientInterface TCPClientOrServer) |
27 | { | 27 | { |
28 | TCPS = TCPClientOrServer; | 28 | TCPS = TCPClientOrServer; |
29 | TCPS.Close += new TCPCommon.CloseDelegate(TCPS_Close); | 29 | TCPS.Close += new TCPCommon.CloseDelegate(TCPS_Close); |
30 | TCPS.ClientConnected += new TCPCommon.ClientConnectedDelegate(TCPS_ClientConnected); | 30 | TCPS.ClientConnected += new TCPCommon.ClientConnectedDelegate(TCPS_ClientConnected); |
31 | TCPS.DataReceived += new TCPCommon.DataReceivedDelegate(TCPS_DataReceived); | 31 | TCPS.DataReceived += new TCPCommon.DataReceivedDelegate(TCPS_DataReceived); |
32 | //TCPS.StartListen(); | 32 | //TCPS.StartListen(); |
33 | } | 33 | } |
34 | 34 | ||
35 | void TCPS_ClientConnected(int ID, System.Net.EndPoint Remote) | 35 | void TCPS_ClientConnected(int ID, System.Net.EndPoint Remote) |
36 | { | 36 | { |
37 | // Create a incoming queue for this connection | 37 | // Create a incoming queue for this connection |
38 | InQueueStruct iq = new InQueueStruct(); | 38 | InQueueStruct iq = new InQueueStruct(); |
39 | iq.Queue = new byte[MaxQueueSize]; | 39 | iq.Queue = new byte[MaxQueueSize]; |
40 | iq.QueueSize = 0; | 40 | iq.QueueSize = 0; |
41 | InQueue.Add(ID, iq); | 41 | InQueue.Add(ID, iq); |
42 | } | 42 | } |
43 | 43 | ||
44 | void TCPS_Close(int ID) | 44 | void TCPS_Close(int ID) |
45 | { | 45 | { |
46 | // Remove queue | 46 | // Remove queue |
47 | InQueue.Remove(ID); | 47 | InQueue.Remove(ID); |
48 | } | 48 | } |
49 | 49 | ||
50 | void TCPS_DataReceived(int ID, byte[] data, int offset, int length) | 50 | void TCPS_DataReceived(int ID, byte[] data, int offset, int length) |
51 | { | 51 | { |
52 | // Copy new data to incoming queue | 52 | // Copy new data to incoming queue |
53 | lock (InQueue[ID].QueueLockObject) | 53 | lock (InQueue[ID].QueueLockObject) |
54 | { | 54 | { |
55 | Array.Copy(data, offset, InQueue[ID].Queue, InQueue[ID].QueueSize, length); | 55 | Array.Copy(data, offset, InQueue[ID].Queue, InQueue[ID].QueueSize, length); |
56 | InQueue[ID].QueueSize += length; | 56 | InQueue[ID].QueueSize += length; |
57 | 57 | ||
58 | // Process incoming queue | 58 | // Process incoming queue |
59 | ProcessQueue(ID); | 59 | ProcessQueue(ID); |
60 | } | 60 | } |
61 | } | 61 | } |
62 | 62 | ||
63 | private void ProcessQueue(int ID) | 63 | private void ProcessQueue(int ID) |
64 | { | 64 | { |
65 | 65 | ||
66 | // This is just a temp implementation -- not so fast :) | 66 | // This is just a temp implementation -- not so fast :) |
67 | 67 | ||
68 | InQueueStruct myIQS = InQueue[ID]; | 68 | InQueueStruct myIQS = InQueue[ID]; |
69 | if (myIQS.QueueSize == 0) | 69 | if (myIQS.QueueSize == 0) |
70 | return; | 70 | return; |
71 | 71 | ||
72 | string receivedData = Encoding.ASCII.GetString(myIQS.Queue, 0, myIQS.QueueSize); | 72 | string receivedData = Encoding.ASCII.GetString(myIQS.Queue, 0, myIQS.QueueSize); |
73 | Debug.WriteLine("RAW: " + receivedData); | 73 | Debug.WriteLine("RAW: " + receivedData); |
74 | 74 | ||
75 | 75 | ||
76 | byte newLine = 10; | 76 | byte newLine = 10; |
77 | while (true) | 77 | while (true) |
78 | { | 78 | { |
79 | bool ShouldProcess = false; | 79 | bool ShouldProcess = false; |
80 | int lineEndPos = 0; | 80 | int lineEndPos = 0; |
81 | 81 | ||
82 | // Look for newline | 82 | // Look for newline |
83 | for (int i = 0; i < myIQS.QueueSize; i++) | 83 | for (int i = 0; i < myIQS.QueueSize; i++) |
84 | { | 84 | { |
85 | if (myIQS.Queue[i] == newLine) | 85 | if (myIQS.Queue[i] == newLine) |
86 | { | 86 | { |
87 | ShouldProcess = true; | 87 | ShouldProcess = true; |
88 | lineEndPos = i; | 88 | lineEndPos = i; |
89 | break; | 89 | break; |
90 | } | 90 | } |
91 | } | 91 | } |
92 | 92 | ||
93 | // Process it? | 93 | // Process it? |
94 | if (!ShouldProcess) | 94 | if (!ShouldProcess) |
95 | return; | 95 | return; |
96 | // Yes | 96 | // Yes |
97 | string cmdLine = Encoding.ASCII.GetString(myIQS.Queue, 0, lineEndPos); | 97 | string cmdLine = Encoding.ASCII.GetString(myIQS.Queue, 0, lineEndPos); |
98 | Debug.WriteLine("Command: " + cmdLine); | 98 | Debug.WriteLine("Command: " + cmdLine); |
99 | 99 | ||
100 | // Fix remaining queue in an inefficient way | 100 | // Fix remaining queue in an inefficient way |
101 | byte[] newQueue = new byte[MaxQueueSize]; | 101 | byte[] newQueue = new byte[MaxQueueSize]; |
102 | Array.Copy(myIQS.Queue, lineEndPos, newQueue, 0, myIQS.QueueSize - lineEndPos); | 102 | Array.Copy(myIQS.Queue, lineEndPos, newQueue, 0, myIQS.QueueSize - lineEndPos); |
103 | myIQS.Queue = newQueue; | 103 | myIQS.Queue = newQueue; |
104 | myIQS.QueueSize -= (lineEndPos + 1); | 104 | myIQS.QueueSize -= (lineEndPos + 1); |
105 | 105 | ||
106 | // Now back to the command | 106 | // Now back to the command |
107 | string[] parts = cmdLine.Split(','); | 107 | string[] parts = cmdLine.Split(','); |
108 | if (parts.Length > 0) | 108 | if (parts.Length > 0) |
109 | { | 109 | { |
110 | string cmd = parts[0]; | 110 | string cmd = parts[0]; |
111 | int paramCount = parts.Length - 1; | 111 | int paramCount = parts.Length - 1; |
112 | string[] param = null; | 112 | string[] param = null; |
113 | 113 | ||
114 | if (paramCount > 0) | 114 | if (paramCount > 0) |
115 | { | 115 | { |
116 | // Process all parameters (decoding them from URL encoding) | 116 | // Process all parameters (decoding them from URL encoding) |
117 | param = new string[paramCount]; | 117 | param = new string[paramCount]; |
118 | for (int i = 1; i < parts.Length; i++) | 118 | for (int i = 1; i < parts.Length; i++) |
119 | { | 119 | { |
120 | param[i - 1] = System.Web.HttpUtility.UrlDecode(parts[i]); | 120 | param[i - 1] = System.Web.HttpUtility.UrlDecode(parts[i]); |
121 | } | 121 | } |
122 | } | 122 | } |
123 | 123 | ||
124 | ReceiveCommand(ID, cmd, param); | 124 | ReceiveCommand(ID, cmd, param); |
125 | } | 125 | } |
126 | } | 126 | } |
127 | } | 127 | } |
128 | 128 | ||
129 | public void SendCommand(int ID, string Command, params object[] p) | 129 | public void SendCommand(int ID, string Command, params object[] p) |
130 | { | 130 | { |
131 | // Call PacketFactory to have it create a packet for us | 131 | // Call PacketFactory to have it create a packet for us |
132 | 132 | ||
133 | //string[] tmpP = new string[p.Length]; | 133 | //string[] tmpP = new string[p.Length]; |
134 | string tmpStr = Command; | 134 | string tmpStr = Command; |
135 | for (int i = 0; i < p.Length; i++) | 135 | for (int i = 0; i < p.Length; i++) |
136 | { | 136 | { |
137 | tmpStr += "," + System.Web.HttpUtility.UrlEncode(p[i].ToString()); // .Replace(",", "%44") | 137 | tmpStr += "," + System.Web.HttpUtility.UrlEncode(p[i].ToString()); // .Replace(",", "%44") |
138 | } | 138 | } |
139 | tmpStr += "\n"; | 139 | tmpStr += "\n"; |
140 | byte[] byteData = Encoding.ASCII.GetBytes(tmpStr); | 140 | byte[] byteData = Encoding.ASCII.GetBytes(tmpStr); |
141 | TCPS.Send(ID, byteData, 0, byteData.Length); | 141 | TCPS.Send(ID, byteData, 0, byteData.Length); |
142 | } | 142 | } |
143 | } | 143 | } |
144 | } \ No newline at end of file | 144 | } \ No newline at end of file |