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 connection client socket has completed sending data.
        /// </summary>
        /// <param name="ar">The asynchronous result object for the asynchronous method.</param>
        private void OnConnectionSocketSent(IAsyncResult ar)
        {
            try
            {
                if (connection != null && connection.Socket != null)
                {
                    int dispatched = connection.Socket.EndSend(ar);

                    if (dispatched > 0)
                    {
                        if (destinationSocket != null)
                        {
                            destinationSocket.BeginReceive(destinationBuffer, 0, destinationBuffer.Length, SocketFlags.None, OnDestinationSocketReceive, destinationSocket);
                        }

                        return;
                    }
                }
            }
            catch { }

            Dispose();
        }