Пример #1
0
        private void ServiceClient(TcpClient client)
        {
            string requestedPath = "";
            var stream = client.GetStream();

            try {
                byte[] buffer = new byte[8000];
                StringBuilder data = new StringBuilder();
                bool httpRequestComplete = false;
                while (client.Connected) {
                    if (stream.DataAvailable && !httpRequestComplete) {
                        int bytes_read = stream.Read(buffer, 0, buffer.Length);
                        data.Append(ASCIIEncoding.ASCII.GetString(buffer, 0, bytes_read));

                        if (data.ToString().Contains("\r\n\r\n")) {
                            Logger.ReportInfo("Request complete");
                            Logger.ReportInfo("Data were: " + data.ToString());
                            httpRequestComplete = true;
                        }
                    } else {
                        Thread.Sleep(1);
                    }

                    if (httpRequestComplete) {

                        ProxyInfo info;
                        var headers = new HttpHeaders(data.ToString());
                        requestedPath = headers.Path.Replace("/", "");
                        using (ChannelFactory<ITrailerProxy> factory = new ChannelFactory<ITrailerProxy>(new NetNamedPipeBinding(), "net.pipe://localhost/mbtrailers"))
                        {
                            ITrailerProxy proxyServer = factory.CreateChannel();
                            info = proxyServer.GetProxyInfo(requestedPath);
                            (proxyServer as ICommunicationObject).Close();
                        }
                        if (info == null)
                        {
                            //probably a request from the player for art - ignore it
                            //Logger.ReportError("Unable to get info for item: " + requestedPath);
                            break;
                        }
                        var target = Path.Combine(cacheDir, info.LocalFilename);

                        if (File.Exists(target)) {
                            ServeStaticFile(stream, buffer, info, target);
                            break;
                        }

                        string cacheFile = Path.Combine(cacheDir, info.LocalFilename + ".tmp");

                        if (File.Exists(cacheFile)) {
                            ServeCachedFile(stream, cacheFile, info);
                            break;
                        }

                        try {
                            ProxyRemoteFile(stream, buffer, info, target, cacheFile);
                        } catch (Exception ee) {
                            Logger.ReportException("Failed to proxy file : " + requestedPath, ee);
                        }
                    }
                }
            } catch (Exception e) {
                Logger.ReportException("Failed to serve file : " + requestedPath, e);
            } finally {
                Interlocked.Decrement(ref incomingConnections);

                try
                {
                    Logger.ReportInfo("MB Trailers closing connections.");
                    stream.Close();
                    client.Close();
                }
                catch
                {
                    // well we tried
                }
            }
        }