Exemple #1
0
        public void StreamingCallBack(string e, StreamIt source)
        {
            Application.DoEvents();
            if ((e.CompareTo("Stopped") == 0))
            {
                if (bStreaming)
                {
                    new XMLCreater(Application.StartupPath + "\\Temp1.xml", "").WriteErrorResponse("A streaming session is already active on this peer");
                }
                else
                {
                    new XMLCreater(Application.StartupPath + "\\Temp1.xml", "").WriteErrorResponse("Problem encountered while initiating the Streaming Session");
                    bStreaming = false;
                }
            }
            else if ((e.CompareTo("Encoder Stopped") == 0) || (e.CompareTo("Disconnected") == 0))
            {
                bStreaming = false;
            }
            else if (e.CompareTo("Copy Complete") == 0)
            {
                bStreaming = true;
                new XMLCreater(Application.StartupPath + "\\Temp1.xml", "").WriteErrorResponse("No Error");
            }
            else
            {
                return;
            }
            if (bStreaming && (e.CompareTo("Connected") == 0))
            {
                connectedStreamingClients = source.ConnectedClients;
            }

            NetworkStream DataStream = new NetworkStream(newSock);

            Byte[]       readBytes = new Byte[1024], read = new Byte[1024];
            FileStream   fs1       = new FileStream(Application.StartupPath + "\\Temp1.xml", FileMode.Open);
            BinaryReader r         = new BinaryReader(fs1);
            int          nreadBytes;

            // write upon the network stream
            while ((nreadBytes = r.Read(readBytes, 0, readBytes.Length)) > 0)
            {
                DataStream.Write(readBytes, 0, nreadBytes);
            }

            r.Close();
            fs1.Close();

            File.Delete(Application.StartupPath + "\\Temp1.xml");
            File.Delete(Application.StartupPath + "\\Temp.xml");
            DataStream.Flush();
            newSock.Close();
        }
Exemple #2
0
        // Resposnd the requests made
        protected void RequestResponse()
        {
            Encoding      ASCII      = Encoding.ASCII;
            NetworkStream DataStream = new NetworkStream(newSock);

            try
            {
                Byte[]   readBytes = new Byte[1024], read = new Byte[1024];
                string[] ChatInfo;

                int        bytes = 0, UploadDownloadPrint;
                bool       responseWritten = false;
                FileStream fs1             = null;

                // read what the browser wrote on its stream and
                bytes = DataStream.Read(read, 0, read.Length);
                // make a temporary file
                FileStream fs = new FileStream(Application.StartupPath + "\\Temp.xml", FileMode.OpenOrCreate);

                // to store that data to
                BinaryWriter w = new BinaryWriter(fs);
                w.Write(read, 0, bytes);
                w.Close();
                fs.Close();

                // then read file and detrmine the kind of request
                xmlCreater = new XMLCreater(Application.StartupPath + "\\Temp1.xml", Application.StartupPath + "\\Share.ini");
                string DnLoadFile = xmlCreater.DetermineRequestType(Application.StartupPath + "\\Temp.xml", out UploadDownloadPrint, out ChatInfo);

                // if it's download/upload request make file object for
                // the file browser has requested to upload to
                // or download from his comp
                if (UploadDownloadPrint == 4)                   // this represents a chat request
                {
                    frmChat oChatFrm = new frmChat(ChatInfo, newSock, LoginName, Application.StartupPath + "\\ChatLog.log", ++ChatWindowCount);
                    oChatFrm.ShowDialog();
                    File.Delete(Application.StartupPath + "\\Temp.xml");
                    bDownLoading = false;
                    return;
                }
                if (DnLoadFile.Length != 0)
                {
                    if (UploadDownloadPrint == 1)
                    {
                        fs1 = new FileStream(DnLoadFile, FileMode.CreateNew);
                        DnLoadFile.EndsWith("mp3");
                    }
                    else if (UploadDownloadPrint == 2)
                    {
                        fs1 = new FileStream(DnLoadFile, FileMode.Open);
                    }
                    else if (UploadDownloadPrint == 3)
                    {
                        ShellExecute(0, "print", DnLoadFile, "", "", 1);
                        File.Delete(Application.StartupPath + "\\Temp.xml");
                        DataStream.Flush();
                        newSock.Close();
                        bDownLoading = false;
                        return;
                    }
                    if (UploadDownloadPrint == 5)                       // this represents a chat request
                    {
                        StreamIt streamDlg = new StreamIt(DnLoadFile, new StreamingCallBackDeligates(StreamingCallBack));
                        streamDlg.ShowDialog();
                        intStreamingThreadIndex = ThreadCount;
                        bDownLoading            = false;
                        return;
                    }
                }
                else
                {
                    // else make a file object for responding in xml formate
                    fs1             = new FileStream(Application.StartupPath + "\\Temp1.xml", FileMode.Open);
                    responseWritten = true;
                }

                // write resoponse
                if (UploadDownloadPrint == 1)
                {
                    bDownLoading = true;
                    // if it was upload request
                    BinaryWriter wr = new BinaryWriter(fs1);
                    int          nreadBytes;

                    // read network stream and write data to a loacal file
                    while ((nreadBytes = DataStream.Read(readBytes, 0, readBytes.Length)) > 0)
                    {
                        wr.Write(readBytes, 0, nreadBytes);
                    }

                    wr.Close();
                    fs1.Close();
                }
                else
                {
                    // write the response or file requested for download
                    // in browsers stream
                    BinaryReader r = new BinaryReader(fs1);
                    int          nreadBytes;

                    // write upon the network stream
                    while ((nreadBytes = r.Read(readBytes, 0, readBytes.Length)) > 0)
                    {
                        DataStream.Write(readBytes, 0, nreadBytes);
                    }

                    r.Close();
                    fs1.Close();
                }

                // delete all temporary files
                if (responseWritten)
                {
                    File.Delete(Application.StartupPath + "\\Temp1.xml");
                }

                File.Delete(Application.StartupPath + "\\Temp.xml");
                DataStream.Flush();
                newSock.Close();
                bDownLoading = false;
            }
            catch (SocketException e) { WriteErrorLog("Responding Browser", e.Message); }
        }