Exemplo n.º 1
0
        public void Start()
        {
            Task.Factory.StartNew(() =>
            {
                Configure();
                try
                {
                    UDTReceiver.connectionExpiryDisabled = true;

                    UDTServerSocket server = new UDTServerSocket(serverPort, localIP);
                    while (true)
                    {
                        UDTSocket socket = server.Accept();
                        Thread.Sleep(1000);

                        Task.Factory.StartNew(() =>
                        {
                            new RequestRunner(socket).Run();
                        });
                    }
                }
                catch (Exception ex)
                {
                    // throw new RuntimeException(ex);
                }
            });
        }
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
        static void Main(string[] args)
        {
            UDTServerSocket socket   = new UDTServerSocket(7777, "127.0.0.1");
            UDTSocket       dTSocket = socket.Accept();

            byte[] buffer = new byte[1470];
            while (!dTSocket.IsClose())
            {
                int r = dTSocket.GetInputStream().Read(buffer);
                Console.WriteLine(Encoding.Default.GetString(buffer, 0, r));
            }
        }
Exemplo n.º 4
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.º 5
0
        /**
         * 返回连接的socket
         */
        public judpSocket Accept()
        {
            UDTSocket socket = SocketControls.GetInstance().GetSocket();

            if (socket == null)
            {
                //再次获取下一个
                socket = SocketControls.GetInstance().GetSocket();
            }
            //包装
            judpSocket jsocket = new judpSocket(socket);

            judpSocketManager.GetInstance(socket.GetEndpoint()).AddSocket(jsocket);
            return(jsocket);
        }
Exemplo n.º 6
0
        public void RunSend()
        {
            try
            {
                UDTReceiver.connectionExpiryDisabled = true;
                //IPAddress myHost = null;
                //if (localIP != "")
                //{
                //    myHost = IPAddress.Parse(localIP);
                //}
                //else
                //{
                //    string hostname = Dns.GetHostName();
                //    IPHostEntry hostip = Dns.GetHostEntry(hostname);
                //    foreach (IPAddress ipaddress in hostip.AddressList)
                //    {
                //        if (ipaddress.ToString().IndexOf(':') < 0)//存在IPV6的地址,所以要判断
                //        {
                //            myHost = ipaddress;
                //            break;
                //        }
                //    }
                //}

                UDTServerSocket server = new UDTServerSocket(serverPort);
                ThreadPool.SetMaxThreads(1, 1);

                while (true)
                {
                    //启动监听(第一次),及获取已经存在的连接信息
                    UDTSocket socket = server.Accept();
                    Thread.Sleep(1000);
                    TaskInfo ti = new TaskInfo(socket, verbose);

                    ThreadPool.QueueUserWorkItem(new WaitCallback(run), ti);
                }
            }
            catch (Exception ex)
            {
                Log.Write(this.ToString(), ex);
            }
        }
Exemplo n.º 7
0
        /**
         *
         * @Title: startThread
         * @Description: 启动线程监测数据源,移除无用连接
         * @param     参数
         * @return void    返回类型
         */
        private void StartThread()
        {
            Thread processSocket = new Thread(() => {
                List <long> list = new List <long>();
                while (true)
                {
                    foreach (KeyValuePair <long, judpGroupSocket> entry in hash)
                    {
                        UDTSocket socket = entry.Value.GetSocket();
                        if (socket != null)
                        {
                            hasSocket.Add(socket);
                            list.Add(entry.Key);
                        }
                    }

                    //
                    if (list.Count > 0)
                    {
                        //移除已经检查成功的socket
                        for (int i = 0; i < list.Count; i++)
                        {
                            judpGroupSocket group = null;
                            if (hash.TryRemove(list[i], out group))
                            {
                                group.Clear();
                            }
                        }
                        list.Clear();
                    }
                    else
                    {
                        //每完成一次全部检查;
                        Thread.Sleep(1000);
                    }
                }
            });

            processSocket.IsBackground = true;
            processSocket.Name         = ("processSocket");
            processSocket.Start();
        }
Exemplo n.º 8
0
        /**
         * 启动接收
         */
        public bool Start()
        {
            if (!isStart || !isSucess)
            {
                FlashLogger.Warn("已经关闭的监听或监听端口不能使用");
                return(false);
            }
            Thread serverThread = new Thread(() =>
            {
                while (isStart)
                {
                    try
                    {
                        UDTSocket csocket = server.Accept();
                        try
                        {
                            csocket.GetInputStream().SetLargeRead(islagerRead);
                            csocket.GetInputStream().ResetBufMaster(isRWMaster);
                        }
                        catch (IOException e)
                        {
                            Console.WriteLine(e);
                        }

                        SocketControls.GetInstance().AddSocket(csocket);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            });

            serverThread.IsBackground = true;
            serverThread.Name         = ("judpServer_" + DateTime.Now.Ticks / 10000);
            serverThread.Start();
            return(true);
        }
Exemplo n.º 9
0
 public judpSocket(UDTSocket usocket)
 {
     this.socket = usocket;
     socketID    = socket.GetSession().SocketID;
 }
Exemplo n.º 10
0
 public TaskInfo(UDTSocket _socket, bool _verbose)
 {
     socket  = _socket;
     verbose = _verbose;
 }
Exemplo n.º 11
0
 /**
  * 添加socket
  * @param socket
  */
 public void AddSocket(UDTSocket socket)
 {
     list.Add(socket);
 }
Exemplo n.º 12
0
 public RequestRunner(UDTSocket socket)
 {
     this.socket = socket;
     format.PercentDecimalDigits = 3;
 }