diff options
Diffstat (limited to 'OpenSim/Region/Environment/Modules/VoiceChat/VoiceClient.cs')
-rw-r--r-- | OpenSim/Region/Environment/Modules/VoiceChat/VoiceClient.cs | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/VoiceChat/VoiceClient.cs b/OpenSim/Region/Environment/Modules/VoiceChat/VoiceClient.cs new file mode 100644 index 0000000..7eb2177 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/VoiceChat/VoiceClient.cs | |||
@@ -0,0 +1,169 @@ | |||
1 | using System; | ||
2 | using System.IO; | ||
3 | using System.Collections.Generic; | ||
4 | using System.Text; | ||
5 | using System.Net.Sockets; | ||
6 | using OpenSim.Region.Environment.Scenes; | ||
7 | using libsecondlife; | ||
8 | |||
9 | namespace OpenSim.Region.Environment.Modules.VoiceChat | ||
10 | { | ||
11 | /** | ||
12 | * Represents a single voiceclient instance | ||
13 | **/ | ||
14 | public class VoiceClient | ||
15 | { | ||
16 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
17 | |||
18 | public Socket m_socket; | ||
19 | public LLUUID m_clientId; | ||
20 | public bool m_authenticated = false; | ||
21 | |||
22 | protected VoicePacketHeader m_header = null; | ||
23 | protected int m_headerBytesReceived = 0; | ||
24 | |||
25 | protected int m_offset = 0; | ||
26 | protected int m_supportedCodecs = 0; | ||
27 | |||
28 | protected byte[] m_buffer = null; | ||
29 | protected byte[] m_headerBytes = new byte[5]; | ||
30 | |||
31 | protected bool m_enabled = true; | ||
32 | |||
33 | protected VoiceChatServer m_server; | ||
34 | |||
35 | public VoiceClient(Socket socket, VoiceChatServer server) | ||
36 | { | ||
37 | m_socket = socket; | ||
38 | m_server = server; | ||
39 | } | ||
40 | |||
41 | public void OnDataReceived(byte[] data, int byteCount) | ||
42 | { | ||
43 | int offset = 0; | ||
44 | while (offset < byteCount) | ||
45 | { | ||
46 | if (m_header == null) | ||
47 | { | ||
48 | if (m_headerBytesReceived < 5) | ||
49 | { | ||
50 | m_headerBytes[m_headerBytesReceived++] = data[offset++]; | ||
51 | } | ||
52 | else if (m_headerBytesReceived == 5) | ||
53 | { | ||
54 | m_header = new VoicePacketHeader(); | ||
55 | m_header.Parse(m_headerBytes); | ||
56 | if (m_header.length > 65535) | ||
57 | { | ||
58 | throw new Exception("Packet size " + m_header.length + " > 65535"); | ||
59 | } | ||
60 | |||
61 | m_buffer = new byte[m_header.length]; | ||
62 | m_offset = 0; | ||
63 | m_headerBytesReceived = 0; | ||
64 | } | ||
65 | } | ||
66 | else | ||
67 | { | ||
68 | int bytesToCopy = m_header.length-m_offset; | ||
69 | if (bytesToCopy > byteCount - offset) | ||
70 | bytesToCopy = byteCount - offset; | ||
71 | |||
72 | Buffer.BlockCopy(data, offset, m_buffer, m_offset, bytesToCopy); | ||
73 | |||
74 | offset += bytesToCopy; | ||
75 | m_offset += bytesToCopy; | ||
76 | |||
77 | if (m_offset == m_header.length) | ||
78 | { | ||
79 | ParsePacket(m_header.type, m_buffer); | ||
80 | m_header = null; | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | |||
86 | void ParsePacket(byte type, byte[] data) | ||
87 | { | ||
88 | switch (type) | ||
89 | { | ||
90 | case 0: //LOGIN | ||
91 | ParseLogin(data); | ||
92 | break; | ||
93 | |||
94 | case 1: //AUDIODATA | ||
95 | if (m_authenticated) | ||
96 | { | ||
97 | VoicePacket packet = new VoicePacket(data); | ||
98 | packet.m_clientId = m_clientId; | ||
99 | m_server.BroadcastVoice(packet); | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | m_log.Warn("[VOICECHAT]: Got unauthorized audio data from " + | ||
104 | m_socket.RemoteEndPoint.ToString()); | ||
105 | m_socket.Close(); | ||
106 | } | ||
107 | break; | ||
108 | |||
109 | case 3: //ENABLEVOIP | ||
110 | if (data[0] == 0) | ||
111 | { | ||
112 | m_log.Warn("[VOICECHAT]: VoiceChat has been disabled for " + m_clientId); | ||
113 | m_enabled = false; | ||
114 | } | ||
115 | else | ||
116 | { | ||
117 | m_log.Warn("[VOICECHAT]: VoiceChat has been enabled for " + m_clientId); | ||
118 | m_enabled = true; | ||
119 | } | ||
120 | break; | ||
121 | |||
122 | |||
123 | default: | ||
124 | throw new Exception("Invalid packet received"); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | void ParseLogin(byte[] data) | ||
129 | { | ||
130 | m_clientId = new LLUUID(data, 0); | ||
131 | |||
132 | m_supportedCodecs = data[16]; | ||
133 | m_supportedCodecs |= data[17] << 8; | ||
134 | m_supportedCodecs |= data[18] << 16; | ||
135 | m_supportedCodecs |= data[19] << 24; | ||
136 | |||
137 | if (m_server.AddClient(this, m_clientId)) | ||
138 | { | ||
139 | m_log.Info("[VOICECHAT]: Client authenticated succesfully: " + m_clientId); | ||
140 | m_authenticated = true; | ||
141 | } | ||
142 | else | ||
143 | { | ||
144 | throw new Exception("Unable to authenticate with id " + m_clientId); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | public bool IsEnabled() | ||
149 | { | ||
150 | return m_enabled; | ||
151 | } | ||
152 | |||
153 | public bool IsCodecSupported(int codec) | ||
154 | { | ||
155 | if ((m_supportedCodecs & codec) != 0) | ||
156 | return true; | ||
157 | |||
158 | return false; | ||
159 | } | ||
160 | |||
161 | public void SendTo(byte[] data) | ||
162 | { | ||
163 | if (m_authenticated) | ||
164 | { | ||
165 | //ServerStatus.ReportOutPacketTcp(m_socket.Send(data)); | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | } | ||