示例#1
0
        /// <summary>
        /// Sends a buffer.
        /// </summary>
        public bool SendAsync(IMessageSocketAsyncEventArgs args)
        {
            ProxyMessageSocketAsyncEventArgs eventArgs = args as ProxyMessageSocketAsyncEventArgs;

            if (eventArgs == null)
            {
                throw new ArgumentNullException("args");
            }
            if (ProxySocket == null)
            {
                throw new InvalidOperationException("The socket is not connected.");
            }
            eventArgs.m_args.SocketError = SocketError.Unknown;
            return(ProxySocket.SendAsync(eventArgs.m_args));
        }
示例#2
0
        /// <summary>
        /// Connects to an endpoint.
        /// </summary>
        public async Task <bool> BeginConnect(Uri endpointUrl, EventHandler <IMessageSocketAsyncEventArgs> callback, object state)
        {
            bool result = false;

            if (endpointUrl == null)
            {
                throw new ArgumentNullException("endpointUrl");
            }

            if (ProxySocket != null)
            {
                throw new InvalidOperationException("The socket is already connected.");
            }

            ProxyMessageSocketAsyncEventArgs args = new ProxyMessageSocketAsyncEventArgs();

            args.UserToken          = state;
            args.m_args.SocketError = SocketError.Host_unknown;

            ProxySocket = new Socket(SocketType.Stream, ProtocolType.Tcp);

            try
            {
                await ProxySocket.ConnectAsync(endpointUrl.DnsSafeHost, endpointUrl.Port);

                args.m_args.SocketError = SocketError.Success;
                result = true;
            }
            catch (Exception e)
            {
                SocketException se = e as SocketException;
                if (se != null)
                {
                    args.m_args.SocketError = se.Error;
                }
            }
            finally
            {
                Task t = Task.Run(() => callback(this, args));
            }
            return(result);
        }