aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/VoiceChat/VoiceClient.cs
blob: 7be0dadc0a941b64cf8477a73edce9bd6193d971 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using OpenSim.Region.Environment.Scenes;
using libsecondlife;

namespace OpenSim.Region.Environment.Modules.VoiceChat
{
    /**
     * Represents a single voiceclient instance
     **/
    public class VoiceClient
    {
        private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public Socket m_socket;
        public LLUUID m_clientId;
        public bool m_authenticated = false;

        protected VoicePacketHeader m_header = null;
        protected int m_headerBytesReceived = 0;

        protected int m_offset = 0;
        protected int m_supportedCodecs = 0;

        protected byte[] m_buffer = null;
        protected byte[] m_headerBytes = new byte[5];

        protected bool m_enabled = true;

        protected VoiceChatServer m_server;

        public VoiceClient(Socket socket, VoiceChatServer server)
        {
            m_socket = socket;
            m_server = server;
        }

        public void OnDataReceived(byte[] data, int byteCount)
        {
            int offset = 0;
            while (offset < byteCount)
            {
                if (m_header == null)
                {
                    if (m_headerBytesReceived < 5)
                    {
                        m_headerBytes[m_headerBytesReceived++] = data[offset++];
                    }
                    else if (m_headerBytesReceived == 5)
                    {
                        m_header = new VoicePacketHeader();
                        m_header.Parse(m_headerBytes);
                        if (m_header.length > 65535)
                        {
                            throw new Exception("Packet size " + m_header.length + " > 65535");
                        }

                        m_buffer = new byte[m_header.length];
                        m_offset = 0;
                        m_headerBytesReceived = 0;
                    }
                }
                else
                {
                    int bytesToCopy = m_header.length-m_offset;
                    if (bytesToCopy > byteCount - offset)
                        bytesToCopy = byteCount - offset;

                    Buffer.BlockCopy(data, offset, m_buffer, m_offset, bytesToCopy);

                    offset += bytesToCopy;
                    m_offset += bytesToCopy;

                    if (m_offset == m_header.length)
                    {
                        ParsePacket(m_header.type, m_buffer);
                        m_header = null;
                    }
                }
            }
        }

        void ParsePacket(byte type, byte[] data)
        {
            switch (type)
            {
                case 0: //LOGIN
                    ParseLogin(data);
                    break;

                case 1: //AUDIODATA
                    if (m_authenticated)
                    {
                        VoicePacket packet = new VoicePacket(data);
                        packet.m_clientId = m_clientId;
                        m_server.BroadcastVoice(packet);
                    }
                    else
                    {
                        m_log.Warn("[VOICECHAT]: Got unauthorized audio data from " +
                                                           m_socket.RemoteEndPoint.ToString());
                        m_socket.Close();
                    }
                    break;

                case 3: //ENABLEVOIP
                    if (data[0] == 0)
                    {
                        m_log.Warn("[VOICECHAT]: VoiceChat has been disabled for " + m_clientId);
                        m_enabled = false;
                    }
                    else 
                    {
                        m_log.Warn("[VOICECHAT]: VoiceChat has been enabled for " + m_clientId);
                        m_enabled = true;
                    }
                    break;
                    

                default:
                    throw new Exception("Invalid packet received");
            }
        }

        void ParseLogin(byte[] data)
        {
            m_clientId = new LLUUID(data, 0);

            m_supportedCodecs = data[16];
            m_supportedCodecs |= data[17] << 8;
            m_supportedCodecs |= data[18] << 16;
            m_supportedCodecs |= data[19] << 24;

            if (m_server.AddClient(this, m_clientId))
            {
                m_log.Info("[VOICECHAT]: Client authenticated succesfully: " + m_clientId);
                m_authenticated = true;
            }
            else
            {
                throw new Exception("Unable to authenticate with id " + m_clientId);
            }
        }

        public bool IsEnabled()
        {
            return m_enabled;
        }

        public bool IsCodecSupported(int codec)
        {
            if ((m_supportedCodecs & codec) != 0)
                return true;

            return false;
        }

        public void SendTo(byte[] data)
        {
            if (m_authenticated)
            {
                //ServerStatus.ReportOutPacketTcp(m_socket.Send(data));
            }
        }
    }
}