aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine')
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs33
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs141
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs62
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs128
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs111
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs204
6 files changed, 0 insertions, 679 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs
deleted file mode 100644
index 14c6078..0000000
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs
+++ /dev/null
@@ -1,33 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28namespace OpenSim.Region.ScriptEngine.Common.TRPC
29{
30 class MyBase
31 {
32 }
33}
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs
deleted file mode 100644
index 624c3bb..0000000
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs
+++ /dev/null
@@ -1,141 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System.Collections.Generic;
29using System.Diagnostics;
30using System.Net;
31using System.Net.Sockets;
32
33namespace OpenSim.Region.ScriptEngine.Common.TRPC
34{
35 public class TCPClient : TCPCommon.ClientInterface
36 {
37 public TCPClient()
38 {
39 }
40
41 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>();
42 private int ClientCount = 0;
43
44 public event TCPCommon.ClientConnectedDelegate ClientConnected;
45 public event TCPCommon.DataReceivedDelegate DataReceived;
46 public event TCPCommon.DataSentDelegate DataSent;
47 public event TCPCommon.CloseDelegate Close;
48 public event TCPCommon.ConnectErrorDelegate ConnectError;
49
50 /// <summary>
51 /// Creates client connection
52 /// </summary>
53 public void Connect(string RemoteHost, int RemotePort)
54 {
55 Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
56 IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort);
57 //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock);
58 newsock.Connect(ipe);
59 }
60
61 public int ConnectAndReturnID(string RemoteHost, int RemotePort)
62 {
63 Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
64 IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort);
65 //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock);
66 newsock.Connect(ipe);
67 return ProcessConnection(newsock);
68 }
69
70 public void Disconnect(int ID)
71 {
72 Clients[ID].Disconnect();
73 }
74
75// TODO: unused
76// void asyncConnected(IAsyncResult iar)
77// {
78// Socket client = (Socket)iar.AsyncState;
79// client.EndConnect(iar);
80// ProcessConnection(client);
81// }
82
83 private int ProcessConnection(Socket client)
84 {
85 try
86 {
87 int id = ClientCount++;
88 TCPSocket S = new TCPSocket(id, client);
89
90 // Add to dictionary
91 Clients.Add(id, S);
92
93 // Add event handlers
94 S.Close += new TCPSocket.CloseDelegate(S_Close);
95 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived);
96 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent);
97
98 // Start it
99 S.Start();
100
101 Debug.WriteLine("Connection established: " + client.RemoteEndPoint.ToString());
102
103 // Fire Connected-event
104 if (ClientConnected != null)
105 ClientConnected(id, client.RemoteEndPoint);
106
107 return id;
108 }
109 catch (SocketException sex)
110 {
111 if (ConnectError != null)
112 ConnectError(sex.Message);
113 }
114 return -1;
115 }
116
117 void S_DataSent(int ID, int length)
118 {
119 if (DataSent != null)
120 DataSent(ID, length);
121 }
122
123 void S_DataReceived(int ID, byte[] data, int offset, int length)
124 {
125 if (DataReceived != null)
126 DataReceived(ID, data, offset, length);
127 }
128
129 void S_Close(int ID)
130 {
131 if (Close != null)
132 Close(ID);
133 Clients.Remove(ID);
134 }
135
136 public void Send(int clientID, byte[] data, int offset, int len)
137 {
138 Clients[clientID].Send(clientID, data, offset, len);
139 }
140 }
141}
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs
deleted file mode 100644
index ab9d9b4..0000000
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs
+++ /dev/null
@@ -1,62 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System.Net;
29
30namespace OpenSim.Region.ScriptEngine.Common.TRPC
31{
32 public class TCPCommon
33 {
34 public delegate void ClientConnectedDelegate(int ID, EndPoint Remote);
35 public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length);
36 public delegate void DataSentDelegate(int ID, int length);
37 public delegate void CloseDelegate(int ID);
38 public delegate void ConnectErrorDelegate(string Reason);
39
40 public interface ServerAndClientInterface
41 {
42 void Send(int clientID, byte[] data, int offset, int len);
43 event ClientConnectedDelegate ClientConnected;
44 event DataReceivedDelegate DataReceived;
45 event DataSentDelegate DataSent;
46 event CloseDelegate Close;
47 }
48
49 public interface ClientInterface : ServerAndClientInterface
50 {
51 event ConnectErrorDelegate ConnectError;
52 void Connect(string RemoteHost, int RemotePort);
53 void Disconnect(int ID);
54 }
55
56 public interface ServerInterface : ServerAndClientInterface
57 {
58 void StartListen();
59 void StopListen();
60 }
61 }
62}
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
deleted file mode 100644
index 26892a2..0000000
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
+++ /dev/null
@@ -1,128 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.Diagnostics;
31using System.Net;
32using System.Net.Sockets;
33
34namespace OpenSim.Region.ScriptEngine.Common.TRPC
35{
36 public class TCPServer: TCPCommon.ServerInterface
37 {
38 public readonly int LocalPort;
39 public TCPServer(int localPort)
40 {
41 LocalPort = localPort;
42 }
43
44 private Socket server;
45
46 /// <summary>
47 /// Starts listening for new connections
48 /// </summary>
49 public void StartListen()
50 {
51 server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
52 IPEndPoint ipe = new IPEndPoint(IPAddress.Any, LocalPort);
53 server.Bind(ipe);
54 server.Listen(10);
55 server.BeginAccept(new AsyncCallback(AsyncAcceptConnections), server);
56 }
57 /// <summary>
58 /// Stops listening for new connections
59 /// </summary>
60 public void StopListen()
61 {
62 server.Close();
63 server = null;
64 }
65
66 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>();
67 private int ClientCount = 0;
68
69 public event TCPCommon.ClientConnectedDelegate ClientConnected;
70 public event TCPCommon.DataReceivedDelegate DataReceived;
71 public event TCPCommon.DataSentDelegate DataSent;
72 public event TCPCommon.CloseDelegate Close;
73
74 /// <summary>
75 /// Async callback for new connections
76 /// </summary>
77 /// <param name="ar"></param>
78 private void AsyncAcceptConnections(IAsyncResult ar)
79 {
80 int id = ClientCount++;
81 Socket oldserver = (Socket)ar.AsyncState;
82 Socket client = oldserver.EndAccept(ar);
83 TCPSocket S = new TCPSocket(id, client);
84
85 // Add to dictionary
86 Clients.Add(id, S);
87
88 // Add event handlers
89 S.Close += new TCPSocket.CloseDelegate(S_Close);
90 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived);
91 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent);
92
93 // Start it
94 S.Start();
95
96 Debug.WriteLine("Connection received: " + client.RemoteEndPoint.ToString());
97
98 // Fire Connected-event
99 if (ClientConnected != null)
100 ClientConnected(id, client.RemoteEndPoint);
101
102 }
103
104 void S_DataSent(int ID, int length)
105 {
106 if (DataSent != null)
107 DataSent(ID, length);
108 }
109
110 void S_DataReceived(int ID, byte[] data, int offset, int length)
111 {
112 if (DataReceived != null)
113 DataReceived(ID, data, offset, length);
114 }
115
116 void S_Close(int ID)
117 {
118 if (Close != null)
119 Close(ID);
120 Clients.Remove(ID);
121 }
122
123 public void Send(int clientID, byte[] data, int offset, int len)
124 {
125 Clients[clientID].Send(clientID, data, offset, len);
126 }
127 }
128}
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs
deleted file mode 100644
index 693ec6f..0000000
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs
+++ /dev/null
@@ -1,111 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Net.Sockets;
30
31namespace OpenSim.Region.ScriptEngine.Common.TRPC
32{
33 public class TCPSocket
34 {
35 public readonly Socket Client;
36 public readonly int ID;
37
38 public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length);
39 public delegate void DataSentDelegate(int ID, int length);
40 public delegate void CloseDelegate(int ID);
41 public event DataReceivedDelegate DataReceived;
42 public event DataSentDelegate DataSent;
43 public event CloseDelegate Close;
44
45 private byte[] RecvQueue = new byte[4096];
46 private int RecvQueueSize = 4096;
47
48 public TCPSocket(int id, Socket client)
49 {
50 ID = id;
51 Client = client;
52 }
53
54 public void Start()
55 {
56 // Start listening
57 BeginReceive();
58 }
59
60 private void BeginReceive()
61 {
62 Client.BeginReceive(RecvQueue, 0, RecvQueueSize, SocketFlags.None, new AsyncCallback(asyncDataReceived), Client);
63 }
64
65 /// <summary>
66 /// Callback for successful receive (or connection close)
67 /// </summary>
68 /// <param name="ar"></param>
69 private void asyncDataReceived(IAsyncResult ar)
70 {
71 Socket client = (Socket)ar.AsyncState;
72 int recv = client.EndReceive(ar);
73
74 // Is connection closed?
75 if (recv == 0)
76 {
77 client.Close();
78 Close(ID);
79 return;
80 }
81
82 // Call receive event
83 DataReceived(ID, RecvQueue, 0, recv);
84
85 // Start new receive
86 BeginReceive();
87 }
88
89 public void Send(int clientID, byte[] data, int offset, int len)
90 {
91 Client.BeginSend(data, offset, len, SocketFlags.None, new AsyncCallback(asyncDataSent), Client);
92 }
93
94 /// <summary>
95 /// Callback for successful send
96 /// </summary>
97 /// <param name="ar"></param>
98 void asyncDataSent(IAsyncResult ar)
99 {
100 Socket client = (Socket)ar.AsyncState;
101 int sent = client.EndSend(ar);
102 DataSent(ID, sent);
103 }
104
105 public void Disconnect()
106 {
107 Client.Close();
108 Close(ID);
109 }
110 }
111}
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs b/OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs
deleted file mode 100644
index b02f2f5..0000000
--- a/OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs
+++ /dev/null
@@ -1,204 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.Diagnostics;
31using System.Net;
32using System.Text;
33using System.Web;
34using OpenMetaverse;
35using OpenSim.Region.ScriptEngine.Common.TRPC;
36
37namespace OpenSim.Region.ScriptEngine.Common
38{
39 public class TRPC_Remote
40 {
41 public readonly int MaxQueueSize = 1024 * 10;
42 public readonly TCPCommon.ServerAndClientInterface TCPS;
43
44 public delegate void ReceiveCommandDelegate(int ID, string Command, params object[] p);
45 public event ReceiveCommandDelegate ReceiveCommand;
46 Dictionary<string, Type> TypeDictionary = new Dictionary<string, Type>();
47 Type[] Types =
48 {
49 typeof(String),
50 typeof(Int16),
51 typeof(Int32),
52 typeof(Int64),
53 typeof(Double),
54 typeof(Decimal),
55 typeof(Array),
56 typeof(UUID),
57 typeof(UInt16),
58 typeof(UInt32),
59 typeof(UInt64)
60 };
61
62 // TODO: Maybe we should move queue into TCPSocket so we won't have to keep one queue instance per connection
63 private Dictionary<int, InQueueStruct> InQueue = new Dictionary<int, InQueueStruct>();
64 private class InQueueStruct
65 {
66 public byte[] Queue;
67 public int QueueSize;
68 public object QueueLockObject = new object();
69 }
70
71 public TRPC_Remote(TCPCommon.ServerAndClientInterface TCPClientOrServer)
72 {
73 TCPS = TCPClientOrServer;
74 TCPS.Close += new TCPCommon.CloseDelegate(TCPS_Close);
75 TCPS.ClientConnected += new TCPCommon.ClientConnectedDelegate(TCPS_ClientConnected);
76 TCPS.DataReceived += new TCPCommon.DataReceivedDelegate(TCPS_DataReceived);
77 //TCPS.StartListen();
78
79 // Make a lookup dictionary for types
80 foreach (Type t in Types)
81 {
82 TypeDictionary.Add(t.ToString(), t);
83 }
84 }
85
86 void TCPS_ClientConnected(int ID, EndPoint Remote)
87 {
88 // Create a incoming queue for this connection
89 InQueueStruct iq = new InQueueStruct();
90 iq.Queue = new byte[MaxQueueSize];
91 iq.QueueSize = 0;
92 InQueue.Add(ID, iq);
93 }
94
95 void TCPS_Close(int ID)
96 {
97 // Remove queue
98 InQueue.Remove(ID);
99 }
100
101 void TCPS_DataReceived(int ID, byte[] data, int offset, int length)
102 {
103 // Copy new data to incoming queue
104 lock (InQueue[ID].QueueLockObject)
105 {
106 Array.Copy(data, offset, InQueue[ID].Queue, InQueue[ID].QueueSize, length);
107 InQueue[ID].QueueSize += length;
108
109 // Process incoming queue
110 ProcessQueue(ID);
111 }
112 }
113
114 private void ProcessQueue(int ID)
115 {
116 // This is just a temp implementation -- not so fast :)
117
118 InQueueStruct myIQS = InQueue[ID];
119 if (myIQS.QueueSize == 0)
120 return;
121
122 string receivedData = Encoding.UTF8.GetString(myIQS.Queue, 0, myIQS.QueueSize);
123 Debug.WriteLine("RAW: " + receivedData);
124
125 byte newLine = 10;
126 while (true)
127 {
128 bool ShouldProcess = false;
129 int lineEndPos = 0;
130
131 // Look for newline
132 for (int i = 0; i < myIQS.QueueSize; i++)
133 {
134 if (myIQS.Queue[i] == newLine)
135 {
136 ShouldProcess = true;
137 lineEndPos = i;
138 break;
139 }
140 }
141
142 // Process it?
143 if (!ShouldProcess)
144 return;
145 // Yes
146 string cmdLine = Encoding.ASCII.GetString(myIQS.Queue, 0, lineEndPos);
147 Debug.WriteLine("Command: " + cmdLine);
148
149 // Fix remaining queue in an inefficient way
150 byte[] newQueue = new byte[MaxQueueSize];
151 Array.Copy(myIQS.Queue, lineEndPos, newQueue, 0, myIQS.QueueSize - lineEndPos);
152 myIQS.Queue = newQueue;
153 myIQS.QueueSize -= (lineEndPos + 1);
154
155 // Now back to the command
156 string[] parts = cmdLine.Split(',');
157 if (parts.Length > 0)
158 {
159 string cmd = parts[0];
160 int paramCount = parts.Length - 1;
161 object[] param = null;
162
163 if (paramCount > 0)
164 {
165 // Process all parameters (decoding them from URL encoding)
166 param = new object[paramCount];
167 for (int i = 1; i < parts.Length; i++)
168 {
169 string[] spl;
170 spl = HttpUtility.UrlDecode(parts[i]).Split('|');
171 string t = spl[0];
172 param[i - 1] = Convert.ChangeType(spl[1], TypeLookup(t));
173 }
174 }
175
176 ReceiveCommand(ID, cmd, param);
177 }
178 }
179 }
180
181 private Type TypeLookup(string t)
182 {
183 Type ret = TypeDictionary[t];
184 if (ret != null)
185 return ret;
186 return typeof(object);
187 }
188
189 public void SendCommand(int ID, string Command, params object[] p)
190 {
191 // Call PacketFactory to have it create a packet for us
192
193 //string[] tmpP = new string[p.Length];
194 string tmpStr = Command;
195 for (int i = 0; i < p.Length; i++)
196 {
197 tmpStr += "," + p[i].GetType().ToString() + "|" + HttpUtility.UrlEncode(p[i].ToString()); // .Replace(",", "%44")
198 }
199 tmpStr += "\n";
200 byte[] byteData = Encoding.UTF8.GetBytes(tmpStr);
201 TCPS.Send(ID, byteData, 0, byteData.Length);
202 }
203 }
204}