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/TCPServer.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/TCPServer.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs | 106 |
1 files changed, 106 insertions, 0 deletions
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 | ||