提交 54d25fc0 编写于 作者: T tanghai

增加ServiceComponent,处理连接,分发封包处理

上级 7cb15395
......@@ -6,7 +6,7 @@ namespace Model
{
public class BehaviorTreeComponent: Component<World>, IAssemblyLoader
{
private Dictionary<int, BehaviorTree> trees = new Dictionary<int, BehaviorTree>();
private readonly Dictionary<int, BehaviorTree> trees = new Dictionary<int, BehaviorTree>();
public void Load(Assembly assembly)
{
......@@ -20,5 +20,13 @@ namespace Model
this.trees[nodeConfig.Id] = behaviorTree;
}
}
public BehaviorTree this[int id]
{
get
{
return this.trees[id];
}
}
}
}
\ No newline at end of file
......@@ -111,6 +111,7 @@ namespace Model
this.typeBuff.Remove(buff.Config.Type, buff);
World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.AfterRemoveBuff, env);
buff.Dispose();
}
public void RemoveById(ObjectId id)
......
......@@ -15,8 +15,8 @@ namespace Model
{
this.events = new Dictionary<int, List<IEvent>>();
var types = assembly.GetTypes();
foreach (var t in types)
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
object[] attrs = t.GetCustomAttributes(typeof (AttributeType), false);
if (attrs.Length == 0)
......
......@@ -29,7 +29,7 @@ namespace Model
object obj = (Activator.CreateInstance(type));
IFactory<T> iFactory = obj as IFactory<T>;
var iFactory = obj as IFactory<T>;
if (iFactory == null)
{
throw new Exception(string.Format("class: {0} not inherit from IFactory", type.Name));
......
namespace Model
{
public class MessageComponent
{
public MessageComponent()
{
}
}
}
\ No newline at end of file
using System;
using Common.Base;
using Common.Event;
using Network;
using TNet;
namespace Model
{
public class ServiceComponent: Component<World>
{
private IService service;
public void Run(string host, int port)
{
service = new TService("127.0.0.1", 8888);
service.Add(AcceptChannel);
service.Run();
}
/// <summary>
/// 接收连接
/// </summary>
private async void AcceptChannel()
{
while (true)
{
IChannel channel = await service.GetChannel();
ProcessChannel(channel);
}
}
/// <summary>
/// 接收分发封包
/// </summary>
/// <param name="channel"></param>
private static async void ProcessChannel(IChannel channel)
{
while (true)
{
byte[] packet = await channel.RecvAsync();
Env env = new Env();
env[EnvKey.Packet] = packet;
int opcode = BitConverter.ToUInt16(packet, 0);
World.Instance.GetComponent<EventComponent<MessageAttribute>>().Run(opcode, env);
}
}
}
}
\ No newline at end of file
......@@ -50,7 +50,7 @@ namespace Model
public void Update()
{
long timeNow = TimeHelper.Now();
List<long> timeoutTimer = new List<long>();
var timeoutTimer = new List<long>();
foreach (long time in this.timeId.Keys)
{
if (time > timeNow)
......
......@@ -6,5 +6,6 @@
public const string OwnerId = "OwnerId";
public const string Buff = "Buff";
public const string BuffId = "BuffId";
public const string Packet = "Packet";
}
}
\ No newline at end of file
......@@ -52,7 +52,7 @@
<Compile Include="BehaviorTree\Node.cs" />
<Compile Include="BehaviorTree\NodeAttribute.cs" />
<Compile Include="Buff.cs" />
<Compile Include="Component\MessageComponent.cs" />
<Compile Include="Component\ServiceComponent.cs" />
<Compile Include="Component\BuffComponent.cs" />
<Compile Include="Component\ConfigComponent.cs" />
<Compile Include="Component\EventComponent.cs" />
......@@ -82,6 +82,14 @@
<Project>{19f8f043-1f99-4550-99df-dea5c7d77e55}</Project>
<Name>Common</Name>
</ProjectReference>
<ProjectReference Include="..\..\Platform\Network\Network.csproj">
<Project>{3bd499ff-3c34-4920-8b21-c55fba580843}</Project>
<Name>Network</Name>
</ProjectReference>
<ProjectReference Include="..\..\Platform\TNet\TNet.csproj">
<Project>{b42d431a-3a54-4649-942a-c5356d7f9fbc}</Project>
<Name>TNet</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
......@@ -14,10 +14,10 @@ namespace MongoDBTest
public void TestMongoDB()
{
const string connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test");
var collection = database.GetCollection<Unit>("Unit");
MongoClient client = new MongoClient(connectionString);
MongoServer server = client.GetServer();
MongoDatabase database = server.GetDatabase("test");
MongoCollection<Unit> collection = database.GetCollection<Unit>("Unit");
World world = World.Instance;
......@@ -36,7 +36,7 @@ namespace MongoDBTest
collection.Insert(player1);
var query = Query<Unit>.EQ(e => e.Id, player1.Id);
IMongoQuery query = Query<Unit>.EQ(e => e.Id, player1.Id);
Unit player2 = collection.FindOne(query);
Console.WriteLine(MongoHelper.ToJson(player2));
......
......@@ -21,9 +21,9 @@ namespace Common.Config
throw new Exception(string.Format("not found config path: {0}", path));
}
foreach (var file in Directory.GetFiles(path))
foreach (string file in Directory.GetFiles(path))
{
var t = MongoHelper.FromJson<T>(File.ReadAllText(file));
T t = MongoHelper.FromJson<T>(File.ReadAllText(file));
this.dict.Add(t.Id, t);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册