コード例 #1
0
        ///<summary>Called when we have sent data to the local client.<br>When all the data has been sent, we will start receiving again from the remote host.</br></summary>
        ///<param name="ar">The result of the asynchronous operation.</param>
        protected void OnClientSent(IAsyncResult ar)
        {
            lock (_bufferLock)
            {
                try
                {
                    if (ClientSocket == null)
                    {
                        return;
                    }

                    int Ret = ClientSocket.EndSend(ar);
                    if (Ret > 0)
                    {
                        if (OnClientSentEnd != null)
                        {
                            //OnClientSentEnd.BeginInvoke(ClientSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer, false, null, null);
                            OnClientSentEnd.Invoke(ClientSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer.Count == 0 ? null : _cachedRemoteBuffer, false);
                        }
                        if (!Cancel)
                        {
                            DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(OnRemoteReceive), DestinationSocket);
                        }
                        else
                        {
                            Dispose();
                        }

                        return;
                    }
                }
                catch
                {
                }
                Dispose();
            }
        }
コード例 #2
0
        ///<summary>Called when we have received data from the remote host.<br>Incoming data will immediately be forwarded to the local client.</br></summary>
        ///<param name="ar">The result of the asynchronous operation.</param>
        protected void OnRemoteReceive(IAsyncResult ar)
        {
            lock (_bufferLock)
            {
                try
                {
                    if (DestinationSocket == null)
                    {
                        return;
                    }

                    int Ret = DestinationSocket.EndReceive(ar);
                    if (Ret <= 0)
                    {
                        if (OnRemoteReceivedEnd != null)
                        {
                            if (_cachedRemoteBuffer != null)
                            {
                                //OnRemoteReceivedEnd.BeginInvoke(DestinationSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer, true, null, null);
                                OnRemoteReceivedEnd.Invoke(DestinationSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer.Count == 0 ? null : _cachedRemoteBuffer, true);
                            }
                            else
                            {
                                OnRemoteReceivedEnd.Invoke(DestinationSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer, true);
                            }
                        }

                        if (OnClientSentEnd != null)
                        {
                            if (_cachedRemoteBufferHeaders != null)
                            {
                                //OnClientSentEnd.BeginInvoke(ClientSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer, true, null, null);
                                OnClientSentEnd.Invoke(ClientSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer.Count == 0 ? null : _cachedRemoteBuffer, true);
                            }
                            else
                            {
                                OnClientSentEnd.Invoke(ClientSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer, true);
                            }
                        }

                        Dispose();
                        return;
                    }
                    _cachedRemoteBuffer.AddRange(RemoteBuffer);
                    if (_cacheRemoteBufferHeaders)
                    {
                        _cachedRemoteBufferHeaders = Encoding.Default.GetString(_cachedRemoteBuffer.ToArray()).TrimEnd("\0".ToCharArray());

                        int index = _cachedRemoteBufferHeaders.IndexOf("\r\n\r\n");
                        if (index != -1)
                        {
                            _cachedRemoteBufferHeaders = _cachedRemoteBufferHeaders.Substring(0, index);

                            _cacheRemoteBufferHeaders = false;
                            if (Ret != index + 4)
                            {
                                _cachedRemoteBuffer = _cachedRemoteBuffer.GetRange(index + 4, _cachedRemoteBuffer.Count - index - 4);
                            }
                            else
                            {
                                _cachedClientBuffer.Clear();
                                _cachedRemoteBuffer.Clear();
                            }

                            if (OnRemoteHeadersParsed != null)
                            {
                                OnRemoteHeadersParsed.Invoke(_cachedRemoteBufferHeaders);
                            }
                        }
                    }
                    if (OnRemoteReceivedEnd != null)
                    {
                        OnRemoteReceivedEnd.Invoke(DestinationSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer.Count == 0 ? null : _cachedRemoteBuffer, false);
                        //OnRemoteReceivedEnd.BeginInvoke(DestinationSocket, this, _cachedRemoteBufferHeaders, _cachedRemoteBuffer.Count == 0 ? null : _cachedRemoteBuffer, false, null, null);
                    }
                    if (!Cancel)
                    {
                        ClientSocket.BeginSend(RemoteBuffer, 0, Ret, SocketFlags.None, new AsyncCallback(OnClientSent), ClientSocket);
                    }
                    else
                    {
                        Dispose();
                    }
                }
                catch
                {
                    Dispose();
                }
            }
        }