Ejemplo n.º 1
0
        /// <summary>
        /// Конструктор чат сервера сообщений
        /// </summary>
        /// <param name="serverHttpPort">Http порт для получения сообщений</param>
        /// <param name="serverTcpPort">TCP порт для работы с файлами</param>
        /// <param name="serverExchangeTcpPort">TCP порт для работы с метаданными</param>
        /// <param name="serverIp">IP адрес сервера</param>
        public ChatServer(int serverHttpPort, int serverTcpPort, int serverExchangeTcpPort, string serverIp = "localhost")
        {
            this.serverIp = serverIp;
            ServiceMessage.AppendLine($"Server Ip {serverIp}");

            this.serverExchangeTcpPort = serverExchangeTcpPort;
            var tcpAddress = new Uri($"net.tcp://{serverIp}:{serverTcpPort}/WPFHost");

            ServiceMessage.AppendLine($"net.tcp        - net.tcp://{serverIp}:{serverTcpPort}/WPFHost");

            var httpAddress = new Uri($"http://{serverIp}:{serverHttpPort}/WPFHost");

            ServiceMessage.AppendLine($"http           - http://{serverIp}:{serverHttpPort}/WPFHost");

            var serverListener = new Uri($"net.pipe://{serverIp}/WPFHost");

            ServiceMessage.AppendLine($"serverListener - net.pipe://{serverIp}/WPFHost");

            Uri[] baseAddresses = { tcpAddress, httpAddress, serverListener };
            host = new ServiceHost(typeof(ChatService), baseAddresses);

            var namedPipeBinding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);

            host.AddServiceEndpoint(typeof(IChatService), namedPipeBinding, "");
        }
Ejemplo n.º 2
0
        public void Connect(string userName, string serverIp)
        {
            try
            {
                localClient      = new Client();
                localClient.Name = userName;
                var context = new InstanceContext(this);
                chatClient = new ChatServiceClient(context);

                //As the address in the configuration file is set to localhost
                //we want to change it so we can call a service in internal
                //network, or over internet
                var servicePath       = chatClient.Endpoint.ListenUri.AbsolutePath;
                var serviceListenPort = chatClient.Endpoint.Address.Uri.Port.ToString();

                chatClient.Endpoint.Address = new EndpointAddress($"net.tcp://{serverIp}:{serviceListenPort}{servicePath}");


                chatClient.Open();

                chatClient.InnerDuplexChannel.Faulted += (sender, args) => HandleProxy();
                chatClient.InnerDuplexChannel.Opened  += (sender, args) => HandleProxy();
                chatClient.InnerDuplexChannel.Closed  += (sender, args) => HandleProxy();
                chatClient.ConnectAsync(localClient);
                Connected = true;
            }
            catch (Exception exception)
            {
                ServiceMessage.AppendLine(exception.Message);
                Connected = false;
            }
        }
Ejemplo n.º 3
0
        public void SendFile(string filePath)
        {
            if (receiver == null)
            {
                return;
            }

            var fileInfo   = new FileInfo(filePath);
            var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);;

            try
            {
                var buffer = new byte[fileStream.Length];

                fileStream.Read(buffer, 0, buffer.Length);

                var fMsg = new FileMessage();
                fMsg.FileName = fileInfo.Name;
                fMsg.Sender   = localClient.Name;
                fMsg.Data     = buffer;
                chatClient.SendFileAsync(fMsg, receiver);
            }
            catch (Exception exception)
            {
                ServiceMessage.AppendLine(exception.Message);
            }
            finally
            {
                fileStream.Close();
            }
        }
Ejemplo n.º 4
0
        private void MetadataBehaviorInit()
        {
            var mBehave = new ServiceMetadataBehavior();

            host.Description.Behaviors.Add(mBehave);
            host.AddServiceEndpoint(typeof(IMetadataExchange),
                                    MetadataExchangeBindings.CreateMexTcpBinding(),
                                    $"net.tcp://{serverIp}:{serverExchangeTcpPort}/WPFHost/mex");
            ServiceMessage.AppendLine($"Behavior       - net.tcp://{serverIp}:{serverExchangeTcpPort}/WPFHost/mex");
        }
Ejemplo n.º 5
0
 public void ReceiverFile(FileMessage fileMsg, Client reciver)
 {
     try
     {
         var fileStream = new FileStream(receiverFilesPath + fileMsg.FileName, FileMode.Create, FileAccess.ReadWrite);
         fileStream.Write(fileMsg.Data, 0, fileMsg.Data.Length);
         ServiceMessage.AppendLine($"Received file, {fileMsg.FileName}");
     }
     catch (Exception exception)
     {
         ServiceMessage.AppendLine(exception.Message);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Инициализация и запуск сервера сообщений
        /// </summary>
        public void StartServer()
        {
            ThrottlingBehaviorInit();
            TcpBindingInit();
            MetadataBehaviorInit();

            ServiceMessage.AppendLine($"MaxConcurrentCalls {MaxConcurrentCalls}");
            ServiceMessage.AppendLine($"MaxConcurrentSessions {MaxConcurrentSessions}");
            ServiceMessage.AppendLine($"MaxConcurrentSessions {MaxConcurrentSessions}");
            ServiceMessage.AppendLine($"MaxSizeBuffer {MaxSizeBuffer}");
            try
            {
                host.Open();
                Open = true;
                ServiceMessage.AppendLine("Start Ok");
            }
            catch (Exception ex)
            {
                Open = false;
                ServiceMessage.AppendLine(ex.Message);
            }
        }