aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Common/TRPC
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs248
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs64
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs210
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs170
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC_Remote.cs286
5 files changed, 489 insertions, 489 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs
index e0a46c5..c176a08 100644
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs
+++ b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs
@@ -1,125 +1,125 @@
1using System; 1using System;
2using System.Collections.Generic; 2using System.Collections.Generic;
3using System.Diagnostics; 3using System.Diagnostics;
4using System.Net; 4using System.Net;
5using System.Net.Sockets; 5using System.Net.Sockets;
6using System.Text; 6using System.Text;
7 7
8namespace OpenSim.Region.ScriptEngine.Common.TRPC 8namespace OpenSim.Region.ScriptEngine.Common.TRPC
9{ 9{
10 public class TCPClient : TCPCommon.ClientInterface 10 public class TCPClient : TCPCommon.ClientInterface
11 { 11 {
12 12
13 public TCPClient() 13 public TCPClient()
14 { 14 {
15 } 15 }
16 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>(); 16 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>();
17 private int ClientCount = 0; 17 private int ClientCount = 0;
18 18
19 19
20 public event TCPCommon.ClientConnectedDelegate ClientConnected; 20 public event TCPCommon.ClientConnectedDelegate ClientConnected;
21 public event TCPCommon.DataReceivedDelegate DataReceived; 21 public event TCPCommon.DataReceivedDelegate DataReceived;
22 public event TCPCommon.DataSentDelegate DataSent; 22 public event TCPCommon.DataSentDelegate DataSent;
23 public event TCPCommon.CloseDelegate Close; 23 public event TCPCommon.CloseDelegate Close;
24 public event TCPCommon.ConnectErrorDelegate ConnectError; 24 public event TCPCommon.ConnectErrorDelegate ConnectError;
25 25
26 26
27 /// <summary> 27 /// <summary>
28 /// Creates client connection 28 /// Creates client connection
29 /// </summary> 29 /// </summary>
30 public void Connect(string RemoteHost, int RemotePort) 30 public void Connect(string RemoteHost, int RemotePort)
31 { 31 {
32 Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 32 Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
33 IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort); 33 IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort);
34 //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock); 34 //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock);
35 newsock.Connect(ipe); 35 newsock.Connect(ipe);
36 } 36 }
37 public int ConnectAndReturnID(string RemoteHost, int RemotePort) 37 public int ConnectAndReturnID(string RemoteHost, int RemotePort)
38 { 38 {
39 Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 39 Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
40 IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort); 40 IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort);
41 //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock); 41 //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock);
42 newsock.Connect(ipe); 42 newsock.Connect(ipe);
43 return ProcessConnection(newsock); 43 return ProcessConnection(newsock);
44 } 44 }
45 45
46 46
47 public void Disconnect(int ID) 47 public void Disconnect(int ID)
48 { 48 {
49 Clients[ID].Disconnect(); 49 Clients[ID].Disconnect();
50 } 50 }
51 51
52 void asyncConnected(IAsyncResult iar) 52 void asyncConnected(IAsyncResult iar)
53 { 53 {
54 Socket client = (Socket)iar.AsyncState; 54 Socket client = (Socket)iar.AsyncState;
55 client.EndConnect(iar); 55 client.EndConnect(iar);
56 ProcessConnection(client); 56 ProcessConnection(client);
57 } 57 }
58 58
59 private int ProcessConnection(Socket client) 59 private int ProcessConnection(Socket client)
60 { 60 {
61 try 61 try
62 { 62 {
63 63
64 64
65 65
66 int id = ClientCount++; 66 int id = ClientCount++;
67 TCPSocket S = new TCPSocket(id, client); 67 TCPSocket S = new TCPSocket(id, client);
68 68
69 // Add to dictionary 69 // Add to dictionary
70 Clients.Add(id, S); 70 Clients.Add(id, S);
71 71
72 // Add event handlers 72 // Add event handlers
73 S.Close += new TCPSocket.CloseDelegate(S_Close); 73 S.Close += new TCPSocket.CloseDelegate(S_Close);
74 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived); 74 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived);
75 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent); 75 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent);
76 76
77 // Start it 77 // Start it
78 S.Start(); 78 S.Start();
79 79
80 Debug.WriteLine("Connection established: " + client.RemoteEndPoint.ToString()); 80 Debug.WriteLine("Connection established: " + client.RemoteEndPoint.ToString());
81 81
82 // Fire Connected-event 82 // Fire Connected-event
83 if (ClientConnected != null) 83 if (ClientConnected != null)
84 ClientConnected(id, client.RemoteEndPoint); 84 ClientConnected(id, client.RemoteEndPoint);
85 85
86 return id; 86 return id;
87 } 87 }
88 catch (SocketException sex) 88 catch (SocketException sex)
89 { 89 {
90 if (ConnectError != null) 90 if (ConnectError != null)
91 ConnectError(sex.Message); 91 ConnectError(sex.Message);
92 } 92 }
93 return -1; 93 return -1;
94 } 94 }
95 95
96 96
97 97
98 98
99 void S_DataSent(int ID, int length) 99 void S_DataSent(int ID, int length)
100 { 100 {
101 if (DataSent != null) 101 if (DataSent != null)
102 DataSent(ID, length); 102 DataSent(ID, length);
103 } 103 }
104 104
105 void S_DataReceived(int ID, byte[] data, int offset, int length) 105 void S_DataReceived(int ID, byte[] data, int offset, int length)
106 { 106 {
107 if (DataReceived != null) 107 if (DataReceived != null)
108 DataReceived(ID, data, offset, length); 108 DataReceived(ID, data, offset, length);
109 } 109 }
110 110
111 void S_Close(int ID) 111 void S_Close(int ID)
112 { 112 {
113 if (Close != null) 113 if (Close != null)
114 Close(ID); 114 Close(ID);
115 Clients.Remove(ID); 115 Clients.Remove(ID);
116 } 116 }
117 117
118 public void Send(int clientID, byte[] data, int offset, int len) 118 public void Send(int clientID, byte[] data, int offset, int len)
119 { 119 {
120 Clients[clientID].Send(clientID, data, offset, len); 120 Clients[clientID].Send(clientID, data, offset, len);
121 } 121 }
122 122
123 123
124 } 124 }
125} \ No newline at end of file 125} \ No newline at end of file
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs
index 83548b4..36a13e3 100644
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs
+++ b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs
@@ -1,33 +1,33 @@
1namespace OpenSim.Region.ScriptEngine.Common.TRPC 1namespace OpenSim.Region.ScriptEngine.Common.TRPC
2{ 2{
3 public class TCPCommon 3 public class TCPCommon
4 { 4 {
5 public delegate void ClientConnectedDelegate(int ID, System.Net.EndPoint Remote); 5 public delegate void ClientConnectedDelegate(int ID, System.Net.EndPoint Remote);
6 public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length); 6 public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length);
7 public delegate void DataSentDelegate(int ID, int length); 7 public delegate void DataSentDelegate(int ID, int length);
8 public delegate void CloseDelegate(int ID); 8 public delegate void CloseDelegate(int ID);
9 public delegate void ConnectErrorDelegate(string Reason); 9 public delegate void ConnectErrorDelegate(string Reason);
10 10
11 11
12 public interface ServerAndClientInterface 12 public interface ServerAndClientInterface
13 { 13 {
14 void Send(int clientID, byte[] data, int offset, int len); 14 void Send(int clientID, byte[] data, int offset, int len);
15 event ClientConnectedDelegate ClientConnected; 15 event ClientConnectedDelegate ClientConnected;
16 event DataReceivedDelegate DataReceived; 16 event DataReceivedDelegate DataReceived;
17 event DataSentDelegate DataSent; 17 event DataSentDelegate DataSent;
18 event CloseDelegate Close; 18 event CloseDelegate Close;
19 } 19 }
20 public interface ClientInterface : ServerAndClientInterface 20 public interface ClientInterface : ServerAndClientInterface
21 { 21 {
22 event TCPCommon.ConnectErrorDelegate ConnectError; 22 event TCPCommon.ConnectErrorDelegate ConnectError;
23 void Connect(string RemoteHost, int RemotePort); 23 void Connect(string RemoteHost, int RemotePort);
24 void Disconnect(int ID); 24 void Disconnect(int ID);
25 } 25 }
26 public interface ServerInterface : ServerAndClientInterface 26 public interface ServerInterface : ServerAndClientInterface
27 { 27 {
28 void StartListen(); 28 void StartListen();
29 void StopListen(); 29 void StopListen();
30 } 30 }
31 31
32 } 32 }
33} \ No newline at end of file 33} \ No newline at end of file
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
index 3af898a..0a85dc4 100644
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
+++ b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
@@ -1,106 +1,106 @@
1using System; 1using System;
2using System.Collections.Generic; 2using System.Collections.Generic;
3using System.Diagnostics; 3using System.Diagnostics;
4using System.Net; 4using System.Net;
5using System.Net.Sockets; 5using System.Net.Sockets;
6using TCPCommon=OpenSim.Region.ScriptEngine.Common.TRPC.TCPCommon; 6using TCPCommon=OpenSim.Region.ScriptEngine.Common.TRPC.TCPCommon;
7 7
8namespace OpenSim.Region.ScriptEngine.Common.TRPC 8namespace OpenSim.Region.ScriptEngine.Common.TRPC
9{ 9{
10 public class TCPServer: TCPCommon.ServerInterface 10 public class TCPServer: TCPCommon.ServerInterface
11 { 11 {
12 public readonly int LocalPort; 12 public readonly int LocalPort;
13 public TCPServer(int localPort) 13 public TCPServer(int localPort)
14 { 14 {
15 LocalPort = localPort; 15 LocalPort = localPort;
16 } 16 }
17 17
18 private Socket server; 18 private Socket server;
19 19
20 /// <summary> 20 /// <summary>
21 /// Starts listening for new connections 21 /// Starts listening for new connections
22 /// </summary> 22 /// </summary>
23 public void StartListen() 23 public void StartListen()
24 { 24 {
25 server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 25 server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
26 IPEndPoint ipe = new IPEndPoint(IPAddress.Any, LocalPort); 26 IPEndPoint ipe = new IPEndPoint(IPAddress.Any, LocalPort);
27 server.Bind(ipe); 27 server.Bind(ipe);
28 server.Listen(10); 28 server.Listen(10);
29 server.BeginAccept(new AsyncCallback(AsyncAcceptConnections), server); 29 server.BeginAccept(new AsyncCallback(AsyncAcceptConnections), server);
30 } 30 }
31 /// <summary> 31 /// <summary>
32 /// Stops listening for new connections 32 /// Stops listening for new connections
33 /// </summary> 33 /// </summary>
34 public void StopListen() 34 public void StopListen()
35 { 35 {
36 server.Close(); 36 server.Close();
37 server = null; 37 server = null;
38 } 38 }
39 39
40 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>(); 40 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>();
41 private int ClientCount = 0; 41 private int ClientCount = 0;
42 42
43 43
44 public event TCPCommon.ClientConnectedDelegate ClientConnected; 44 public event TCPCommon.ClientConnectedDelegate ClientConnected;
45 public event TCPCommon.DataReceivedDelegate DataReceived; 45 public event TCPCommon.DataReceivedDelegate DataReceived;
46 public event TCPCommon.DataSentDelegate DataSent; 46 public event TCPCommon.DataSentDelegate DataSent;
47 public event TCPCommon.CloseDelegate Close; 47 public event TCPCommon.CloseDelegate Close;
48 48
49 /// <summary> 49 /// <summary>
50 /// Async callback for new connections 50 /// Async callback for new connections
51 /// </summary> 51 /// </summary>
52 /// <param name="ar"></param> 52 /// <param name="ar"></param>
53 private void AsyncAcceptConnections(IAsyncResult ar) 53 private void AsyncAcceptConnections(IAsyncResult ar)
54 { 54 {
55 int id = ClientCount++; 55 int id = ClientCount++;
56 Socket oldserver = (Socket)ar.AsyncState; 56 Socket oldserver = (Socket)ar.AsyncState;
57 Socket client = oldserver.EndAccept(ar); 57 Socket client = oldserver.EndAccept(ar);
58 TCPSocket S = new TCPSocket(id, client); 58 TCPSocket S = new TCPSocket(id, client);
59 59
60 // Add to dictionary 60 // Add to dictionary
61 Clients.Add(id, S); 61 Clients.Add(id, S);
62 62
63 // Add event handlers 63 // Add event handlers
64 S.Close += new TCPSocket.CloseDelegate(S_Close); 64 S.Close += new TCPSocket.CloseDelegate(S_Close);
65 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived); 65 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived);
66 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent); 66 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent);
67 67
68 // Start it 68 // Start it
69 S.Start(); 69 S.Start();
70 70
71 Debug.WriteLine("Connection received: " + client.RemoteEndPoint.ToString()); 71 Debug.WriteLine("Connection received: " + client.RemoteEndPoint.ToString());
72 72
73 // Fire Connected-event 73 // Fire Connected-event
74 if (ClientConnected != null) 74 if (ClientConnected != null)
75 ClientConnected(id, client.RemoteEndPoint); 75 ClientConnected(id, client.RemoteEndPoint);
76 76
77 } 77 }
78 78
79 void S_DataSent(int ID, int length) 79 void S_DataSent(int ID, int length)
80 { 80 {
81 if (DataSent != null) 81 if (DataSent != null)
82 DataSent(ID, length); 82 DataSent(ID, length);
83 } 83 }
84 84
85 void S_DataReceived(int ID, byte[] data, int offset, int length) 85 void S_DataReceived(int ID, byte[] data, int offset, int length)
86 { 86 {
87 if (DataReceived != null) 87 if (DataReceived != null)
88 DataReceived(ID, data, offset, length); 88 DataReceived(ID, data, offset, length);
89 } 89 }
90 90
91 void S_Close(int ID) 91 void S_Close(int ID)
92 { 92 {
93 if (Close != null) 93 if (Close != null)
94 Close(ID); 94 Close(ID);
95 Clients.Remove(ID); 95 Clients.Remove(ID);
96 } 96 }
97 97
98 public void Send(int clientID, byte[] data, int offset, int len) 98 public void Send(int clientID, byte[] data, int offset, int len)
99 { 99 {
100 Clients[clientID].Send(clientID, data, offset, len); 100 Clients[clientID].Send(clientID, data, offset, len);
101 } 101 }
102 102
103 103
104 104
105 } 105 }
106} \ No newline at end of file 106} \ No newline at end of file
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs
index 1079846..ce50a27 100644
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs
+++ b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs
@@ -1,86 +1,86 @@
1using System; 1using System;
2using System.Net.Sockets; 2using System.Net.Sockets;
3 3
4namespace OpenSim.Region.ScriptEngine.Common.TRPC 4namespace OpenSim.Region.ScriptEngine.Common.TRPC
5{ 5{
6 public class TCPSocket 6 public class TCPSocket
7 { 7 {
8 8
9 public readonly Socket Client; 9 public readonly Socket Client;
10 public readonly int ID; 10 public readonly int ID;
11 11
12 public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length); 12 public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length);
13 public delegate void DataSentDelegate(int ID, int length); 13 public delegate void DataSentDelegate(int ID, int length);
14 public delegate void CloseDelegate(int ID); 14 public delegate void CloseDelegate(int ID);
15 public event DataReceivedDelegate DataReceived; 15 public event DataReceivedDelegate DataReceived;
16 public event DataSentDelegate DataSent; 16 public event DataSentDelegate DataSent;
17 public event CloseDelegate Close; 17 public event CloseDelegate Close;
18 18
19 private byte[] RecvQueue = new byte[4096]; 19 private byte[] RecvQueue = new byte[4096];
20 private int RecvQueueSize = 4096; 20 private int RecvQueueSize = 4096;
21 21
22 public TCPSocket(int id, Socket client) 22 public TCPSocket(int id, Socket client)
23 { 23 {
24 ID = id; 24 ID = id;
25 Client = client; 25 Client = client;
26 } 26 }
27 public void Start() 27 public void Start()
28 { 28 {
29 // Start listening 29 // Start listening
30 BeginReceive(); 30 BeginReceive();
31 } 31 }
32 32
33 private void BeginReceive() 33 private void BeginReceive()
34 { 34 {
35 Client.BeginReceive(RecvQueue, 0, RecvQueueSize, SocketFlags.None, new AsyncCallback(asyncDataReceived), Client); 35 Client.BeginReceive(RecvQueue, 0, RecvQueueSize, SocketFlags.None, new AsyncCallback(asyncDataReceived), Client);
36 } 36 }
37 37
38 /// <summary> 38 /// <summary>
39 /// Callback for successful receive (or connection close) 39 /// Callback for successful receive (or connection close)
40 /// </summary> 40 /// </summary>
41 /// <param name="ar"></param> 41 /// <param name="ar"></param>
42 private void asyncDataReceived(IAsyncResult ar) 42 private void asyncDataReceived(IAsyncResult ar)
43 { 43 {
44 Socket client = (Socket)ar.AsyncState; 44 Socket client = (Socket)ar.AsyncState;
45 int recv = client.EndReceive(ar); 45 int recv = client.EndReceive(ar);
46 46
47 // Is connection closed? 47 // Is connection closed?
48 if (recv == 0) 48 if (recv == 0)
49 { 49 {
50 client.Close(); 50 client.Close();
51 Close(ID); 51 Close(ID);
52 return; 52 return;
53 } 53 }
54 54
55 // Call receive event 55 // Call receive event
56 DataReceived(ID, RecvQueue, 0, recv); 56 DataReceived(ID, RecvQueue, 0, recv);
57 57
58 // Start new receive 58 // Start new receive
59 BeginReceive(); 59 BeginReceive();
60 60
61 } 61 }
62 62
63 63
64 public void Send(int clientID, byte[] data, int offset, int len) 64 public void Send(int clientID, byte[] data, int offset, int len)
65 { 65 {
66 Client.BeginSend(data, offset, len, SocketFlags.None, new AsyncCallback(asyncDataSent), Client); 66 Client.BeginSend(data, offset, len, SocketFlags.None, new AsyncCallback(asyncDataSent), Client);
67 } 67 }
68 68
69 /// <summary> 69 /// <summary>
70 /// Callback for successful send 70 /// Callback for successful send
71 /// </summary> 71 /// </summary>
72 /// <param name="ar"></param> 72 /// <param name="ar"></param>
73 void asyncDataSent(IAsyncResult ar) 73 void asyncDataSent(IAsyncResult ar)
74 { 74 {
75 Socket client = (Socket)ar.AsyncState; 75 Socket client = (Socket)ar.AsyncState;
76 int sent = client.EndSend(ar); 76 int sent = client.EndSend(ar);
77 DataSent(ID, sent); 77 DataSent(ID, sent);
78 } 78 }
79 79
80 public void Disconnect() 80 public void Disconnect()
81 { 81 {
82 Client.Close(); 82 Client.Close();
83 Close(ID); 83 Close(ID);
84 } 84 }
85 } 85 }
86} \ No newline at end of file 86} \ No newline at end of file
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 @@
1using System; 1using System;
2using System.Collections.Generic; 2using System.Collections.Generic;
3using System.Diagnostics; 3using System.Diagnostics;
4using System.Text; 4using System.Text;
5using OpenSim.Region.ScriptEngine.Common.TRPC; 5using OpenSim.Region.ScriptEngine.Common.TRPC;
6 6
7namespace OpenSim.Region.ScriptEngine.Common 7namespace 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