aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs210
1 files changed, 105 insertions, 105 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
index 3af898a..0a85dc4 100644
--- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
+++ b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs
@@ -1,106 +1,106 @@
1using System; 1using System;
2using System.Collections.Generic; 2using System.Collections.Generic;
3using System.Diagnostics; 3using System.Diagnostics;
4using System.Net; 4using System.Net;
5using System.Net.Sockets; 5using System.Net.Sockets;
6using TCPCommon=OpenSim.Region.ScriptEngine.Common.TRPC.TCPCommon; 6using TCPCommon=OpenSim.Region.ScriptEngine.Common.TRPC.TCPCommon;
7 7
8namespace OpenSim.Region.ScriptEngine.Common.TRPC 8namespace OpenSim.Region.ScriptEngine.Common.TRPC
9{ 9{
10 public class TCPServer: TCPCommon.ServerInterface 10 public class TCPServer: TCPCommon.ServerInterface
11 { 11 {
12 public readonly int LocalPort; 12 public readonly int LocalPort;
13 public TCPServer(int localPort) 13 public TCPServer(int localPort)
14 { 14 {
15 LocalPort = localPort; 15 LocalPort = localPort;
16 } 16 }
17 17
18 private Socket server; 18 private Socket server;
19 19
20 /// <summary> 20 /// <summary>
21 /// Starts listening for new connections 21 /// Starts listening for new connections
22 /// </summary> 22 /// </summary>
23 public void StartListen() 23 public void StartListen()
24 { 24 {
25 server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 25 server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
26 IPEndPoint ipe = new IPEndPoint(IPAddress.Any, LocalPort); 26 IPEndPoint ipe = new IPEndPoint(IPAddress.Any, LocalPort);
27 server.Bind(ipe); 27 server.Bind(ipe);
28 server.Listen(10); 28 server.Listen(10);
29 server.BeginAccept(new AsyncCallback(AsyncAcceptConnections), server); 29 server.BeginAccept(new AsyncCallback(AsyncAcceptConnections), server);
30 } 30 }
31 /// <summary> 31 /// <summary>
32 /// Stops listening for new connections 32 /// Stops listening for new connections
33 /// </summary> 33 /// </summary>
34 public void StopListen() 34 public void StopListen()
35 { 35 {
36 server.Close(); 36 server.Close();
37 server = null; 37 server = null;
38 } 38 }
39 39
40 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>(); 40 private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>();
41 private int ClientCount = 0; 41 private int ClientCount = 0;
42 42
43 43
44 public event TCPCommon.ClientConnectedDelegate ClientConnected; 44 public event TCPCommon.ClientConnectedDelegate ClientConnected;
45 public event TCPCommon.DataReceivedDelegate DataReceived; 45 public event TCPCommon.DataReceivedDelegate DataReceived;
46 public event TCPCommon.DataSentDelegate DataSent; 46 public event TCPCommon.DataSentDelegate DataSent;
47 public event TCPCommon.CloseDelegate Close; 47 public event TCPCommon.CloseDelegate Close;
48 48
49 /// <summary> 49 /// <summary>
50 /// Async callback for new connections 50 /// Async callback for new connections
51 /// </summary> 51 /// </summary>
52 /// <param name="ar"></param> 52 /// <param name="ar"></param>
53 private void AsyncAcceptConnections(IAsyncResult ar) 53 private void AsyncAcceptConnections(IAsyncResult ar)
54 { 54 {
55 int id = ClientCount++; 55 int id = ClientCount++;
56 Socket oldserver = (Socket)ar.AsyncState; 56 Socket oldserver = (Socket)ar.AsyncState;
57 Socket client = oldserver.EndAccept(ar); 57 Socket client = oldserver.EndAccept(ar);
58 TCPSocket S = new TCPSocket(id, client); 58 TCPSocket S = new TCPSocket(id, client);
59 59
60 // Add to dictionary 60 // Add to dictionary
61 Clients.Add(id, S); 61 Clients.Add(id, S);
62 62
63 // Add event handlers 63 // Add event handlers
64 S.Close += new TCPSocket.CloseDelegate(S_Close); 64 S.Close += new TCPSocket.CloseDelegate(S_Close);
65 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived); 65 S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived);
66 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent); 66 S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent);
67 67
68 // Start it 68 // Start it
69 S.Start(); 69 S.Start();
70 70
71 Debug.WriteLine("Connection received: " + client.RemoteEndPoint.ToString()); 71 Debug.WriteLine("Connection received: " + client.RemoteEndPoint.ToString());
72 72
73 // Fire Connected-event 73 // Fire Connected-event
74 if (ClientConnected != null) 74 if (ClientConnected != null)
75 ClientConnected(id, client.RemoteEndPoint); 75 ClientConnected(id, client.RemoteEndPoint);
76 76
77 } 77 }
78 78
79 void S_DataSent(int ID, int length) 79 void S_DataSent(int ID, int length)
80 { 80 {
81 if (DataSent != null) 81 if (DataSent != null)
82 DataSent(ID, length); 82 DataSent(ID, length);
83 } 83 }
84 84
85 void S_DataReceived(int ID, byte[] data, int offset, int length) 85 void S_DataReceived(int ID, byte[] data, int offset, int length)
86 { 86 {
87 if (DataReceived != null) 87 if (DataReceived != null)
88 DataReceived(ID, data, offset, length); 88 DataReceived(ID, data, offset, length);
89 } 89 }
90 90
91 void S_Close(int ID) 91 void S_Close(int ID)
92 { 92 {
93 if (Close != null) 93 if (Close != null)
94 Close(ID); 94 Close(ID);
95 Clients.Remove(ID); 95 Clients.Remove(ID);
96 } 96 }
97 97
98 public void Send(int clientID, byte[] data, int offset, int len) 98 public void Send(int clientID, byte[] data, int offset, int len)
99 { 99 {
100 Clients[clientID].Send(clientID, data, offset, len); 100 Clients[clientID].Send(clientID, data, offset, len);
101 } 101 }
102 102
103 103
104 104
105 } 105 }
106} \ No newline at end of file 106} \ No newline at end of file