aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Common/TRPC
diff options
context:
space:
mode:
authorJeff Ames2008-01-12 01:35:25 +0000
committerJeff Ames2008-01-12 01:35:25 +0000
commit5d7e120d56873d91141695ddf7aebd88e4619dce (patch)
tree79e5ae7fcb518dfc726e447a029744b05204001c /OpenSim/Region/ScriptEngine/Common/TRPC
parentAnd one last time - removed duplicate exception dump :) (diff)
downloadopensim-SC_OLD-5d7e120d56873d91141695ddf7aebd88e4619dce.zip
opensim-SC_OLD-5d7e120d56873d91141695ddf7aebd88e4619dce.tar.gz
opensim-SC_OLD-5d7e120d56873d91141695ddf7aebd88e4619dce.tar.bz2
opensim-SC_OLD-5d7e120d56873d91141695ddf7aebd88e4619dce.tar.xz
Set svn:eol-style.
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/TRPC')
-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
4 files changed, 346 insertions, 346 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