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