예제 #1
0
        private async void ConnectAndListen(string socketPath)
        {
            try
            {
                using (_socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
                {
                    var ep = new UnixDomainSocketEndPoint(socketPath);
                    await _socket.ConnectAsync(ep);

                    var clientReceives = Task.Run(async() =>
                    {
                        while (true)
                        {
                            var buffer = new byte[1024];
                            await _socket.ReceiveAsync(new ArraySegment <byte>(buffer), SocketFlags.None);
                            var message = Encoding.ASCII.GetString(buffer);
                            message     = message.Remove(message.IndexOf('\n'));
                            OnMessageReceived(new MessageEventArgs()
                            {
                                Message = message
                            });
                        }
                    });

                    await clientReceives;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Socket Error: " + ex.Message);
            }
        }
        private async void ConnectAndListen(string socketPath)
        {
            try
            {
                using (_socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
                {
                    var ep = new UnixDomainSocketEndPoint(socketPath);
                    await _socket.ConnectAsync(ep);

                    var clientReceives = Task.Run(async() =>
                    {
                        var message = "";
                        while (true)
                        {
                            var buffer         = new byte[1024];
                            int bytesReceived  = await _socket.ReceiveAsync(new ArraySegment <byte>(buffer), SocketFlags.None);
                            var messagePart    = Encoding.UTF8.GetString(buffer, 0, bytesReceived);
                            var linebreakIndex = messagePart.IndexOf('\n');
                            if (linebreakIndex == -1)
                            {
                                message += messagePart;
                            }
                            else
                            {
                                message += messagePart.Substring(0, linebreakIndex);
                                OnMessageReceived(new MessageEventArgs()
                                {
                                    Message = message
                                });
                                message = "";
                            }
                        }
                    });

                    await clientReceives;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Socket Error: " + ex.Message);
            }
        }