Skip to content

Commit dd36d4e

Browse files
committed
optimize network
1 parent b0f996c commit dd36d4e

File tree

2 files changed

+50
-63
lines changed

2 files changed

+50
-63
lines changed

CrazyCar/Assets/Scripts/Game/NetworkController.cs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,54 @@ private void Awake() {
4242

4343
private void FixedUpdate() {
4444
// KCP 开了线程所以只能把 RespondAction放进主线程
45-
if (this.GetSystem<INetworkSystem>().PlayerCreateMsgs.Count > 0) {
46-
lock (this.GetSystem<INetworkSystem>().MsgLock) {
45+
lock (this.GetSystem<INetworkSystem>().MsgLock) {
46+
if (this.GetSystem<INetworkSystem>().PlayerCreateMsgs.Count > 0) {
4747
this.SendCommand(new MakeNewPlayerCommand(this.GetSystem<INetworkSystem>().PlayerCreateMsgs.Peek()));
4848
this.GetSystem<INetworkSystem>().PlayerCreateMsgs.Dequeue();
4949
}
50-
}
5150

52-
if (this.GetSystem<INetworkSystem>().PlayerStateMsgs.Count > 0) {
53-
lock (this.GetSystem<INetworkSystem>().MsgLock) {
51+
if (this.GetSystem<INetworkSystem>().PlayerStateMsgs.Count > 0) {
5452
this.GetSystem<IPlayerManagerSystem>().RespondStateAction(
5553
this.GetSystem<INetworkSystem>().PlayerStateMsgs.Peek());
5654
this.GetSystem<INetworkSystem>().PlayerStateMsgs.Dequeue();
5755
}
58-
}
5956

60-
if (this.GetSystem<INetworkSystem>().PlayerOperatMsgs.Count > 0) {
61-
lock (this.GetSystem<INetworkSystem>().MsgLock) {
57+
if (this.GetSystem<INetworkSystem>().PlayerOperatMsgs.Count > 0) {
6258
this.GetSystem<IPlayerManagerSystem>().RespondOperatAction(
6359
this.GetSystem<INetworkSystem>().PlayerOperatMsgs.Peek());
6460
this.GetSystem<INetworkSystem>().PlayerOperatMsgs.Dequeue();
6561
}
66-
}
6762

68-
if (this.GetSystem<INetworkSystem>().PlayerCompleteMsgs.Count > 0) {
69-
lock (this.GetSystem<INetworkSystem>().MsgLock) {
63+
if (this.GetSystem<INetworkSystem>().PlayerCompleteMsgs.Count > 0) {
7064
this.SendCommand(new UpdateMatchResultUICommand(this.GetSystem<INetworkSystem>().PlayerCompleteMsgs.Peek()));
7165
this.GetSystem<INetworkSystem>().PlayerCompleteMsgs.Dequeue();
7266
}
73-
}
74-
75-
if (this.GetSystem<INetworkSystem>().OnMatchRoomCreateMsg != null) {
76-
this.GetSystem<IMatchRoomSystem>().OnCreateMsg(this.GetSystem<INetworkSystem>().OnMatchRoomCreateMsg);
77-
this.GetSystem<INetworkSystem>().OnMatchRoomCreateMsg = null;
78-
}
79-
80-
if (this.GetSystem<INetworkSystem>().OnMatchRoomJoinMsg != null) {
81-
this.GetSystem<IMatchRoomSystem>().OnJoinMsg(this.GetSystem<INetworkSystem>().OnMatchRoomJoinMsg);
82-
this.GetSystem<INetworkSystem>().OnMatchRoomJoinMsg = null;
83-
}
84-
85-
if (this.GetSystem<INetworkSystem>().OnMatchRoomExitMsg != null) {
86-
this.GetSystem<IMatchRoomSystem>().OnExitMsg(this.GetSystem<INetworkSystem>().OnMatchRoomExitMsg);
87-
this.GetSystem<INetworkSystem>().OnMatchRoomExitMsg = null;
88-
}
89-
90-
if (this.GetSystem<INetworkSystem>().OnMatchRoomStartMsg != null) {
91-
this.GetSystem<IMatchRoomSystem>().OnStartMsg(this.GetSystem<INetworkSystem>().OnMatchRoomStartMsg);
92-
this.GetSystem<INetworkSystem>().OnMatchRoomStartMsg = null;
93-
}
94-
95-
if (this.GetSystem<INetworkSystem>().OnMatchRoomStatusMsg != null) {
96-
this.GetSystem<IMatchRoomSystem>().OnStatusMsg(this.GetSystem<INetworkSystem>().OnMatchRoomStatusMsg);
97-
this.GetSystem<INetworkSystem>().OnMatchRoomStatusMsg = null;
98-
}
67+
68+
if (this.GetSystem<INetworkSystem>().OnMatchRoomCreateMsg != null) {
69+
this.GetSystem<IMatchRoomSystem>().OnCreateMsg(this.GetSystem<INetworkSystem>().OnMatchRoomCreateMsg);
70+
this.GetSystem<INetworkSystem>().OnMatchRoomCreateMsg = null;
71+
}
72+
73+
if (this.GetSystem<INetworkSystem>().OnMatchRoomJoinMsg != null) {
74+
this.GetSystem<IMatchRoomSystem>().OnJoinMsg(this.GetSystem<INetworkSystem>().OnMatchRoomJoinMsg);
75+
this.GetSystem<INetworkSystem>().OnMatchRoomJoinMsg = null;
76+
}
77+
78+
if (this.GetSystem<INetworkSystem>().OnMatchRoomExitMsg != null) {
79+
this.GetSystem<IMatchRoomSystem>().OnExitMsg(this.GetSystem<INetworkSystem>().OnMatchRoomExitMsg);
80+
this.GetSystem<INetworkSystem>().OnMatchRoomExitMsg = null;
81+
}
82+
83+
if (this.GetSystem<INetworkSystem>().OnMatchRoomStartMsg != null) {
84+
this.GetSystem<IMatchRoomSystem>().OnStartMsg(this.GetSystem<INetworkSystem>().OnMatchRoomStartMsg);
85+
this.GetSystem<INetworkSystem>().OnMatchRoomStartMsg = null;
86+
}
87+
88+
if (this.GetSystem<INetworkSystem>().OnMatchRoomStatusMsg != null) {
89+
this.GetSystem<IMatchRoomSystem>().OnStatusMsg(this.GetSystem<INetworkSystem>().OnMatchRoomStatusMsg);
90+
this.GetSystem<INetworkSystem>().OnMatchRoomStatusMsg = null;
91+
}
92+
}
9993
}
10094

10195
public IArchitecture GetArchitecture() {

CrazyCar/Assets/Scripts/System/NetworkSystem.cs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,37 +147,30 @@ public void SendMsgToServer(string msg) {
147147

148148
public void RespondAction(JsonData recJD){
149149
MsgType msgType = (MsgType)(int)recJD["msg_type"];
150-
151-
if (msgType == MsgType.CreatePlayer) {
152-
playerCreateMsg = this.GetSystem<IDataParseSystem>().ParsePlayerCreateMsg(recJD);
153-
lock (MsgLock) {
150+
lock (MsgLock) {
151+
if (msgType == MsgType.CreatePlayer) {
152+
playerCreateMsg = this.GetSystem<IDataParseSystem>().ParsePlayerCreateMsg(recJD);
154153
PlayerCreateMsgs.Enqueue(playerCreateMsg);
155-
}
156-
} else if(msgType == MsgType.PlayerState) {
157-
playerStateMsg = this.GetSystem<IDataParseSystem>().ParsePlayerStateMsg(recJD);
158-
lock (MsgLock) {
154+
} else if (msgType == MsgType.PlayerState) {
155+
playerStateMsg = this.GetSystem<IDataParseSystem>().ParsePlayerStateMsg(recJD);
159156
PlayerStateMsgs.Enqueue(playerStateMsg);
160-
}
161-
} else if (msgType == MsgType.PlayerOperat){
162-
playerOperatMsg = this.GetSystem<IDataParseSystem>().ParsePlayerOperatMsg(recJD);
163-
lock (MsgLock) {
157+
} else if (msgType == MsgType.PlayerOperat) {
158+
playerOperatMsg = this.GetSystem<IDataParseSystem>().ParsePlayerOperatMsg(recJD);
164159
PlayerOperatMsgs.Enqueue(playerOperatMsg);
165-
}
166-
} else if (msgType == MsgType.PlayerCompleteGame) {
167-
playerCompleteMsg = this.GetSystem<IDataParseSystem>().ParsePlayerCompleteMsg(recJD);
168-
lock (MsgLock) {
160+
} else if (msgType == MsgType.PlayerCompleteGame) {
161+
playerCompleteMsg = this.GetSystem<IDataParseSystem>().ParsePlayerCompleteMsg(recJD);
169162
PlayerCompleteMsgs.Enqueue(playerCompleteMsg);
163+
} else if (msgType == MsgType.MatchRoomCreate) {
164+
OnMatchRoomCreateMsg = recJD;
165+
} else if (msgType == MsgType.MatchRoomJoin) {
166+
OnMatchRoomJoinMsg = recJD;
167+
} else if (msgType == MsgType.MatchRoomStart) {
168+
OnMatchRoomStartMsg = recJD;
169+
} else if (msgType == MsgType.MatchRoomStatus) {
170+
OnMatchRoomStatusMsg = recJD;
171+
} else if (msgType == MsgType.MatchRoomExit) {
172+
OnMatchRoomExitMsg = recJD;
170173
}
171-
} else if (msgType == MsgType.MatchRoomCreate) {
172-
OnMatchRoomCreateMsg = recJD;
173-
} else if (msgType == MsgType.MatchRoomJoin) {
174-
OnMatchRoomJoinMsg = recJD;
175-
} else if (msgType == MsgType.MatchRoomStart) {
176-
OnMatchRoomStartMsg = recJD;
177-
} else if (msgType == MsgType.MatchRoomStatus) {
178-
OnMatchRoomStatusMsg = recJD;
179-
} else if (msgType == MsgType.MatchRoomExit) {
180-
OnMatchRoomExitMsg = recJD;
181174
}
182175
}
183176

0 commit comments

Comments
 (0)