Skip to content

Commit

Permalink
unity升级到5.5.2,增加vs调试启动参数,很多人问我,所以加上去了。增加主线程同步上下文,mongodb异步函数就能回调到主线程
Browse files Browse the repository at this point in the history
  • Loading branch information
egametang committed Mar 30, 2017
1 parent ba4730a commit e17b4ab
Show file tree
Hide file tree
Showing 29 changed files with 182 additions and 59 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

*.pyc
*.suo
*.user

.settings/
.localhistory/
Expand Down
3 changes: 2 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
����:
2017-3-30: unity������5.5.2������vs���������������ܶ������ң����Լ���ȥ�ˡ��������߳�ͬ�������ģ�mongodb�첽�������ܻص������߳�

2017-2-17: ʹ��ILRuntime�ṩUnity C#�����ȸ���֧��,����Ĩƽ��Mono���ILRuntime�����,ʹ�����������ԭ���÷�����һ��, �ӵ����lua�����!


Expand Down
4 changes: 4 additions & 0 deletions Server/App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ internal static class Program
{
private static void Main(string[] args)
{
// 异步方法全部会回掉到主线程
SynchronizationContext.SetSynchronizationContext(new OneThreadSynchronizationContext());

try
{
Game.EntityEventManager.Register("Model", typeof(Game).Assembly);
Expand Down Expand Up @@ -68,6 +71,7 @@ private static void Main(string[] args)
try
{
Thread.Sleep(1);
Game.Poller.Update();
Game.EntityEventManager.Update();
}
catch (Exception e)
Expand Down
7 changes: 7 additions & 0 deletions Server/App/Server.App.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartArguments>--appId=1 --appType=AllServer --config=../Config/StartConfig/LocalAllServer.txt</StartArguments>
<StartAction>Project</StartAction>
</PropertyGroup>
</Project>
2 changes: 2 additions & 0 deletions Server/Hotfix/Message/C2R_LoginHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ protected override async void Run(Session session, C2R_Login message, Action<R2C
response.Address = outerAddress;
response.Key = g2RGetLoginKey.Key;
reply(response);

session.Send(new R2C_ServerLog());
}
catch (Exception e)
{
Expand Down
12 changes: 12 additions & 0 deletions Server/Model/Other/OneThreadSynchronizationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading;

namespace Model
{
public class OneThreadSynchronizationContext : SynchronizationContext
{
public override void Post(SendOrPostCallback callback, object state)
{
Game.Poller.Add(() => { callback(state); });
}
}
}
4 changes: 4 additions & 0 deletions Server/Model/Server.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\InnerMessage.cs">
<Link>Entity\Message\InnerMessage.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\Opcode.cs">
<Link>Message\Opcode.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\OuterMessage.cs">
<Link>Entity\Message\OuterMessage.cs</Link>
</Compile>
Expand Down Expand Up @@ -205,6 +208,7 @@
<Compile Include="Message\AMHandler.cs" />
<Compile Include="Message\IMHandler.cs" />
<Compile Include="Message\MessageHandlerAttribute.cs" />
<Compile Include="Other\OneThreadSynchronizationContext.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions Server/ThirdParty/ENet/ENet.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public object Run(ILMethod method, object instance, object[] p)
throw new NullReferenceException();
#if UNITY_EDITOR
if(System.Threading.Thread.CurrentThread.ManagedThreadId == AppDomain.UnityMainThreadID)
UnityEngine.Profiler.BeginSample(method.ToString());
UnityEngine.Profiling.Profiler.BeginSample(method.ToString());
#endif
OpCode[] body = method.Body;
StackFrame frame;
Expand Down Expand Up @@ -1697,12 +1697,12 @@ public object Run(ILMethod method, object instance, object[] p)
#endif
#if UNITY_EDITOR
if(System.Threading.Thread.CurrentThread.ManagedThreadId == AppDomain.UnityMainThreadID)
UnityEngine.Profiler.BeginSample(cm.ToString());
UnityEngine.Profiling.Profiler.BeginSample(cm.ToString());
#endif
object result = cm.Invoke(this, esp, mStack);
#if UNITY_EDITOR
if(System.Threading.Thread.CurrentThread.ManagedThreadId == AppDomain.UnityMainThreadID)
UnityEngine.Profiler.EndSample();
UnityEngine.Profiling.Profiler.EndSample();
#endif
if (result is CrossBindingAdaptorType)
result = ((CrossBindingAdaptorType)result).ILInstance;
Expand Down Expand Up @@ -3707,7 +3707,7 @@ public object Run(ILMethod method, object instance, object[] p)
}
#if UNITY_EDITOR
if(System.Threading.Thread.CurrentThread.ManagedThreadId == AppDomain.UnityMainThreadID)
UnityEngine.Profiler.EndSample();
UnityEngine.Profiling.Profiler.EndSample();
#endif
//ClearStack
return stack.PopFrame(ref frame, esp, mStack);
Expand Down
Binary file modified Unity/Assets/Res/Code/Hotfix.dll.bytes
Binary file not shown.
Binary file modified Unity/Assets/Res/Code/Hotfix.dll.mdb.bytes
Binary file not shown.
Binary file modified Unity/Assets/Res/Code/Hotfix.pdb.bytes
Binary file not shown.
3 changes: 2 additions & 1 deletion Unity/Assets/Scripts/Component/MessageDispatherComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private void Load()

this.opcodeTypes.Add(messageAttribute.Opcode, monoType);
}

#if ILRuntime
Type[] types = DllHelper.GetHotfixTypes();
#else
Expand Down Expand Up @@ -90,7 +91,7 @@ public void Handle(Session session, MessageInfo messageInfo)
{
try
{
ev.Run(session, messageInfo);
ev.Run(session, messageInfo.Message);
}
catch (Exception e)
{
Expand Down
3 changes: 3 additions & 0 deletions Unity/Assets/Scripts/Entity/Game.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Base;

namespace Model
{
Expand All @@ -10,6 +11,8 @@ public static class Game

private static EntityEventManager entityEventManager;

public static TPoller Poller { get; } = new TPoller();

private static Scene scene;

public static Scene Scene
Expand Down
1 change: 1 addition & 0 deletions Unity/Assets/Scripts/Entity/Message/Opcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public static class Opcode
public const ushort G2C_LoginGate = 5;
public const ushort C2G_GetPlayerInfo = 6;
public const ushort G2C_GetPlayerInfo = 7;
public const ushort C2M_Reload = 8;
}
}
12 changes: 6 additions & 6 deletions Unity/Assets/Scripts/Entity/Message/OuterMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Model
{
[Message(1)]
[Message(Opcode.C2R_Login)]
[BsonIgnoreExtraElements]
public class C2R_Login: ARequest
{
Expand All @@ -16,7 +16,7 @@ public class C2R_Login: ARequest
public string Password;
}

[Message(2)]
[Message(Opcode.R2C_Login)]
[BsonIgnoreExtraElements]
public class R2C_Login: AResponse
{
Expand All @@ -27,7 +27,7 @@ public class R2C_Login: AResponse
public long Key { get; set; }
}

[Message(3)]
[Message(Opcode.R2C_ServerLog)]
[BsonIgnoreExtraElements]
public class R2C_ServerLog: AMessage
{
Expand All @@ -44,7 +44,7 @@ public class R2C_ServerLog: AMessage
public string Log { get; set; }
}

[Message(8)]
[Message(Opcode.C2G_LoginGate)]
[BsonIgnoreExtraElements]
public class C2G_LoginGate: ARequest
{
Expand All @@ -57,13 +57,13 @@ public C2G_LoginGate(long key)
}
}

[Message(9)]
[Message(Opcode.G2C_LoginGate)]
[BsonIgnoreExtraElements]
public class G2C_LoginGate: AResponse
{
}

[Message(10)]
[Message(Opcode.C2M_Reload)]
[BsonIgnoreExtraElements]
public class C2M_Reload: ARequest
{
Expand Down
22 changes: 18 additions & 4 deletions Unity/Assets/Scripts/Entity/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

namespace Model
{
public sealed class Session: Entity
public sealed class Session : Entity
{
private static uint RpcId { get; set; }
private readonly NetworkComponent network;
private readonly Dictionary<uint, Action<byte[], int, int>> requestCallback = new Dictionary<uint, Action<byte[], int, int>>();
private readonly AChannel channel;
private bool isRpc;

public Session(NetworkComponent network, AChannel channel): base(EntityType.Session)
public Session(NetworkComponent network, AChannel channel)
{
this.network = network;
this.channel = channel;
Expand All @@ -39,6 +40,7 @@ public ChannelType ChannelType

private async void StartRecv()
{
TimerComponent timerComponent = Game.Scene.GetComponent<TimerComponent>();
while (true)
{
if (this.Id == 0)
Expand All @@ -49,6 +51,11 @@ private async void StartRecv()
byte[] messageBytes;
try
{
if (this.isRpc)
{
this.isRpc = false;
await timerComponent.WaitAsync(0);
}
messageBytes = await channel.Recv();
}
catch (Exception e)
Expand All @@ -63,6 +70,7 @@ private async void StartRecv()
}

ushort opcode = BitConverter.ToUInt16(messageBytes, 0);

try
{
this.Run(opcode, messageBytes);
Expand Down Expand Up @@ -121,6 +129,7 @@ private void RunDecompressedBytes(ushort opcode, uint rpcId, uint rpcFlag, byte[
public Task<Response> Call<Request, Response>(Request request, CancellationToken cancellationToken) where Request : ARequest
where Response : AResponse
{

this.SendMessage(++RpcId, request);

var tcs = new TaskCompletionSource<Response>();
Expand All @@ -135,11 +144,13 @@ public Task<Response> Call<Request, Response>(Request request, CancellationToken
tcs.SetException(new RpcException(response.Error, response.Message));
return;
}
//Log.Debug($"recv: {response.ToJson()}");
this.isRpc = true;
tcs.SetResult(response);
}
catch (Exception e)
{
tcs.SetException(new Exception($"Rpc Error: {typeof (Response).FullName}", e));
tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
}
};

Expand All @@ -166,11 +177,13 @@ public Task<Response> Call<Request, Response>(Request request) where Request : A
tcs.SetException(new RpcException(response.Error, response.Message));
return;
}
//Log.Info($"recv: {response.ToJson()}");
this.isRpc = true;
tcs.SetResult(response);
}
catch (Exception e)
{
tcs.SetException(new Exception($"Rpc Error: {typeof (Response).FullName}", e));
tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
}
};

Expand All @@ -197,6 +210,7 @@ public void Reply<Response>(uint rpcId, Response message) where Response : AResp

private void SendMessage(uint rpcId, object message, bool isCall = true)
{
//Log.Debug($"send: {message.ToJson()}");
ushort opcode = this.network.Owner.GetComponent<MessageDispatherComponent>().GetOpcode(message.GetType());
byte[] opcodeBytes = BitConverter.GetBytes(opcode);
if (!isCall)
Expand Down
18 changes: 18 additions & 0 deletions Unity/Hotfix/Handler/R2C_ServerLogHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Base;
using Model;

namespace Hotfix
{
[MessageHandler(Opcode.R2C_ServerLog)]
public class R2C_ServerLogHandler
{
public void Handle(Session session, R2C_ServerLog message)
{
Log.Debug("111111111111111111111111");
}
}
}
5 changes: 2 additions & 3 deletions Unity/Hotfix/Unity.Hotfix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Component\UnitComponentE.cs" />
<Compile Include="Handler\R2C_ServerLogHandler.cs" />
<Compile Include="UI\UILobby\Component\UILobbyComponentE.cs" />
<Compile Include="UI\UILobby\Event\InitSceneStart_CreateLobbyUI.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -71,9 +72,7 @@
<Name>Unity.Plugins</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Handler\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>"$(ProjectDir)..\CSharp60Support\Roslyn\pdb2mdb.exe" $(TargetFileName)
Expand Down
24 changes: 10 additions & 14 deletions Unity/ProjectSettings/GraphicsSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
--- !u!30 &1
GraphicsSettings:
m_ObjectHideFlags: 0
serializedVersion: 7
serializedVersion: 9
m_Deferred:
m_Mode: 1
m_Shader: {fileID: 69, guid: 0000000000000000f000000000000000, type: 0}
Expand Down Expand Up @@ -38,22 +38,18 @@ GraphicsSettings:
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
m_ShaderSettings_Tier1:
m_TierSettings_Tier1:
renderingPath: 1
useCascadedShadowMaps: 1
standardShaderQuality: 2
useReflectionProbeBoxProjection: 1
useReflectionProbeBlending: 1
m_ShaderSettings_Tier2:
m_TierSettings_Tier2:
renderingPath: 1
useCascadedShadowMaps: 1
standardShaderQuality: 2
useReflectionProbeBoxProjection: 1
useReflectionProbeBlending: 1
m_ShaderSettings_Tier3:
m_TierSettings_Tier3:
renderingPath: 1
useCascadedShadowMaps: 1
standardShaderQuality: 2
useReflectionProbeBoxProjection: 1
useReflectionProbeBlending: 1
m_BuildTargetShaderSettings: []
m_DefaultRenderingPath: 1
m_DefaultMobileRenderingPath: 1
m_TierSettings: []
m_LightmapStripping: 0
m_FogStripping: 0
m_LightmapKeepPlain: 1
Expand Down
4 changes: 4 additions & 0 deletions Unity/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,12 @@ PlayerSettings:
intPropertyNames:
- Android::ScriptingBackend
- Standalone::ScriptingBackend
- iOS::Architecture
- iOS::ScriptingBackend
Android::ScriptingBackend: 0
Standalone::ScriptingBackend: 0
iOS::Architecture: 2
iOS::ScriptingBackend: 1
boolPropertyNames:
- Android::VR::enable
- Metro::VR::enable
Expand Down
Loading

0 comments on commit e17b4ab

Please sign in to comment.