Example #1
0
        public override void UpdateConfiguration(MediaBrowser.Model.Plugins.BasePluginConfiguration configuration)
        {
            PluginConfiguration pc = configuration as PluginConfiguration;

            SocketClientAsync.ChangeAddress(pc.ServerIP, pc.ServerPort);            // throws exception if can't connect with new data thus skipping save
            bool changedIp = (pc.ServerIP != Configuration.ServerIP);               // true if ip was changed
            bool changedPort = (pc.ServerPort != Configuration.ServerPort);         // true if ip was changed

            if (changedIp || changedPort)
                WMCService.AddonGoingDown();                                        // tell current serverwmc at old IP, this mbs is going down

            base.UpdateConfiguration(configuration);                                // update to new config data

            // if Ip chaged, tell core to change datasource
            if (changedIp || changedPort)                                           //  tell mbs to load new data source
            {
                WMCService.DataSourceChange();
            }
        }
Example #2
0
        /// <summary>
        /// get results from socket server using TPL
        /// </summary>
        /// <param name="command">command to send to server</param>
        /// <param name="streamId">stream Id to embed on command</param>
        /// <param name="cancelToken">for canceling asyc operation</param>
        /// <returns>array of string reponses from server</returns>
        public static async Task <string[]> GetVectorAsync(string command, CancellationToken cancelToken, string streamId = "")
        {
            Socket            socket = null;
            SocketClientAsync asyncS = new SocketClientAsync();

            await _socketSemaphore.WaitAsync(cancelToken).ConfigureAwait(false);    // one at a time query of server

            try
            {
                // Create a TCP/IP socket.
                socket = new Socket(_ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                // Connect to the remote endpoint
                await Task.Factory.FromAsync(socket.BeginConnect(_remoteEP, null, socket), socket.EndConnect);

                // build the request string
                string sendCommand = string.Format("MediaBrowser^@{1}@{2}|{0}<Client Quit>", command, _machineName, streamId);
                byte[] bytesToSend = Encoding.UTF8.GetBytes(sendCommand);

                // send the command string to server
                int bytesSent = await Task <int> .Factory.FromAsync(socket.BeginSend(bytesToSend, 0, bytesToSend.Length, 0, null, socket), socket.EndSend);

                //Debug.WriteLine("Sent {0} bytes to server.", bytesSent);

                // use this to accumulate server responses
                StringBuilder sb = new StringBuilder();

                // keep getting results from server until zero bytes are read
                int bytesRead;
                while ((bytesRead = await Task <int> .Factory.FromAsync(socket.BeginReceive(_buffer, 0, _buffer.Length, 0, null, socket), socket.EndReceive)) > 0)
                {
                    sb.Append(Encoding.UTF8.GetString(_buffer, 0, bytesRead));      // accumulate results in sb
                    //Debug.WriteLine("Bytes read: " + bytesRead);
                }

                // convert input string to results array
                string        bigStr  = sb.ToString();
                List <string> results = bigStr.Split(_stringSeparators, StringSplitOptions.None).ToList();

                // check/clean up results
                if (results.Last() == "<EOF>")
                {
                    results.RemoveAt(results.Count - 1);                            // remove EOF
                }
                else
                {
                    throw new Exception("Error> ServerWMC response is incomplete"); // EOF not found
                }
                // Release the socket.
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();

                return(results.ToArray());
            }
            catch (Exception e)
            {
                //Debug.WriteLine(e.ToString());
                throw e;                            // rethrow to let mbs handle
            }
            finally
            {
                _socketSemaphore.Release();
            }
        }