Ejemplo n.º 1
0
        public bool Service(uint time)
        {
            while (this.data_out.Count > 0)
            {
                try
                {
                    this.sock.Send(this.data_out[0]);
                    this.data_out.RemoveAt(0);
                }
                catch { break; }
            }

            byte[]      buf     = new byte[8192];
            SocketError e       = SocketError.Success;
            int         size    = 0;
            bool        success = true;

            try
            {
                this.avail = this.sock.Available;

                if (this.avail > 8192)
                {
                    this.avail = 8192;
                }

                size = this.sock.Receive(buf, 0, this.avail, SocketFlags.None, out e);
            }
            catch { }

            if (size == 0)
            {
                if (e == SocketError.WouldBlock)
                {
                    this.health = 0;
                }
                else if (this.health++ > 3)
                {
                    this.SockCode = (int)e;
                    success       = false;
                }
            }
            else
            {
                this.health = 0;
                this.data_in.AddRange(buf.Take(size));
            }

            while (this.data_in.Count >= 3)
            {
                ushort len = BitConverter.ToUInt16(this.data_in.ToArray(), 0);

                if (this.data_in.Count >= (len + 3))
                {
                    byte   id     = this.data_in[2];
                    byte[] packet = this.data_in.GetRange(3, len).ToArray();
                    this.data_in.RemoveRange(0, (len + 3));

                    PacketReceivedEventArgs args = new PacketReceivedEventArgs
                    {
                        Msg    = (TCPMsg)id,
                        Packet = new TCPPacketReader(packet),
                        Time   = time
                    };

                    if (args.Msg == TCPMsg.MSG_CHAT_CLIENTCOMPRESSED)
                    {
                        packet = Zip.Decompress(packet);

                        if (packet != null)
                        {
                            if (packet.Length > 0)
                            {
                                this.data_in.InsertRange(0, packet);
                            }
                        }
                    }
                    else
                    {
                        try { this.PacketReceived(null, args); }
                        catch { }
                    }
                }
                else
                {
                    break;
                }
            }

            return(success);
        }
Ejemplo n.º 2
0
        public static byte[][] GetPackets(VoiceRecorderSendMethod m, byte len)
        {
            List <byte[]> packets = new List <byte[]>();

            byte[]      org_data          = RecordBytes();
            uint        ident             = Settings.Time;
            byte        clip_len          = (byte)(len + 2);
            uint        uncompressed_size = (uint)org_data.Length;
            uint        compressed_size   = uncompressed_size;
            List <uint> compress_results  = new List <uint>();

            if (m == VoiceRecorderSendMethod.Opus)
            {
                org_data          = Opus.Encode(org_data);
                uncompressed_size = (uint)org_data.Length;
                List <byte> data_to_send = new List <byte>(org_data);

                if (data_to_send.Count < MAX_CHUNK_SIZE)
                {
                    packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, 0, uncompressed_size, data_to_send.ToArray()));
                }
                else
                {
                    packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, 0, uncompressed_size, data_to_send.ToArray()));
                    data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);

                    while (data_to_send.Count >= MAX_CHUNK_SIZE)
                    {
                        packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.GetRange(0, MAX_CHUNK_SIZE).ToArray()));
                        data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);
                    }

                    if (data_to_send.Count > 0)
                    {
                        packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.ToArray()));
                    }
                }
            }
            else
            {
                while (true)
                {
                    byte[] tmp = Zip.Compress(org_data);

                    if (tmp.Length < org_data.Length)
                    {
                        compress_results.Add((uint)tmp.Length);
                        compressed_size = (uint)tmp.Length;
                        org_data        = tmp;
                    }
                    else
                    {
                        break;
                    }
                }

                List <byte> data_to_send          = new List <byte>(org_data);
                List <byte> compress_results_data = new List <byte>();

                for (int i = 0; i < compress_results.Count; i++)
                {
                    compress_results_data.AddRange(BitConverter.GetBytes(compress_results[i]));
                }

                if (data_to_send.Count < MAX_CHUNK_SIZE)
                {
                    packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, (byte)compress_results.Count,
                                                       uncompressed_size, compress_results_data.ToArray().Concat(data_to_send).ToArray()));
                }
                else
                {
                    packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, (byte)compress_results.Count,
                                                       uncompressed_size, compress_results_data.ToArray().Concat(data_to_send.GetRange(0, MAX_CHUNK_SIZE)).ToArray()));

                    data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);

                    while (data_to_send.Count >= MAX_CHUNK_SIZE)
                    {
                        packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.GetRange(0, MAX_CHUNK_SIZE).ToArray()));
                        data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);
                    }

                    if (data_to_send.Count > 0)
                    {
                        packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.ToArray()));
                    }
                }
            }

            return(packets.ToArray());
        }