diff options
author | Tedd Hansen | 2008-01-12 00:48:58 +0000 |
---|---|---|
committer | Tedd Hansen | 2008-01-12 00:48:58 +0000 |
commit | 1e9a66cbaae97759c5c4e936664b5cc7a4feca89 (patch) | |
tree | 902166cca5b637ca85d97a6d8b8d941a4ed4a138 /OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs | |
parent | Fix some warnings under mono. (diff) | |
download | opensim-SC-1e9a66cbaae97759c5c4e936664b5cc7a4feca89.zip opensim-SC-1e9a66cbaae97759c5c4e936664b5cc7a4feca89.tar.gz opensim-SC-1e9a66cbaae97759c5c4e936664b5cc7a4feca89.tar.bz2 opensim-SC-1e9a66cbaae97759c5c4e936664b5cc7a4feca89.tar.xz |
ScriptServer communication protocol (v1), primitive RPC-like TCP client/server
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs | 109 |
1 files changed, 109 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 | ||