Exemplo n.º 1
0
 /*
  * 获取初始化序列
  */
 public long GetInitSeqNo()
 {
     if (socket != null)
     {
         return(socket.GetSession().InitialSequenceNumber);
     }
     return(0);
 }
Exemplo n.º 2
0
        /**
         * 保存UDTSocket
         * @param socket
         */
        public void AddSocket(UDTSocket socket)
        {
            long            id    = socket.GetSession().Destination.SocketID;//同一个目的
            judpGroupSocket group = null;

            if (!hash.TryGetValue(id, out group))
            {
                group    = new judpGroupSocket();
                hash[id] = group;
                group.AddSocket(socket);
            }
        }
Exemplo n.º 3
0
        public void Run()
        {
            try
            {
                //create an UDTServerSocket on a free port
                UDTServerSocket server = new UDTServerSocket(0);
                // do hole punching to allow the client to connect
                IPAddress  clientAddress = IPAddress.Parse(clientIP);
                IPEndPoint point         = new IPEndPoint(clientAddress, clientPort);
                //发送一字节确认端口
                Util.DoHolePunch(server.getEndpoint(), point);
                int localPort = server.getEndpoint().LocalPort; //获取真实端口
                                                                //output client port
                writeToOut("OUT: " + localPort);

                //now start the send...
                UDTSocket       socket    = server.Accept();
                UDTOutputStream outStream = socket.GetOutputStream();
                FileInfo        file      = new FileInfo(localFilename);
                if (!file.Exists)
                {
                    Console.WriteLine("没有文件:" + localFilename);
                    socket.Close();
                    server.ShutDown();
                    return;
                }
                FileStream fis = new FileStream(localFilename, FileMode.Open);
                try
                {
                    //send file size info
                    long size = fis.Length;
                    PacketUtil.Encode(size);
                    outStream.Write(PacketUtil.Encode(size));
                    long start = DateTime.Now.Ticks;
                    //and send the file
                    Util.CopyFileSender(fis, outStream, size, false);
                    long end = DateTime.Now.Ticks;
                    Console.WriteLine(socket.GetSession().Statistics);
                    float mbRate   = 1000 * size / 1024 / 1024 / (end - start);
                    float mbitRate = 8 * mbRate;
                    Console.WriteLine("Rate: " + (int)mbRate + " MBytes/sec. " + mbitRate + " mbit/sec.");
                }
                finally
                {
                    fis.Close();
                    socket.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 4
0
 public judpSocket(UDTSocket usocket)
 {
     this.socket = usocket;
     socketID    = socket.GetSession().SocketID;
 }
Exemplo n.º 5
0
            public void Run()
            {
                try
                {
                    FlashLogger.Info("Handling request from " + socket.GetSession().Destination);
                    UDTInputStream  intStream = socket.GetInputStream();
                    UDTOutputStream outStream = socket.GetOutputStream();
                    byte[]          readBuf   = new byte[32768];

                    //read file name info
                    while (intStream.Read(readBuf) == 0)
                    {
                        Thread.Sleep(100);
                    }

                    //how many bytes to read for the file name
                    byte[] len = new byte[4];
                    //bb.get(len);

                    Array.Copy(readBuf, 0, len, 0, 4);//文件名称长度
                    if (verbose)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < len.Length; i++)
                        {
                            sb.Append((len[i]).ToString());
                            sb.Append(" ");
                        }
                        Console.WriteLine("[SendFile] name length data: " + sb.ToString());
                    }
                    long length = Decode(len, 0);
                    if (verbose)
                    {
                        Console.WriteLine("[SendFile] name length     : " + length);
                    }
                    byte[] fileName = new byte[(int)length];
                    //读取文件名称
                    Array.Copy(readBuf, 4, fileName, 0, fileName.Length);
                    FileInfo file = new FileInfo(Encoding.UTF8.GetString(fileName));//兼容java
                    Console.WriteLine("[SendFile] File requested: '" + file.FullName + "'");

                    FileStream fis = null;
                    try
                    {
                        long size = file.Length;
                        Console.WriteLine("[SendFile] File size: " + size);
                        //send size info
                        outStream.Write(Encode64(size)); //先写入大小
                        outStream.Flush();               //传输完成

                        long start = DateTime.Now.Ticks;
                        fis = new FileStream(file.FullName, FileMode.Open);

                        CopyFile(fis, outStream, size, false);
                        Console.WriteLine("[SendFile] Finished sending data.");
                        long end = DateTime.Now.Ticks;
                        Console.WriteLine(socket.GetSession().Statistics.toString());
                        double rate = 1000.0 * size / 1024 / 1024 / (end - start);
                        Console.WriteLine("[SendFile] Rate: " + rate.ToString(format) + " MBytes/sec. " + (8 * rate).ToString(format) + " MBit/sec.");
                    }
                    finally
                    {
                        socket.GetSender().Stop();
                        if (fis != null)
                        {
                            fis.Close();
                        }
                    }
                    FlashLogger.Info("Finished request from " + socket.GetSession().Destination);
                }
                catch (Exception ex)
                {
                    FlashLogger.Error(ex.Message);
                }
            }