From b7d9de1b402c5ae77d6ab9010d002998d8c47383 Mon Sep 17 00:00:00 2001 From: MW Date: Mon, 5 Mar 2007 15:35:18 +0000 Subject: [PATCH] Started to port Local AssetCache from standalone branch --- Assets/AssetCache.cs | 302 ++++++++++++++++++++++++++++++++++++ GridServers/IAssetServer.cs | 12 +- GridServers/LoginServer.cs | 14 +- Main.cs | 12 +- OpenSimClient.cs | 8 +- Second-server.csproj | 2 + 6 files changed, 324 insertions(+), 26 deletions(-) create mode 100644 Assets/AssetCache.cs diff --git a/Assets/AssetCache.cs b/Assets/AssetCache.cs new file mode 100644 index 0000000000..47f1842ee3 --- /dev/null +++ b/Assets/AssetCache.cs @@ -0,0 +1,302 @@ +/* +* Copyright (c) OpenSim project, http://sim.opensecondlife.org/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of the nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.Collections.Generic; +using libsecondlife; +using libsecondlife.Packets; +using OpenSim; +using OpenSim.GridServers; + +namespace OpenSim.Assets +{ + /// + /// Manages local cache of assets and their sending to viewers. + /// + public class AssetCache : IAssetReceived + { + public Dictionary Assets; + public Dictionary Textures; + + public List AssetRequests = new List(); //assets ready to be sent to viewers + public List TextureRequests = new List(); //textures ready to be sent + + public List RequestedAssets = new List(); //Assets requested from the asset server + public List RequestedTextures = new List(); //Textures requested from the asset server + + private IAssetServer _assetServer; + + /// + /// + /// + public AssetCache( IAssetServer assetServer) + { + _assetServer = assetServer; + _assetServer.SetReceiver(this); + } + + /// + /// + /// + private void RunAssetManager() + { + this.ProcessAssetQueue(); + this.ProcessTextureQueue(); + } + + /// + /// + /// + private void ProcessTextureQueue() + { + if(this.TextureRequests.Count == 0) + { + //no requests waiting + return; + } + int num; + //should be running in its own thread but for now is called by timer + + if(this.TextureRequests.Count < 5) + { + //lower than 5 so do all of them + num = this.TextureRequests.Count; + } + else + { + num=5; + } + AssetRequest req; + for(int i = 0; i < num; i++) + { + req=(AssetRequest)this.TextureRequests[i]; + + if(req.PacketCounter == 0) + { + //first time for this request so send imagedata packet + if(req.NumPackets == 1) + { + //only one packet so send whole file + ImageDataPacket im = new ImageDataPacket(); + im.ImageID.Packets = 1; + im.ImageID.ID = req.ImageInfo.FullID; + im.ImageID.Size = (uint)req.ImageInfo.Data.Length; + im.ImageData.Data = req.ImageInfo.Data; + im.ImageID.Codec = 2; + req.RequestUser.OutPacket(im); + req.PacketCounter++; + //req.ImageInfo.l= time; + //System.Console.WriteLine("sent texture: "+req.image_info.FullID); + } + else + { + //more than one packet so split file up + ImageDataPacket im = new ImageDataPacket(); + im.ImageID.Packets = (ushort)req.NumPackets; + im.ImageID.ID = req.ImageInfo.FullID; + im.ImageID.Size = (uint)req.ImageInfo.Data.Length; + im.ImageData.Data = new byte[600]; + Array.Copy(req.ImageInfo.Data, 0, im.ImageData.Data, 0, 600); + im.ImageID.Codec = 2; + req.RequestUser.OutPacket(im); + req.PacketCounter++; + //req.ImageInfo.last_used = time; + //System.Console.WriteLine("sent first packet of texture: + } + } + else + { + //send imagepacket + //more than one packet so split file up + ImagePacketPacket im = new ImagePacketPacket(); + im.ImageID.Packet = (ushort)req.PacketCounter; + im.ImageID.ID = req.ImageInfo.FullID; + int size = req.ImageInfo.Data.Length - 600 - 1000*(req.PacketCounter - 1); + if(size > 1000) size = 1000; + im.ImageData.Data = new byte[size]; + Array.Copy(req.ImageInfo.Data, 600 + 1000*(req.PacketCounter - 1), im.ImageData.Data, 0, size); + req.RequestUser.OutPacket(im); + req.PacketCounter++; + //req.ImageInfo.last_used = time; + //System.Console.WriteLine("sent a packet of texture: "+req.image_info.FullID); + } + } + + //remove requests that have been completed + for(int i = 0; i < num; i++) + { + req=(AssetRequest)this.TextureRequests[i]; + if(req.PacketCounter == req.NumPackets) + { + this.TextureRequests.Remove(req); + } + } + + } + public void AssetReceived(AssetBase asset) + { + //check if it is a texture or not + //then add to the correct cache list + //then check for waiting requests for this asset/texture (in the Requested lists) + //and move those requests into the Requests list. + } + + public void AssetNotFound(AssetBase asset) + { + //the asset server had no knowledge of requested asset + + } + + #region Assets + /// + /// + /// + /// + /// + public void AddAssetRequest(OpenSimClient userInfo, TransferRequestPacket transferRequest) + { + LLUUID requestID = new LLUUID(transferRequest.TransferInfo.Params, 0); + //check to see if asset is in local cache, if not we need to request it from asset server. + if(!this.Assets.ContainsKey(requestID)) + { + //not found asset + // so request from asset server + AssetRequest request = new AssetRequest(); + request.RequestUser = userInfo; + request.RequestImage = requestID; + this.AssetRequests.Add(request); + this._assetServer.RequestAsset(requestID); + return; + } + //it is in our cache + AssetInfo info = this.Assets[requestID]; + + //work out how many packets it should be sent in + // and add to the AssetRequests list + } + + /// + /// + /// + private void ProcessAssetQueue() + { + + } + + #endregion + + #region Textures + /// + /// + /// + /// + /// + public void AddTextureRequest(OpenSimClient userInfo, LLUUID imageID) + { + //check to see if texture is in local cache, if not request from asset server + if(!this.Textures.ContainsKey(imageID)) + { + //not is cache so request from asset server + AssetRequest request = new AssetRequest(); + request.RequestUser = userInfo; + request.RequestImage = imageID; + this.TextureRequests.Add(request); + this._assetServer.RequestAsset(imageID); + return; + } + TextureImage imag = this.Textures[imageID]; + AssetRequest req = new AssetRequest(); + req.RequestUser = userInfo; + req.RequestImage = imageID; + req.ImageInfo = imag; + + if(imag.Data.LongLength>600) + { + //over 600 bytes so split up file + req.NumPackets = 1 + (int)(imag.Data.Length-600+999)/1000; + } + else + { + req.NumPackets = 1; + } + + this.TextureRequests.Add(req); + } + #endregion + + } + + public class AssetRequest + { + public OpenSimClient RequestUser; + public LLUUID RequestImage; + public AssetInfo AssetInf; + public TextureImage ImageInfo; + public long DataPointer = 0; + public int NumPackets = 0; + public int PacketCounter = 0; + //public bool AssetInCache; + //public int TimeRequested; + + public AssetRequest() + { + + } + } + + public class AssetBase + { + public byte[] Data; + public LLUUID FullID; + public sbyte Type; + public sbyte InvType; + public string Name; + public string Description; + + public AssetBase() + { + + } + } + + public class AssetInfo : AssetBase + { + public AssetInfo() + { + + } + } + + public class TextureImage : AssetBase + { + public TextureImage() + { + + } + } + +} diff --git a/GridServers/IAssetServer.cs b/GridServers/IAssetServer.cs index abd6fdf464..f6141b96a5 100644 --- a/GridServers/IAssetServer.cs +++ b/GridServers/IAssetServer.cs @@ -27,6 +27,7 @@ using System; using libsecondlife; +using OpenSim.Assets; namespace OpenSim.GridServers { @@ -47,11 +48,11 @@ namespace OpenSim.GridServers { } - public void UpdateAsset() + public void UpdateAsset(AssetBase asset) { } - public void UploadNewAsset() + public void UploadNewAsset(AssetBase asset) { } @@ -61,13 +62,14 @@ namespace OpenSim.GridServers { void SetReceiver(IAssetReceived receiver); void RequestAsset(LLUUID assetID); - void UpdateAsset(); - void UploadNewAsset(); + void UpdateAsset(AssetBase asset); + void UploadNewAsset(AssetBase asset); } // could change to delegate public interface IAssetReceived { - void AssetReceived(); + void AssetReceived(AssetBase asset); + void AssetNotFound(AssetBase asset); } } diff --git a/GridServers/LoginServer.cs b/GridServers/LoginServer.cs index 86949f3a9c..81dced83b1 100644 --- a/GridServers/LoginServer.cs +++ b/GridServers/LoginServer.cs @@ -64,8 +64,8 @@ namespace OpenSim.GridServers private string _mpasswd; private bool _needPasswd=false; - // InitializeLoginProxy: initialize the login proxy - private void InitializeLoginProxy() { + // InitializeLogin: initialize the login + private void InitializeLogin() { loginServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); loginServer.Bind(new IPEndPoint(remoteAddress, _loginPort)); loginServer.Listen(1); @@ -90,13 +90,13 @@ namespace OpenSim.GridServers public void Startup() { - this.InitializeLoginProxy(); - Thread runLoginProxy = new Thread(new ThreadStart(RunLoginProxy)); + this.InitializeLogin(); + Thread runLoginProxy = new Thread(new ThreadStart(RunLogin)); runLoginProxy.IsBackground = true; runLoginProxy.Start(); } - private void RunLoginProxy() + private void RunLogin() { Console.WriteLine("Starting Login Server"); try @@ -113,7 +113,7 @@ namespace OpenSim.GridServers try { - ProxyLogin(networkReader, networkWriter); + LoginRequest(networkReader, networkWriter); } catch (Exception e) { @@ -138,7 +138,7 @@ namespace OpenSim.GridServers } // ProxyLogin: proxy a login request - private void ProxyLogin(StreamReader reader, StreamWriter writer) + private void LoginRequest(StreamReader reader, StreamWriter writer) { lock(this) { diff --git a/Main.cs b/Main.cs index c437f2369b..60de7c869a 100644 --- a/Main.cs +++ b/Main.cs @@ -112,9 +112,9 @@ namespace OpenSim } private void Startup() { - timer1.Enabled = true; - timer1.Interval = 100; - timer1.Elapsed +=new ElapsedEventHandler( this.Timer1Tick ); + timer1.Enabled = true; + timer1.Interval = 100; + timer1.Elapsed +=new ElapsedEventHandler( this.Timer1Tick ); // We check our local database first, then the grid for config options Console.WriteLine("Main.cs:Startup() - Loading configuration"); @@ -132,8 +132,6 @@ namespace OpenSim Console.WriteLine("Main.cs:Startup() - Starting up messaging system"); local_world.PhysScene = this.physManager.GetPhysicsScene("PhysX"); //should be reading from the config file what physics engine to use - //MainListener = new Thread(new ThreadStart(MainServerListener)); - //MainListener.Start(); MainServerListener(); } @@ -175,9 +173,7 @@ namespace OpenSim Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null); Console.WriteLine("Main.cs:MainServerListener() - Listening..."); - /*while(true) { - Thread.Sleep(1000); - }*/ + } void Timer1Tick( object sender, System.EventArgs e ) diff --git a/OpenSimClient.cs b/OpenSimClient.cs index a3994b70d8..a8c06f488a 100644 --- a/OpenSimClient.cs +++ b/OpenSimClient.cs @@ -46,8 +46,6 @@ namespace OpenSim public LLUUID AgentID; public LLUUID SessionID; - private string AgentFirstName; - private string AgentLastName; public uint CircuitCode; public world.Avatar ClientAvatar; private UseCircuitCodePacket cirpack; @@ -407,8 +405,6 @@ namespace OpenSim OpenSim_Main.local_world.AddViewerAgent(this); world.Entity tempent=OpenSim_Main.local_world.Entities[this.AgentID]; this.ClientAvatar=(world.Avatar)tempent; - this.ClientAvatar.firstname = this.AgentFirstName; - this.ClientAvatar.lastname = this.AgentLastName; } private void AuthUser() @@ -424,12 +420,12 @@ namespace OpenSim { Console.WriteLine("OpenSimClient.cs:AuthUser() - Got authenticated connection from " + userEP.ToString()); //session is authorised - this.AgentFirstName = sessionInfo.LoginInfo.First; - this.AgentLastName = sessionInfo.LoginInfo.Last; this.AgentID=cirpack.CircuitCode.ID; this.SessionID=cirpack.CircuitCode.SessionID; this.CircuitCode=cirpack.CircuitCode.Code; InitNewClient(); + this.ClientAvatar.firstname = sessionInfo.LoginInfo.First; + this.ClientAvatar.lastname = sessionInfo.LoginInfo.Last; ClientLoop(); } } diff --git a/Second-server.csproj b/Second-server.csproj index 1126572ad8..8c63ba6eef 100644 --- a/Second-server.csproj +++ b/Second-server.csproj @@ -68,9 +68,11 @@ + + \ No newline at end of file