diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs | 248 |
1 files changed, 124 insertions, 124 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 @@ | |||
1 | using System; | 1 | using System; |
2 | using System.Collections.Generic; | 2 | using System.Collections.Generic; |
3 | using System.Diagnostics; | 3 | using System.Diagnostics; |
4 | using System.Net; | 4 | using System.Net; |
5 | using System.Net.Sockets; | 5 | using System.Net.Sockets; |
6 | using System.Text; | 6 | using System.Text; |
7 | 7 | ||
8 | namespace OpenSim.Region.ScriptEngine.Common.TRPC | 8 | namespace 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 |