Example #1
0
        public ClientSocket Start(Action <byte[]> replyMessageReceivedCallback)
        {
            Task.Factory.StartNew(() =>
            {
                _socketService.ReceiveMessage(new SocketInfo(_socket), replyMessage =>
                {
                    try
                    {
                        replyMessageReceivedCallback(replyMessage);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                    }
                });
            }, TaskCreationOptions.None)
            .ContinueWith(task => _logger.Error(task.Exception), TaskContinuationOptions.OnlyOnFaulted);

            return(this);
        }
Example #2
0
        public void Start(Action <ReceiveContext> messageReceivedCallback)
        {
            if (_isRunning)
            {
                return;
            }

            _isRunning = true;

            _listenNewClientWorker = new Worker("ServerSocket.AcceptNewClient", () =>
            {
                if (!_isRunning)
                {
                    return;
                }

                _messageReceivedCallback = messageReceivedCallback;
                _newClientSocketSignal.Reset();

                try
                {
                    _socket.BeginAccept(asyncResult =>
                    {
                        if (!_isRunning)
                        {
                            return;
                        }

                        var clientSocket = _socket.EndAccept(asyncResult);
                        var socketInfo   = new SocketInfo(clientSocket);
                        NotifyNewSocketAccepted(socketInfo);
                        _newClientSocketSignal.Set();

                        if (!_isRunning)
                        {
                            return;
                        }

                        _socketService.ReceiveMessage(socketInfo, receivedMessage =>
                        {
                            var receiveContext = new ReceiveContext(socketInfo, receivedMessage, context =>
                            {
                                _socketService.SendMessage(context.ReplySocketInfo, context.ReplyMessage, sendResult =>
                                {
                                    if (context.ReplySentCallback != null)
                                    {
                                        context.ReplySentCallback(sendResult);
                                    }
                                });
                            });
                            _messageReceivedCallback(receiveContext);
                        });
                    }, _socket);
                }
                catch (SocketException socketException)
                {
                    _logger.Error(string.Format("Socket accept exception, ErrorCode:{0}", socketException.SocketErrorCode), socketException);
                }
                catch (Exception ex)
                {
                    _logger.Error("Unknown socket accept exception.", ex);
                }

                if (_isRunning)
                {
                    _newClientSocketSignal.WaitOne();
                }
            });
            _listenNewClientWorker.Start();
        }