diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/TRPC')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs | 33 | ||||
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs | 141 | ||||
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs | 62 | ||||
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPServer.cs | 128 | ||||
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs | 111 |
5 files changed, 0 insertions, 475 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs deleted file mode 100644 index 14c6078..0000000 --- a/OpenSim/Region/ScriptEngine/Common/TRPC/MyBase.cs +++ /dev/null | |||
@@ -1,33 +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 | namespace OpenSim.Region.ScriptEngine.Common.TRPC | ||
29 | { | ||
30 | class MyBase | ||
31 | { | ||
32 | } | ||
33 | } | ||
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs deleted file mode 100644 index 624c3bb..0000000 --- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPClient.cs +++ /dev/null | |||
@@ -1,141 +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.Collections.Generic; | ||
29 | using System.Diagnostics; | ||
30 | using System.Net; | ||
31 | using System.Net.Sockets; | ||
32 | |||
33 | namespace OpenSim.Region.ScriptEngine.Common.TRPC | ||
34 | { | ||
35 | public class TCPClient : TCPCommon.ClientInterface | ||
36 | { | ||
37 | public TCPClient() | ||
38 | { | ||
39 | } | ||
40 | |||
41 | private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>(); | ||
42 | private int ClientCount = 0; | ||
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 | public event TCPCommon.ConnectErrorDelegate ConnectError; | ||
49 | |||
50 | /// <summary> | ||
51 | /// Creates client connection | ||
52 | /// </summary> | ||
53 | public void Connect(string RemoteHost, int RemotePort) | ||
54 | { | ||
55 | Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
56 | IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort); | ||
57 | //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock); | ||
58 | newsock.Connect(ipe); | ||
59 | } | ||
60 | |||
61 | public int ConnectAndReturnID(string RemoteHost, int RemotePort) | ||
62 | { | ||
63 | Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
64 | IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort); | ||
65 | //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock); | ||
66 | newsock.Connect(ipe); | ||
67 | return ProcessConnection(newsock); | ||
68 | } | ||
69 | |||
70 | public void Disconnect(int ID) | ||
71 | { | ||
72 | Clients[ID].Disconnect(); | ||
73 | } | ||
74 | |||
75 | // TODO: unused | ||
76 | // void asyncConnected(IAsyncResult iar) | ||
77 | // { | ||
78 | // Socket client = (Socket)iar.AsyncState; | ||
79 | // client.EndConnect(iar); | ||
80 | // ProcessConnection(client); | ||
81 | // } | ||
82 | |||
83 | private int ProcessConnection(Socket client) | ||
84 | { | ||
85 | try | ||
86 | { | ||
87 | int id = ClientCount++; | ||
88 | TCPSocket S = new TCPSocket(id, client); | ||
89 | |||
90 | // Add to dictionary | ||
91 | Clients.Add(id, S); | ||
92 | |||
93 | // Add event handlers | ||
94 | S.Close += new TCPSocket.CloseDelegate(S_Close); | ||
95 | S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived); | ||
96 | S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent); | ||
97 | |||
98 | // Start it | ||
99 | S.Start(); | ||
100 | |||
101 | Debug.WriteLine("Connection established: " + client.RemoteEndPoint.ToString()); | ||
102 | |||
103 | // Fire Connected-event | ||
104 | if (ClientConnected != null) | ||
105 | ClientConnected(id, client.RemoteEndPoint); | ||
106 | |||
107 | return id; | ||
108 | } | ||
109 | catch (SocketException sex) | ||
110 | { | ||
111 | if (ConnectError != null) | ||
112 | ConnectError(sex.Message); | ||
113 | } | ||
114 | return -1; | ||
115 | } | ||
116 | |||
117 | void S_DataSent(int ID, int length) | ||
118 | { | ||
119 | if (DataSent != null) | ||
120 | DataSent(ID, length); | ||
121 | } | ||
122 | |||
123 | void S_DataReceived(int ID, byte[] data, int offset, int length) | ||
124 | { | ||
125 | if (DataReceived != null) | ||
126 | DataReceived(ID, data, offset, length); | ||
127 | } | ||
128 | |||
129 | void S_Close(int ID) | ||
130 | { | ||
131 | if (Close != null) | ||
132 | Close(ID); | ||
133 | Clients.Remove(ID); | ||
134 | } | ||
135 | |||
136 | public void Send(int clientID, byte[] data, int offset, int len) | ||
137 | { | ||
138 | Clients[clientID].Send(clientID, data, offset, len); | ||
139 | } | ||
140 | } | ||
141 | } | ||
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs deleted file mode 100644 index ab9d9b4..0000000 --- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPCommon.cs +++ /dev/null | |||
@@ -1,62 +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.Net; | ||
29 | |||
30 | namespace OpenSim.Region.ScriptEngine.Common.TRPC | ||
31 | { | ||
32 | public class TCPCommon | ||
33 | { | ||
34 | public delegate void ClientConnectedDelegate(int ID, EndPoint Remote); | ||
35 | public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length); | ||
36 | public delegate void DataSentDelegate(int ID, int length); | ||
37 | public delegate void CloseDelegate(int ID); | ||
38 | public delegate void ConnectErrorDelegate(string Reason); | ||
39 | |||
40 | public interface ServerAndClientInterface | ||
41 | { | ||
42 | void Send(int clientID, byte[] data, int offset, int len); | ||
43 | event ClientConnectedDelegate ClientConnected; | ||
44 | event DataReceivedDelegate DataReceived; | ||
45 | event DataSentDelegate DataSent; | ||
46 | event CloseDelegate Close; | ||
47 | } | ||
48 | |||
49 | public interface ClientInterface : ServerAndClientInterface | ||
50 | { | ||
51 | event ConnectErrorDelegate ConnectError; | ||
52 | void Connect(string RemoteHost, int RemotePort); | ||
53 | void Disconnect(int ID); | ||
54 | } | ||
55 | |||
56 | public interface ServerInterface : ServerAndClientInterface | ||
57 | { | ||
58 | void StartListen(); | ||
59 | void StopListen(); | ||
60 | } | ||
61 | } | ||
62 | } | ||
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 | } | ||
diff --git a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs b/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs deleted file mode 100644 index 693ec6f..0000000 --- a/OpenSim/Region/ScriptEngine/Common/TRPC/TCPSocket.cs +++ /dev/null | |||
@@ -1,111 +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.Net.Sockets; | ||
30 | |||
31 | namespace OpenSim.Region.ScriptEngine.Common.TRPC | ||
32 | { | ||
33 | public class TCPSocket | ||
34 | { | ||
35 | public readonly Socket Client; | ||
36 | public readonly int ID; | ||
37 | |||
38 | public delegate void DataReceivedDelegate(int ID, byte[] data, int offset, int length); | ||
39 | public delegate void DataSentDelegate(int ID, int length); | ||
40 | public delegate void CloseDelegate(int ID); | ||
41 | public event DataReceivedDelegate DataReceived; | ||
42 | public event DataSentDelegate DataSent; | ||
43 | public event CloseDelegate Close; | ||
44 | |||
45 | private byte[] RecvQueue = new byte[4096]; | ||
46 | private int RecvQueueSize = 4096; | ||
47 | |||
48 | public TCPSocket(int id, Socket client) | ||
49 | { | ||
50 | ID = id; | ||
51 | Client = client; | ||
52 | } | ||
53 | |||
54 | public void Start() | ||
55 | { | ||
56 | // Start listening | ||
57 | BeginReceive(); | ||
58 | } | ||
59 | |||
60 | private void BeginReceive() | ||
61 | { | ||
62 | Client.BeginReceive(RecvQueue, 0, RecvQueueSize, SocketFlags.None, new AsyncCallback(asyncDataReceived), Client); | ||
63 | } | ||
64 | |||
65 | /// <summary> | ||
66 | /// Callback for successful receive (or connection close) | ||
67 | /// </summary> | ||
68 | /// <param name="ar"></param> | ||
69 | private void asyncDataReceived(IAsyncResult ar) | ||
70 | { | ||
71 | Socket client = (Socket)ar.AsyncState; | ||
72 | int recv = client.EndReceive(ar); | ||
73 | |||
74 | // Is connection closed? | ||
75 | if (recv == 0) | ||
76 | { | ||
77 | client.Close(); | ||
78 | Close(ID); | ||
79 | return; | ||
80 | } | ||
81 | |||
82 | // Call receive event | ||
83 | DataReceived(ID, RecvQueue, 0, recv); | ||
84 | |||
85 | // Start new receive | ||
86 | BeginReceive(); | ||
87 | } | ||
88 | |||
89 | public void Send(int clientID, byte[] data, int offset, int len) | ||
90 | { | ||
91 | Client.BeginSend(data, offset, len, SocketFlags.None, new AsyncCallback(asyncDataSent), Client); | ||
92 | } | ||
93 | |||
94 | /// <summary> | ||
95 | /// Callback for successful send | ||
96 | /// </summary> | ||
97 | /// <param name="ar"></param> | ||
98 | void asyncDataSent(IAsyncResult ar) | ||
99 | { | ||
100 | Socket client = (Socket)ar.AsyncState; | ||
101 | int sent = client.EndSend(ar); | ||
102 | DataSent(ID, sent); | ||
103 | } | ||
104 | |||
105 | public void Disconnect() | ||
106 | { | ||
107 | Client.Close(); | ||
108 | Close(ID); | ||
109 | } | ||
110 | } | ||
111 | } | ||