Exemple #1
0
        /// <summary>
        /// Called when the socket receives the response to a connect request.
        /// </summary>
        /// <param name="ar">The asynchronous result object for the asynchronous method.</param>
        private void OnSocketReceiveConnect(IAsyncResult ar)
        {
            try
            {
                int received = socket.EndReceive(ar);

                if (received != 5 || buffer[0] != 5 || buffer[1] != 0)
                {
                    throw new Exception();
                }

                int length = 0;

                switch (buffer[3])
                {
                case 1:
                    length = 5;
                    break;

                case 3:
                    length = buffer[4] + 2;
                    break;

                case 4:
                    length = 17;
                    break;

                default:
                    throw new Exception();
                }

                finalLength = length;

                socket.BeginReceive(buffer, 1, length, SocketFlags.None, OnSocketReceiveConnectFinal, socket);
            }
            catch
            {
                if (callback != null)
                {
                    callback(false);
                }
            }
        }
        /// <summary>
        /// Called when the destination socket has dispatched data.
        /// </summary>
        /// <param name="ar">The asynchronous result object for the asynchronous method.</param>
        private void OnDestinationSocketReceive(IAsyncResult result)
        {
            try
            {
                if (destinationSocket != null)
                {
                    int received = destinationSocket.EndReceive(result);

                    if (received > 0)
                    {
                        if (connection != null && connection.Socket != null)
                        {
                            connection.Socket.BeginSend(destinationBuffer, 0, received, SocketFlags.None, OnConnectionSocketSent, connection.Socket);
                        }

                        return;
                    }
                }
            }
            catch { }

            Dispose();
        }