void Connect_Connect_End(IAsyncResult ar) { Connect_SO stateObj = (Connect_SO)ar.AsyncState; try { stateObj.UpdateContext(); _socket.EndConnect(ar); //------------------------------------------ // Send CONNECT command byte[] cmd = GetConnectCmd(stateObj.HostName, stateObj.HostPort, stateObj.UseCredentials); NStream.BeginWrite(cmd, 0, cmd.Length, new AsyncCallback(Connect_Write_End), stateObj); } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } }
void Connect_Connect_End(IAsyncResult ar) { Connect_SO stateObj = (Connect_SO)ar.AsyncState; try { stateObj.UpdateContext(); _socket.EndConnect(ar); _localEndPoint = null; _remoteEndPoint = stateObj.RemoteEndPoint; //------------------------------------ // Send CONNECT command // byte[] cmd = PrepareConnectCmd(stateObj.RemoteEndPoint); NStream.BeginWrite(cmd, 0, cmd.Length, new AsyncCallback(Connect_Write_End), stateObj); } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } }
void Bind_Connect_End(IAsyncResult ar) { Bind_SO stateObj = (Bind_SO)ar.AsyncState; try { stateObj.UpdateContext(); _socket.EndConnect(ar); //------------------------------------ // Send CONNECT command // byte[] cmd = PrepareBindCmd(stateObj.BaseSocket); NStream.BeginWrite(cmd, 0, cmd.Length, new AsyncCallback(Bind_Write_End), stateObj); } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } }
void Connect_ReadReply_End(IAsyncResult ar) { Connect_SO stateObj = (Connect_SO)ar.AsyncState; try { stateObj.UpdateContext(); ByteVector reply = EndReadReply(ar); //------------------------------------------ // Analyze reply string reason = null; int code = AnalyzeReply(reply, out reason); //------------------------------------------ // is good return code? if (code >= 200 && code <= 299) { stateObj.SetCompleted(); } else if ((407 == code) && !(stateObj.UseCredentials) && (_proxyUser != null)) { //------------------------------------------ // If Proxy Authentication Required // but we do not issued it, then try again stateObj.UseCredentials = true; //------------------------------------------ // Send CONNECT command byte[] cmd = GetConnectCmd( stateObj.HostName, stateObj.HostPort, stateObj.UseCredentials); NStream.BeginWrite(cmd, 0, cmd.Length, new AsyncCallback(Connect_Write_End), stateObj); } else { // string msg = string.Format("Connection refused by web proxy: {0} ({1}).", reason, code); // throw new ProxyErrorException(msg); throw new SocketException(SockErrors.WSAECONNREFUSED); } } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } }