Exemple #1
0
        protected void doReceive()
        {
            while (!stopped)
            {
                try{
                    try{
                        //will block until a packet is received or timeout has expired
                        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                        EndPoint   Remote = (EndPoint)sender;
                        //dgSocket.Bind(_ipep);
                        dgSocket.ReceiveFrom(dp, ref Remote);

                        Remoteinfo = (IPEndPoint)Remote;
                        Destination peer = new Destination(Remoteinfo.Address, Remoteinfo.Port);

                        int       l      = dp.Length;
                        UDTPacket packet = PacketFactory.createPacket(dp, l);
                        lastPacket = packet;

                        //handle connection handshake
                        if (packet.isConnectionHandshake())
                        {
                            lock (thisLock)
                            {
                                long       id = packet.getDestinationID();
                                UDTSession session;
                                sessions.TryGetValue(id, out session);
                                if (session == null)
                                {
                                    session = new ServerSession(dp, this);
                                    addSession(session.getSocketID(), session);
                                    //TODO need to check peer to avoid duplicate server session
                                    if (serverSocketMode)
                                    {
                                        Log.Write(this.ToString(), "Pooling new request.");
                                        sessionHandoff.Enqueue(session);
                                        Log.Write(this.ToString(), "Request taken for processing.");
                                    }
                                }
                                peer.setSocketID(((ConnectionHandshake)packet).getSocketID());
                                session.received(packet, peer);
                            }
                        }
                        else
                        {
                            //dispatch to existing session
                            long       dest = packet.getDestinationID();
                            UDTSession session;
                            if (dest == lastDestID)
                            {
                                session = lastSession;
                            }
                            else
                            {
                                sessions.TryGetValue(dest, out session);
                                lastSession = session;
                                lastDestID  = dest;
                            }
                            if (session == null)
                            {
                                n++;
                                if (n % 100 == 1)
                                {
                                    Log.Write(this.ToString(), "Unknown session <" + dest + "> requested from <" + peer + "> packet type " + packet.ToString());
                                }
                            }
                            else
                            {
                                session.received(packet, peer);
                            }
                        }
                    }
                    catch (SocketException ex)
                    {
                        Log.Write(this.ToString(), "INFO", ex);
                    }
                }catch (Exception ex) {
                    Log.Write(this.ToString(), "WARNING", ex);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 处理接收的数据
        /// </summary>
        /// <param name="dp"></param>
        /// <param name="_remoteIP"></param>
        private void ReceiveData(byte[] dp, IPEndPoint _remoteIP)
        {
            try
            {
                Destination peer = new Destination(_remoteIP.Address, _remoteIP.Port);

                int       l      = dp.Length;
                UDTPacket packet = PacketFactory.createPacket(dp, l);
                lastPacket = packet;

                //handle connection handshake  处理连接握手
                if (packet.isConnectionHandshake())
                {
                    lock (thisLock)
                    {
                        long       id = packet.getDestinationID();
                        UDTSession session;
                        sessions.TryGetValue(id, out session);//ClientSession 或是 ServerSession
                        if (session == null)
                        {
                            session = new ServerSession(dp, this);
                            addSession(session.getSocketID(), session);
                            //TODO need to check peer to avoid duplicate server session
                            if (serverSocketMode)
                            {
                                sessionHandoff.Enqueue(session);
                                Log.Write(this.ToString(), "Pooling new request, request taken for processing.");
                            }
                        }
                        try
                        {
                            peer.setSocketID(((ConnectionHandshake)packet).getSocketID());
                            session.received(packet, peer);//ClientSession 或是 ServerSession
                        }
                        catch (Exception ex)
                        {
                            Log.Write(this.ToString(), "WARNING", ex);
                        }
                    }
                }
                else
                {
                    //dispatch to existing session
                    long       dest = packet.getDestinationID();
                    UDTSession session;
                    if (dest == lastDestID)
                    {
                        session = lastSession;
                    }
                    else
                    {
                        sessions.TryGetValue(dest, out session);
                        lastSession = session;
                        lastDestID  = dest;
                    }
                    if (session == null)
                    {
                        n++;
                        if (n % 100 == 1)
                        {
                            Log.Write(this.ToString(), "Unknown session <" + dest + "> requested from <" + peer + "> packet type " + packet.ToString());
                        }
                    }
                    else
                    {
                        session.received(packet, peer);
                    }
                }
            }
            catch (SocketException ex)
            {
                Log.Write(this.ToString(), "INFO", ex);
            }
        }