1.解决上一次留下的问题:
log和reg的时候也有session,输出看一下这两个session是同一个不:
实测结果reg log accOnline中的session都是同一个对象,但是getAccid时候的session就是另一个了。
测试结果,说明在LogicHandler的process中就已经是不同的
public void process(Session session, SocketModel model){try{switch (model.Type){case 0:Console.WriteLine("process-log:"+session.idTime);LoginHandler.getInstance().process(session, model);break;case 1:Console.WriteLine("process-map:"+session.idTime);MapHandler.getInstance().process(session, model);break;case 2:Console.WriteLine("process-user:"+session.idTime);UserHandler.getInstance().process(session, model);break;}}catch (Exception ex){//MyLog.form.textAdd(ex.Message);Console.WriteLine(ex.Message);}}
再继续往上翻一层:
public static void readMessage(Session session, byte[] bytes)//读取数据包
{try{ByteArray byteArray = new ByteArray(bytes);int num1 = byteArray.ReadInt();int num2 = byteArray.ReadInt();int num3 = byteArray.ReadInt();int length = byteArray.ReadInt();string str = (string)null;if (length > 0)str = byteArray.ReadUTFBytes((uint)length);LogicHandler.getInstance().process(session, new SocketModel(){Type = num1,Area = num2,Command = num3,Message = str});Console.WriteLine("readMessage"+ num1+session.idTime+"!!!!!!!!!!!!!!!!!!!!!!!!!");}catch{}
}
结果很明显,在readMessage时就已经出现严重分化
再往上翻一层ReceiveCallBack
private static void ReceiveCallBack(IAsyncResult ar)
{Session asyncState = (Session)ar.AsyncState;try{int count = asyncState.socket.EndReceive(ar);if (count == 0){//MyLog.form.textAdd("有客户端断开连接");Console.WriteLine("有客户端断开连接");LogicHandler.getInstance().sessionClose(asyncState);return;}byte[] numArray = new byte[count];Buffer.BlockCopy((Array)asyncState.message, 0, (Array)numArray, 0, count);ServerStart.readMessage(asyncState, numArray);}catch (SocketException ex){LogicHandler.getInstance().sessionClose(asyncState);asyncState.socket.Close();return;}asyncState.socket.BeginReceive(asyncState.message, 0, asyncState.message.Length, SocketFlags.None, new AsyncCallback(ServerStart.ReceiveCallBack), (object)asyncState);
}
测试发现,只要点击了panel中的注册按钮,session就被更新了。
再往上翻一层
private static void AcceptCallBack(IAsyncResult ar)
{//MyLog.form.textAdd("有客户端连接");Console.WriteLine("有客户端连接");try{Socket asyncState = (Socket)ar.AsyncState;Socket socket = asyncState.EndAccept(ar);Session session = new Session();session.socket = socket;LogicHandler.getInstance().sessionOpen(session);socket.BeginReceive(session.message, 0, session.message.Length, SocketFlags.None, new AsyncCallback(ServerStart.ReceiveCallBack), (object)session);asyncState.BeginAccept(new AsyncCallback(ServerStart.AcceptCallBack), (object)asyncState);}catch{}
}