diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs deleted file mode 100644 index 26892a2..0000000 --- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs +++ /dev/null | |||
@@ -1,128 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Diagnostics; | ||
31 | using System.Net; | ||
32 | using System.Net.Sockets; | ||
33 | |||
34 | namespace OpenSim.Region.ScriptEngine.Common.TRPC | ||
35 | { | ||
36 | public class TCPServer: TCPCommon.ServerInterface | ||
37 | { | ||
38 | public readonly int LocalPort; | ||
39 | public TCPServer(int localPort) | ||
40 | { | ||
41 | LocalPort = localPort; | ||
42 | } | ||
43 | |||
44 | private Socket server; | ||
45 | |||
46 | /// <summary> | ||
47 | /// Starts listening for new connections | ||
48 | /// </summary> | ||
49 | public void StartListen() | ||
50 | { | ||
51 | server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
52 | IPEndPoint ipe = new IPEndPoint(IPAddress.Any, LocalPort); | ||
53 | server.Bind(ipe); | ||
54 | server.Listen(10); | ||
55 | server.BeginAccept(new AsyncCallback(AsyncAcceptConnections), server); | ||
56 | } | ||
57 | /// <summary> | ||
58 | /// Stops listening for new connections | ||
59 | /// </summary> | ||
60 | public void StopListen() | ||
61 | { | ||
62 | server.Close(); | ||
63 | server = null; | ||
64 | } | ||
65 | |||
66 | private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>(); | ||
67 | private int ClientCount = 0; | ||
68 | |||
69 | public event TCPCommon.ClientConnectedDelegate ClientConnected; | ||
70 | public event TCPCommon.DataReceivedDelegate DataReceived; | ||
71 | public event TCPCommon.DataSentDelegate DataSent; | ||
72 | public event TCPCommon.CloseDelegate Close; | ||
73 | |||
74 | /// <summary> | ||
75 | /// Async callback for new connections | ||
76 | /// </summary> | ||
77 | /// <param name="ar"></param> | ||
78 | private void AsyncAcceptConnections(IAsyncResult ar) | ||
79 | { | ||
80 | int id = ClientCount++; | ||
81 | Socket oldserver = (Socket)ar.AsyncState; | ||
82 | Socket client = oldserver.EndAccept(ar); | ||
83 | TCPSocket S = new TCPSocket(id, client); | ||
84 | |||
85 | // Add to dictionary | ||
86 | Clients.Add(id, S); | ||
87 | |||
88 | // Add event handlers | ||
89 | S.Close += new TCPSocket.CloseDelegate(S_Close); | ||
90 | S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived); | ||
91 | S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent); | ||
92 | |||
93 | // Start it | ||
94 | S.Start(); | ||
95 | |||
96 | Debug.WriteLine("Connection received: " + client.RemoteEndPoint.ToString()); | ||
97 | |||
98 | // Fire Connected-event | ||
99 | if (ClientConnected != null) | ||
100 | ClientConnected(id, client.RemoteEndPoint); | ||
101 | |||
102 | } | ||
103 | |||
104 | void S_DataSent(int ID, int length) | ||
105 | { | ||
106 | if (DataSent != null) | ||
107 | DataSent(ID, length); | ||
108 | } | ||
109 | |||
110 | void S_DataReceived(int ID, byte[] data, int offset, int length) | ||
111 | { | ||
112 | if (DataReceived != null) | ||
113 | DataReceived(ID, data, offset, length); | ||
114 | } | ||
115 | |||
116 | void S_Close(int ID) | ||
117 | { | ||
118 | if (Close != null) | ||
119 | Close(ID); | ||
120 | Clients.Remove(ID); | ||
121 | } | ||
122 | |||
123 | public void Send(int clientID, byte[] data, int offset, int len) | ||
124 | { | ||
125 | Clients[clientID].Send(clientID, data, offset, len); | ||
126 | } | ||
127 | } | ||
128 | } | ||