Exemple #1
0
        public void listen_audio_header()
        {
            while (true)
            {
                Package pack = conn.recv();
                if (pack.type == "request_audio")
                {
                    /*
                     * header
                     * [0] count
                     *
                     */
                    audio_resp = audio_reader.loadWaveOutStream(Int32.Parse(pack.header[0]));
                    MemoryStream    stream    = new MemoryStream();
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, audio_resp);
                    byte[] data = stream.ToArray();

                    Package pack_resp = new Package("audio_resp");
                    pack_resp.data = data;
                    Connector conn_resp = Client.find_conn(Client.conn_audio_data, pack.from);
                    if (conn_resp != null)
                    {
                        conn_resp.send(pack_resp);
                    }
                }
                else if (pack.type == "ask_audio")
                {
                    /*
                     * header
                     * [0] count
                     *
                     * data
                     * format
                     *
                     */
                    /*audio_reader.loadFile(Local.ref_addr + @"audio\" + pack.header[0]);
                     * MemoryStream stream = new MemoryStream();
                     * BinaryFormatter formatter = new BinaryFormatter();
                     * formatter.Serialize(stream, audio_reader.format);
                     * byte[] data = stream.ToArray();
                     */
                    byte[]   data = new byte[0];
                    String[] tmp  = pack.header[0].Split('\\');
                    try
                    {
                        data = File.ReadAllBytes(Local.ref_addr + "audio\\" + tmp[tmp.Length - 1]);
                    }
                    catch
                    {
                    }

                    Package pack_resp = new Package("audio_format");
                    pack_resp.data = data;
                    pack_resp.header.Add(tmp[tmp.Length - 1]);
                    Connector conn_resp = Client.find_conn(Client.conn_audio_data, pack.from);
                    conn_resp.send(pack_resp);
                }
            }
        }
Exemple #2
0
        //只有server在监听这个=。=
        public void listen_danmu_server()
        {
            if (Client.isServer == 0)
            {
                return;
            }
            while (true)
            {
                Package pack = conn.recv();
                if (pack == null)
                {
                    continue;
                }
                if (pack.type == "danmu_ask")
                {
                    /*
                     * header:
                     * [0]filename
                     *
                     * return:
                     * data -> serialized danmulist
                     */
                    DanmuList danmuList = new DanmuList();

                    String[] tmp = pack.header[0].Split('\\');

                    danmuList.readFromFile(tmp[tmp.Length - 1]);

                    MemoryStream    stream    = new MemoryStream();
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, danmuList);

                    byte[] data = stream.ToArray();

                    Package pack_resp = new Package("danmu_resp");
                    pack_resp.data = data;
                    Connector conn_resp = Client.find_conn(Client.conn_danmu_client, pack.from);

                    conn_resp.send(pack_resp);
                }
                else if (pack.type == "danmu_add")
                {
                    /*
                     * header:
                     * [0] filename
                     * [1] num
                     * [2] content
                     *
                     * no need to resp
                     */
                    DanmuList.appendDanmu(Int32.Parse(pack.header[1]), pack.header[2], pack.header[0]);
                }
            }
        }
Exemple #3
0
        public void listen_pmm_resp()
        {
            while (true)
            {
                Package pack = conn.recv();
                if (pack == null)
                {
                    continue;
                }
                if (pack.type == "ask_pmm")
                {
                    if (Local.exist(pack.header[0]))
                    {
                        pmm = PMM.loadFile(pack.header[0]);
                        Package   pack_resp = new Package("has_pmm");
                        Connector conn_resp = Client.find_conn(Client.conn_pmm_ask, pack.from);
                        pack_resp.header.Add("" + pmm.width);
                        pack_resp.header.Add("" + pmm.height);
                        pack_resp.header.Add(pmm.method);

                        pmm_data = new List <byte[]>();
                        int position = 0;
                        int len      = pmm.data.Length / 10;

                        for (int i = 0; i < 9; i++)
                        {
                            pmm_data.Add(new byte[len]);
                            Buffer.BlockCopy(pmm.data, position, pmm_data[i], 0, len);
                            position += len;
                        }
                        pmm_data.Add(new byte[pmm.data.Length - position]);
                        Buffer.BlockCopy(pmm.data, position, pmm_data[9], 0, pmm.data.Length - position);

                        conn_resp.send(pack_resp);
                    }
                    else
                    {
                        Package   pack_resp = new Package("no_pmm");
                        Connector conn_resp = Client.find_conn(Client.conn_pmm_ask, pack.from);
                        conn_resp.send(pack_resp);
                    }
                }
                else if (pack.type == "request_pmm")
                {
                    Package   pack_resp = new Package("resp_pmm");
                    Connector conn_resp = Client.find_conn(Client.conn_pmm_ask, pack.from);
                    pack_resp.header.Add(pack.header[0]);
                    pack_resp.data = pmm_data[Int32.Parse(pack.header[0])];
                    conn_resp.send(pack_resp);
                }
            }
        }
Exemple #4
0
        public void listen_exit()
        {
            while (true)
            {
                Package pack = conn.recv();
                if (pack == null)
                {
                    continue;
                }
                if (pack.type == "client_exit")
                {
                    //更新本地clients列表和ip_list
                    //发送exit_count消息给xxxx

                    Connector conn_resp = Client.find_conn(Client.conn_user_exit, pack.from);

                    Package pack_resp = new Package("exit_count");
                    conn_resp.send(pack_resp);

                    Client.update_on_client_leave(pack.from);
                }
                else if (pack.type == "server_exit")
                {
                    //发送exit_count消息给xxxx
                    //其实想直接退出的
                    //就想想

                    Connector conn_resp = Client.find_conn(Client.conn_user_exit, pack.from);

                    Package pack_resp = new Package("exit_count");
                    conn_resp.send(pack_resp);

                    Client.update_on_client_leave(pack.from);
                }
                else if (pack.type == "exit_count")
                {
                    //要退出的程序收到这个以后减小记数
                    Client.onLeave_userCount--;
                }
            }
        }
Exemple #5
0
        public void listen_search_resp()
        {
            while (true)
            {
                Package pack = conn.recv();
                if (pack == null)
                {
                    continue;
                }
                if (pack.type == "search")
                {
                    List <video_info> result    = Utils.search_list(pack.header[0], Client.media);
                    MemoryStream      stream    = new MemoryStream();
                    BinaryFormatter   formatter = new BinaryFormatter();
                    formatter.Serialize(stream, result);
                    byte[] data = stream.ToArray();

                    Package   pack_resp = new Package("search_result");
                    Connector conn_resp = Client.find_conn(Client.conn_search, pack.from);
                    pack_resp.data = data;
                    conn_resp.send(pack_resp);
                }
            }
        }
Exemple #6
0
        public void listen_video_header()
        {
            while (true)
            {
                Console.Out.WriteLine("listen_video_header is running...");
                Package pack = conn.recv();
                if (pack == null)
                {
                    continue;
                }
                if (pack.type == "ask_video")
                {
                    /*
                     * header for request:
                     * [0] Name
                     */
                    List <String> header   = new List <String>(pack.header);
                    String[]      tmp      = header[0].Split('\\');
                    String        tmp_addr = Utils.search_addr_from_list(tmp[tmp.Length - 1], Client.media);
                    if (tmp_addr != "" && File.Exists(tmp_addr))
                    {
                        //该文件存在,载入到本线程的reader里面
                        //接受的时候不需要reader,只有传的时候需要,所以一个reader就够了

                        /*
                         * type of resp: has_video
                         *
                         * reader the info from the video file and response a header
                         *
                         * header of resp:
                         * [0] FrameRate(给player设置interval)
                         * [1] Height
                         * [2] Width
                         *
                         * 那个wmv是历史遗留问题反正avi也是这个参数。。。
                         *
                         */

                        header = reader.loadFile(tmp_addr, "wmv");


                        for (int i = 0; i < header.Count; i++)
                        {
                            Console.Out.WriteLine("{0}: {1}", i, header[i]);
                        }
                        Package pack_resp = new Package("has_video");
                        pack_resp.header = new List <string>(header);
                        Connector conn_resp = Client.find_conn(Client.conn_video_data, pack.from);
                        conn_resp.send(pack_resp);
                    }
                    else
                    {
                        Package   pack_resp = new Package("no_video");
                        Connector conn_resp = Client.find_conn(Client.conn_video_data, pack.from);
                        conn_resp.send(pack_resp);
                    }
                }
                else if (pack.type == "request_video" && num_frac == 0)
                {
                    /*
                     * header for request:
                     * [0] 第几个bitmapStream
                     */

                    List <String> header = new List <String>(pack.header);

                    if (reader.finish == 1)
                    {
                        //空了,给一个end_video
                        Package          pack_resp = new Package("end_video");
                        List <Connector> conn_resp = Client.find_conns(Client.conn_video_data, pack.from);

                        conn_resp[0].send(pack_resp);
                    }
                    else
                    {
                        BitmapStream bitmapStream = reader.loadBitmapStream_count(Int32.Parse(header[0]));

                        CompressedBitmapStream comp_bitmapStream = new CompressedBitmapStream(bitmapStream);

                        //把object serialize成memoryStream,再到byte[]
                        MemoryStream    stream    = new MemoryStream();
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, comp_bitmapStream);

                        byte[] data = stream.ToArray();

                        //byte[] m = Compress.compress(data);

                        List <byte[]> data_list = new List <byte[]>();

                        int offset = (int)data.Length / Client.NUM_FRACTION;

                        for (int i = 0; i < Client.NUM_FRACTION; i++)
                        {
                            byte[] tmp_data;
                            if (i < Client.NUM_FRACTION - 1)
                            {
                                tmp_data = new byte[offset];
                            }
                            else
                            {
                                tmp_data = new byte[data.Length - (Client.NUM_FRACTION - 1) * offset];
                            }
                            System.Buffer.BlockCopy(data, i * offset, tmp_data, 0, tmp_data.Length);
                            data_list.Add(tmp_data);
                        }

                        //发回去

                        List <Connector> conn_resp = Client.find_conns(Client.conn_video_data, pack.from);

                        if (conn_resp.Count == 0)
                        {
                            return;
                        }

                        for (int i = 0; i < Client.NUM_FRACTION; i++)
                        {
                            Package pack_resp = new Package("video_resp");
                            pack_resp.data = data_list[i];
                            if (conn_resp[i].ip_send == "")
                            {
                                return;
                            }
                            send_thread a = new send_thread(conn_resp[i], pack_resp);
                            a.start();
                        }
                    }
                }
            }
        }