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