Beispiel #1
0
        protected void Clean()
        {
            if (trp.KeepAlive)
            {
                targetSocket = null;
                targetConnectionPool.ReuseSocket(crp, targetSocketInfo);
            }
            else
            {
                targetSocket = null;
                targetConnectionPool.RemoveSocket(crp, targetSocketInfo);
            }

            targetSocket = null;
            //clear both buffers
            crp = null;
            trp = null;
            try
            {
                clientSocket.BeginReceive(clientBuffer, 0, BiConnection.BUFFER_SIZE,
                                          SocketFlags.None, new AsyncCallback(OnClientReceived), null);
            }
            catch (SocketException)
            {
                RetryOrDispose(true, false, false);
            }
        }
Beispiel #2
0
 public virtual void ReuseSocket(ClientRequestPacket crp, SocketInfo socketInfo)
 {
     lock (socketPool)
     {
         AddSocket(crp, socketInfo);
         socketInfo.InUse = false;
     }
 }
Beispiel #3
0
        protected void OnClientReceived(IAsyncResult ir)
        {
            int len = 0;

            try
            {
                SocketError se;
                len = clientSocket.EndReceive(ir, out se);
            }
            catch (SocketException)
            {
                //client failed.
                RetryOrDispose(true, false, false);
                return;
            }
            if (len <= 0)
            {
                RetryOrDispose(true, false, false);
                return;
            }
            if (crp == null)
            {
                crp = new ClientRequestPacket();
            }

            crp.AppendData(clientBuffer, 0, len);
            if (crp.HeaderReceived && !crp.HeaderShooted)
            {
                //shoot header;
                this.listener.InvokeNewRequestEvent(new NewRequestEventHandlerArgs
                {
                    Url = crp.Headers[0]
                });
                ConnectTarget();
            }
            else if (crp.HeaderShooted && !crp.ContentShooted)
            {
                if (crp.ContentBuffer != null && crp.ContentBuffer.Length > 0)
                {
                    try
                    {
                        targetSocket.BeginSend(crp.ContentBuffer, 0, crp.ContentBuffer.Length, SocketFlags.None,
                                               new AsyncCallback(OnTargetContentSent), targetSocket);
                    }
                    catch (SocketException)
                    {
                        RetryOrDispose(false, true, false);
                        return;
                    }
                }
            }
        }
        public bool NeedValidate(ClientRequestPacket crp)
        {
            string fullURL;

            if (crp.Port == 80)
            {
                fullURL = crp.Protocol + "://" + crp.Host + crp.RelativePath;
            }
            else
            {
                fullURL = crp.Protocol + "://" + crp.Host + ":" + crp.Port + crp.RelativePath;
            }
            return(pacSetting.IsNeedValidate(fullURL));
        }
        public bool ValidateTargetResponse(ClientRequestPacket crp, TargetResponsePacket trp)
        {
            string fullURL;

            if (crp.Port == 80)
            {
                fullURL = crp.Protocol + "://" + crp.Host + crp.RelativePath;
            }
            else
            {
                fullURL = crp.Protocol + "://" + crp.Host + ":" + crp.Port + crp.RelativePath;
            }
            string html = ReformHTML(trp);

            return(pacSetting.ValidateHtml(fullURL, html));
        }
Beispiel #6
0
        public virtual void RemoveSocket(ClientRequestPacket crp, SocketInfo socketInfo)
        {
            string            requestHost = crp.Host + ":" + crp.Host;
            List <SocketInfo> sockets;

            lock (socketLock)
            {
                if (socketPool.TryGetValue(requestHost, out sockets))
                {
                    sockets.Remove(socketInfo);
                    if (sockets.Count == 0)
                    {
                        socketPool.Remove(requestHost);
                    }
                }
            }
        }
Beispiel #7
0
        protected virtual void AddSocket(ClientRequestPacket crp, SocketInfo socketInfo)
        {
            string            requestHost = crp.Host + ":" + crp.Port;
            List <SocketInfo> sockets;

            Debug.Assert(socketInfo.Socket != null);
            lock (socketLock)
            {
                if (!socketPool.TryGetValue(requestHost, out sockets))
                {
                    sockets = new List <SocketInfo>(1);
                    socketPool[requestHost] = sockets;
                }
                if (!sockets.Contains(socketInfo))
                {
                    sockets.Add(socketInfo);
                }
            }
        }
Beispiel #8
0
        public virtual bool RequestSocketReuse(ClientRequestPacket crp, out SocketInfo socketInfo)
        {
            string            requestHost = crp.Host + ":" + crp.Port;
            List <SocketInfo> sockets;

            socketInfo = null;
            lock (socketLock)
            {
                if (!socketPool.TryGetValue(requestHost, out sockets))
                {
                    sockets = new List <SocketInfo>(1);
                    socketPool[requestHost] = sockets;
                    List <SocketInfo> toRemove = new List <SocketInfo>();

                    foreach (SocketInfo si in sockets)
                    {
                        if (!si.InUse)
                        {
                            if (TouchSocket(si))
                            {
                                socketInfo       = si;
                                socketInfo.InUse = true;
                                break;
                            }
                            else
                            {
                                //remove
                                toRemove.Add(si);
                            }
                        }
                    }
                    foreach (SocketInfo si in toRemove)
                    {
                        //si.Socket.Shutdown(SocketShutdown.Both);
                        Console.WriteLine("Actively Removed {0}", ((IPEndPoint)si.Socket.RemoteEndPoint).Port);
                        si.Socket.Close();
                        sockets.Remove(si);
                    }
                }
            }
            //if no connection available,return null;
            return(socketInfo != null);
        }