diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/TCPJSONStream/ClientNetworkContext.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/TCPJSONStream/ClientNetworkContext.cs | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/TCPJSONStream/ClientNetworkContext.cs b/OpenSim/Region/ClientStack/TCPJSONStream/ClientNetworkContext.cs new file mode 100644 index 0000000..591f817 --- /dev/null +++ b/OpenSim/Region/ClientStack/TCPJSONStream/ClientNetworkContext.cs | |||
@@ -0,0 +1,143 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.IO; | ||
4 | using System.Net; | ||
5 | using System.Net.Sockets; | ||
6 | using System.Text; | ||
7 | |||
8 | namespace OpenSim.Region.ClientStack.TCPJSONStream | ||
9 | { | ||
10 | public class ClientNetworkContext | ||
11 | { | ||
12 | private Socket _socket; | ||
13 | private string _remoteAddress; | ||
14 | private string _remotePort; | ||
15 | private WebSocketConnectionStage _wsConnectionStatus = WebSocketConnectionStage.Accept; | ||
16 | private int _bytesLeft; | ||
17 | private NetworkStream _stream; | ||
18 | private byte[] _buffer; | ||
19 | public event EventHandler<DisconnectedEventArgs> Disconnected = delegate { }; | ||
20 | |||
21 | public ClientNetworkContext(IPEndPoint endPoint, int port, Stream stream, int buffersize, Socket sock) | ||
22 | { | ||
23 | _socket = sock; | ||
24 | _remoteAddress = endPoint.Address.ToString(); | ||
25 | _remotePort = port.ToString(); | ||
26 | _stream = stream as NetworkStream; | ||
27 | _buffer = new byte[buffersize]; | ||
28 | |||
29 | |||
30 | } | ||
31 | |||
32 | public void BeginRead() | ||
33 | { | ||
34 | _wsConnectionStatus = WebSocketConnectionStage.Http; | ||
35 | try | ||
36 | { | ||
37 | _stream.BeginRead(_buffer, 0, _buffer.Length, OnReceive, _wsConnectionStatus); | ||
38 | } | ||
39 | catch (IOException err) | ||
40 | { | ||
41 | //m_log.Debug(err.ToString()); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | private void OnReceive(IAsyncResult ar) | ||
46 | { | ||
47 | try | ||
48 | { | ||
49 | int bytesRead = _stream.EndRead(ar); | ||
50 | if (bytesRead == 0) | ||
51 | { | ||
52 | |||
53 | Disconnected(this, new DisconnectedEventArgs(SocketError.ConnectionReset)); | ||
54 | return; | ||
55 | } | ||
56 | |||
57 | } | ||
58 | } | ||
59 | /// <summary> | ||
60 | /// send a whole buffer | ||
61 | /// </summary> | ||
62 | /// <param name="buffer">buffer to send</param> | ||
63 | /// <exception cref="ArgumentNullException"></exception> | ||
64 | public void Send(byte[] buffer) | ||
65 | { | ||
66 | if (buffer == null) | ||
67 | throw new ArgumentNullException("buffer"); | ||
68 | Send(buffer, 0, buffer.Length); | ||
69 | } | ||
70 | |||
71 | /// <summary> | ||
72 | /// Send data using the stream | ||
73 | /// </summary> | ||
74 | /// <param name="buffer">Contains data to send</param> | ||
75 | /// <param name="offset">Start position in buffer</param> | ||
76 | /// <param name="size">number of bytes to send</param> | ||
77 | /// <exception cref="ArgumentNullException"></exception> | ||
78 | /// <exception cref="ArgumentOutOfRangeException"></exception> | ||
79 | public void Send(byte[] buffer, int offset, int size) | ||
80 | { | ||
81 | |||
82 | if (offset + size > buffer.Length) | ||
83 | throw new ArgumentOutOfRangeException("offset", offset, "offset + size is beyond end of buffer."); | ||
84 | |||
85 | if (_stream != null && _stream.CanWrite) | ||
86 | { | ||
87 | try | ||
88 | { | ||
89 | _stream.Write(buffer, offset, size); | ||
90 | } | ||
91 | catch (IOException) | ||
92 | { | ||
93 | |||
94 | } | ||
95 | } | ||
96 | |||
97 | } | ||
98 | private void Reset() | ||
99 | { | ||
100 | if (_stream == null) | ||
101 | return; | ||
102 | _stream.Dispose(); | ||
103 | _stream = null; | ||
104 | if (_socket == null) | ||
105 | return; | ||
106 | if (_socket.Connected) | ||
107 | _socket.Disconnect(true); | ||
108 | _socket = null; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | public enum WebSocketConnectionStage | ||
113 | { | ||
114 | Reuse, | ||
115 | Accept, | ||
116 | Http, | ||
117 | WebSocket, | ||
118 | Closed | ||
119 | } | ||
120 | |||
121 | public enum FrameOpCodesRFC6455 | ||
122 | { | ||
123 | Continue = 0x0, | ||
124 | Text = 0x1, | ||
125 | Binary = 0x2, | ||
126 | Close = 0x8, | ||
127 | Ping = 0x9, | ||
128 | Pong = 0xA | ||
129 | } | ||
130 | |||
131 | public enum DataState | ||
132 | { | ||
133 | Empty = 0, | ||
134 | Waiting = 1, | ||
135 | Receiving = 2, | ||
136 | Complete = 3, | ||
137 | Closed = 4, | ||
138 | Ping = 5, | ||
139 | Pong = 6 | ||
140 | } | ||
141 | |||
142 | |||
143 | } | ||