Skip to content

Commit a8fcbb1

Browse files
committed
SocketClient线程安全问题
1 parent efb0052 commit a8fcbb1

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

Assets/LuaFramework/Scripts/Manager/NetworkManager.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
namespace LuaFramework {
88
public class NetworkManager : Manager {
99
private SocketClient socket;
10-
static Queue<KeyValuePair<int, ByteBuffer>> sEvents = new Queue<KeyValuePair<int, ByteBuffer>>();
10+
static readonly object m_lockObject = new object();
11+
static Queue<KeyValuePair<int, ByteBuffer>> mEvents = new Queue<KeyValuePair<int, ByteBuffer>>();
1112

1213
SocketClient SocketClient {
1314
get {
@@ -42,16 +43,18 @@ public object[] CallMethod(string func, params object[] args) {
4243

4344
///------------------------------------------------------------------------------------
4445
public static void AddEvent(int _event, ByteBuffer data) {
45-
sEvents.Enqueue(new KeyValuePair<int, ByteBuffer>(_event, data));
46+
lock (m_lockObject) {
47+
mEvents.Enqueue(new KeyValuePair<int, ByteBuffer>(_event, data));
48+
}
4649
}
4750

4851
/// <summary>
4952
/// 交给Command,这里不想关心发给谁。
5053
/// </summary>
5154
void Update() {
52-
if (sEvents.Count > 0) {
53-
while (sEvents.Count > 0) {
54-
KeyValuePair<int, ByteBuffer> _event = sEvents.Dequeue();
55+
if (mEvents.Count > 0) {
56+
while (mEvents.Count > 0) {
57+
KeyValuePair<int, ByteBuffer> _event = mEvents.Dequeue();
5558
facade.SendMessageCommand(NotiConst.DISPATCH_MESSAGE, _event);
5659
}
5760
}

Assets/LuaFramework/Scripts/Manager/ThreadManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ThreadManager : Manager {
3131
private Stopwatch sw = new Stopwatch();
3232
private string currDownFile = string.Empty;
3333

34-
static readonly object m_lockObj = new object();
34+
static readonly object m_lockObject = new object();
3535
static Queue<ThreadEvent> events = new Queue<ThreadEvent>();
3636

3737
delegate void ThreadSyncEvent(NotiData data);
@@ -51,7 +51,7 @@ void Start() {
5151
/// 添加到事件队列
5252
/// </summary>
5353
public void AddEvent(ThreadEvent ev, Action<NotiData> func) {
54-
lock (m_lockObj) {
54+
lock (m_lockObject) {
5555
this.func = func;
5656
events.Enqueue(ev);
5757
}
@@ -69,7 +69,7 @@ private void OnSyncEvent(NotiData data) {
6969
// Update is called once per frame
7070
void OnUpdate() {
7171
while (true) {
72-
lock (m_lockObj) {
72+
lock (m_lockObject) {
7373
if (events.Count > 0) {
7474
ThreadEvent e = events.Dequeue();
7575
try {

Assets/LuaFramework/Scripts/Network/SocketClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ public enum DisType {
1313
}
1414

1515
public class SocketClient {
16+
public static bool loggedIn = false;
17+
1618
private TcpClient client = null;
1719
private NetworkStream outStream = null;
1820
private MemoryStream memStream;
1921
private BinaryReader reader;
2022

2123
private const int MAX_READ = 8192;
2224
private byte[] byteBuffer = new byte[MAX_READ];
23-
public static bool loggedIn = false;
2425

2526
// Use this for initialization
2627
public SocketClient() {
@@ -109,7 +110,6 @@ void OnRead(IAsyncResult asr) {
109110
client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null);
110111
}
111112
} catch (Exception ex) {
112-
//PrintBytes();
113113
OnDisconnected(DisType.Exception, ex.Message);
114114
}
115115
}

0 commit comments

Comments
 (0)