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