예제 #1
0
파일: ClientSocket.cs 프로젝트: inrg/KryX2
        //inserts information needed for async receive.
        //I could pass the asynceventargs instead of sd but for now
        //im leaving it at sd for clarification
        internal void StartReceive()
        {
            //if connection is closed
            if (SocketActions.IsConnectionClosed(_socket))
            {
                SocketActions.ProcessError(this, "StartReceive: connection closed");
                return;
            }

            //determine if there is data to be read.
            byte[] data = RetrieveReceived();
            if (data != null)
            {
                //let handlereceived decide what to do with the data
                SocketActions.HandleReceived(this, data);
                //exit method since we dont need to set a completion event as we already
                //know there is data
                return;
            }

            try
            {
                SocketAsyncEventArgs eventArgs = new SocketAsyncEventArgs();
                byte[] buffer = new byte[512]; //255 //r/
                eventArgs.SetBuffer(buffer, 0, 512);
                eventArgs.UserToken  = _socket;
                eventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(DataReceived);
                _socket.ReceiveAsync(eventArgs);
            }
            catch
            {
                SocketActions.ProcessError(this, "StartReceive: catch");
            }
        }
예제 #2
0
파일: ClientSocket.cs 프로젝트: inrg/KryX2
 internal void DataReceived(object sender, SocketAsyncEventArgs e)
 {
     byte[] data = RetrieveReceived(e);
     if (data != null)
     {
         SocketActions.HandleReceived(this, data);
     }
     //null array or 0 bytes means graceful socket close
     else
     {
         //SocketActions.ProcessError(this, "DataReceived: null data");
     }
 }
예제 #3
0
파일: ClientSocket.cs 프로젝트: inrg/KryX2
 internal void ConnectClient()
 {
     try
     {
         this.ProxyAuthorized = false;
         ResetTiming();
         _timing.StartTimer();
         _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         IPEndPoint           hostEndPoint = new IPEndPoint(this.ProxyAddress, this.ProxyPort);
         SocketAsyncEventArgs eventArgs    = new SocketAsyncEventArgs();
         eventArgs.RemoteEndPoint = hostEndPoint;
         eventArgs.UserToken      = _socket;
         eventArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(ClientConnected);
         _socket.ConnectAsync(eventArgs);
     }
     catch
     {
         SocketActions.ProcessError(this, "ConnectClient: catch");
     }
 }
예제 #4
0
파일: ClientSocket.cs 프로젝트: inrg/KryX2
        private byte[] RetrieveReceived()
        {
            try
            {
                int bytesRead = _socket.Available;
                if (bytesRead == 0)
                {
                    return(null);
                }

                byte[] result = new byte[bytesRead];
                _socket.Receive(result);
                return(result);
            }
            catch
            {
                SocketActions.ProcessError(this, "RetrieveReceived: catch");
                return(null);
            }
        }
예제 #5
0
파일: ClientSocket.cs 프로젝트: inrg/KryX2
        private byte[] RetrieveReceived(SocketAsyncEventArgs e)
        {
            try
            {
                int bytesRead = e.BytesTransferred;
                //if 0 socket has been closed
                if (bytesRead == 0)
                {
                    return(null);
                }
                byte[] result = new byte[bytesRead];
                Buffer.BlockCopy(e.Buffer, 0, result, 0, bytesRead);

                return(result);
            }
            catch
            {
                SocketActions.ProcessError(this, "RetrieveReceived(e): catch");
                return(null);
            }
        }
예제 #6
0
        } //end authorizeproxy

        internal static void SendProxyHeader(ClientSocket cs)
        {
            //r/ if proxy format isn't specified assume socks4. normally we'd let user decide default if not specified or determined.
            if (cs.ProxyFormat == ProxyType.Default)
            {
                cs.ProxyFormat = ProxyType.Http;
            }

            switch (cs.ProxyFormat)
            {
            case ProxyType.Http:
                SocketActions.SendData(cs, KryX2.Settings.GeneratedSettings.HttpHeader);
                break;

            case ProxyType.Socks4:
                SocketActions.SendData(cs, KryX2.Settings.GeneratedSettings.Socks4Header);
                break;

            case ProxyType.Socks5:
                //socks5 is currently unused
                break;
            }
        }
예제 #7
0
파일: ClientSocket.cs 프로젝트: inrg/KryX2
        private void ClientConnected(object sender, SocketAsyncEventArgs e)
        {
            //free a semaphore
            SocketActions.ReleaseCurrentlyConnecting();
            //resets timeout
            ResetTiming();
            //determine if connection has been made
            bool connected = (e.SocketError == SocketError.Success);

            //define socketdata
            if (connected)
            {
                //sends header based on if using proxy or not and server type
                SocketActions.SendProxyHeader(this);
                //begins async receive
                StartReceive();
            }
            else
            {
                //failed to connect successfully
                SocketActions.ProcessError(this, "ClientConnected: failed to connect");
            }
        }