Esempio n. 1
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="beginMethod"></param>
 /// <param name="endMethod"></param>
 /// <param name="resultCallback"> </param>
 /// <returns></returns>
 public void HandleAsyncVoid <T>(Fleck2Extensions.Func <AsyncCallback, object, T> beginMethod,
                                 Fleck2Extensions.Action <T> endMethod, Action <SocketResult> resultCallback)
 {
     DoAsyncTask(() => beginMethod(result => DoAsyncTask(() =>
     {
         endMethod((T)result);
         resultCallback(new SocketResult(true));
     }, resultCallback), null), resultCallback);
 }
        public void Send(byte[] buffer, Fleck2Extensions.Action callback, Fleck2Extensions.Action <Exception> error)
        {
            Fleck2Extensions.Func <AsyncCallback, object, IAsyncResult> begin =
                (cb, s) => _stream.BeginWrite(buffer, 0, buffer.Length, cb, s);

            _socketFactory.HandleAsyncVoid(begin, _stream.EndWrite, result =>
            {
                result.Success(callback);
                result.Error(error);
            });
        }
Esempio n. 3
0
        public void Receive(byte[] buffer, Fleck2Extensions.Action <int> callback, Fleck2Extensions.Action <Exception> error, int offset = 0)
        {
            Fleck2Extensions.Func <AsyncCallback, object, IAsyncResult> begin = (cb, data) =>
                                                                                _stream.BeginRead(buffer, offset, buffer.Length, cb, data);

            _socketFactory.HandleAsync(begin, _stream.EndRead, result =>
            {
                result.Success(callback);
                result.Error(error);
            });
        }
        public void Accept(Fleck2Extensions.Action <ISocket> callback, Fleck2Extensions.Action <Exception> error)
        {
            Fleck2Extensions.Func <IAsyncResult, ICancellationToken, ISocket> end = (result, token) =>
            {
                token.ThrowIfCancellationRequested();
                return(new SocketWrapper(_socket.EndAccept(result)));
            };

            _socketFactory.HandleAsync(_socket.BeginAccept, end, result =>
            {
                result.Success(callback);
                result.Error(error);
            });
        }
Esempio n. 5
0
 public WebSocketConnection(ISocket socket, Action <IWebSocketConnection> initialize,
                            Fleck2Extensions.Func <byte[], WebSocketHttpRequest> parseRequest,
                            Fleck2Extensions.Func <WebSocketHttpRequest, IHandler> handlerFactory)
 {
     Socket          = socket;
     OnOpen          = () => { };
     OnClose         = () => { };
     OnMessage       = x => { };
     OnBinary        = x => { };
     OnError         = x => { };
     _initialize     = initialize;
     _handlerFactory = handlerFactory;
     _parseRequest   = parseRequest;
 }
 public WebSocketConnection(ISocket socket, Action<IWebSocketConnection> initialize, 
     Fleck2Extensions.Func<byte[], WebSocketHttpRequest> parseRequest, 
     Fleck2Extensions.Func<WebSocketHttpRequest, IHandler> handlerFactory)
 {
     Socket = socket;
     OnOpen = () => { };
     OnClose = () => { };
     OnMessage = x => { };
     OnBinary = x => { };
     OnError = x => { };
     _initialize = initialize;
     _handlerFactory = handlerFactory;
     _parseRequest = parseRequest;
 }
        public void Authenticate(X509Certificate2 certificate, Fleck2Extensions.Action callback, Fleck2Extensions.Action <Exception> error)
        {
            var ssl = new SslStream(_stream, false);

            _stream = ssl;

            Fleck2Extensions.Func <AsyncCallback, object, IAsyncResult> begin =
                (cb, s) => ssl.BeginAuthenticateAsServer(certificate, false, SslProtocols.Tls, false, cb, s);

            _socketFactory.HandleAsyncVoid(begin, ssl.EndAuthenticateAsServer, result =>
            {
                result.Success(callback);
                result.Error(error);
            });
        }
Esempio n. 8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="beginMethod"></param>
 /// <param name="endMethod"></param>
 /// <param name="resultCallback"> </param>
 /// <returns></returns>
 public void HandleAsync(Fleck2Extensions.Func <AsyncCallback, object, IAsyncResult> beginMethod,
                         Fleck2Extensions.Func <IAsyncResult, ICancellationToken, ISocket> endMethod, Action <SocketResult> resultCallback)
 {
     DoAsyncTask(() => beginMethod(result => DoAsyncTask(() =>
                                                         resultCallback(new SocketResult(endMethod(result, Token))), resultCallback), null), resultCallback);
 }