aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/TCPJSONStream/ClientNetworkContext.cs
blob: 591f817b8fa92b3f19c900f4480142bda74e9e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace OpenSim.Region.ClientStack.TCPJSONStream
{
    public class ClientNetworkContext
    {
        private Socket _socket;
        private string  _remoteAddress;
        private string _remotePort;
        private WebSocketConnectionStage _wsConnectionStatus = WebSocketConnectionStage.Accept;
        private int _bytesLeft;
        private NetworkStream _stream;
        private byte[] _buffer;
        public event EventHandler<DisconnectedEventArgs> Disconnected = delegate { };

        public ClientNetworkContext(IPEndPoint endPoint, int port, Stream stream, int buffersize, Socket sock)
        {
            _socket = sock;
            _remoteAddress = endPoint.Address.ToString();
            _remotePort = port.ToString();
            _stream = stream as NetworkStream;
            _buffer = new byte[buffersize];
            

        }

        public void BeginRead()
        {
            _wsConnectionStatus = WebSocketConnectionStage.Http;
            try
            {
                _stream.BeginRead(_buffer, 0, _buffer.Length, OnReceive, _wsConnectionStatus);
            }
            catch (IOException err)
            {
                //m_log.Debug(err.ToString());
            }
        }

        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                int bytesRead = _stream.EndRead(ar);
                if (bytesRead == 0)
                {

                    Disconnected(this, new DisconnectedEventArgs(SocketError.ConnectionReset));
                    return;
                }

            }
        }
        /// <summary>
        /// send a whole buffer
        /// </summary>
        /// <param name="buffer">buffer to send</param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Send(byte[] buffer)
        {
            if (buffer == null)
                throw new ArgumentNullException("buffer");
            Send(buffer, 0, buffer.Length);
        }

        /// <summary>
        /// Send data using the stream
        /// </summary>
        /// <param name="buffer">Contains data to send</param>
        /// <param name="offset">Start position in buffer</param>
        /// <param name="size">number of bytes to send</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public void Send(byte[] buffer, int offset, int size)
        {

            if (offset + size > buffer.Length)
                throw new ArgumentOutOfRangeException("offset", offset, "offset + size is beyond end of buffer.");

            if (_stream != null && _stream.CanWrite)
            {
                try
                {
                    _stream.Write(buffer, offset, size);
                }
                catch (IOException)
                {

                }
            }

        }
        private void Reset()
        {
            if (_stream == null)
                return;
            _stream.Dispose();
            _stream = null;
            if (_socket == null)
                return;
            if (_socket.Connected)
                _socket.Disconnect(true);
            _socket = null;
        }
    }

    public enum WebSocketConnectionStage
    {
        Reuse,
        Accept,
        Http,
        WebSocket, 
        Closed
    }

    public enum FrameOpCodesRFC6455
    {
        Continue = 0x0,
        Text = 0x1,
        Binary = 0x2,
        Close = 0x8,
        Ping = 0x9,
        Pong = 0xA
    }

    public enum DataState
    {
        Empty = 0,
        Waiting = 1,
        Receiving = 2, 
        Complete  = 3,
        Closed = 4,
        Ping = 5,
        Pong = 6
    }
    

}