//======================================================================================== public override void SocketClientResponseCallBack(byte[] RecieveMessageBytes, int RecieveCount) { string BaseMessageString; /* * if (RecieveMessageBytes[2] == 254) * { * BaseMessageString = Encoding.Unicode.GetString(RecieveMessageBytes, 3, RecieveCount - 3); * } * else * { * BaseMessageString = Encoding.ASCII.GetString(RecieveMessageBytes, 3, RecieveCount - 3); * } */ BaseMessageString = Encoding.UTF8.GetString(RecieveMessageBytes, 3, RecieveCount - 3); string MyMobileID = BaseMessageString.Substring(BaseMessageString.IndexOf("#") + 1, 15); string MyLockID = BaseMessageString.Substring(BaseMessageString.IndexOf("-") + 1, 15); string MyCommandMessage = BaseMessageString.Substring(0, BaseMessageString.IndexOf("#")); //string MyTimeMarkerStr = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); string MyTimeMarkerStr = string.Format("{0:HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); string ClientSocketEndPointStr = MyMobileServerClientAPI.MyTcpClient.Client.LocalEndPoint.ToString(); //--找移动端通道-------------------------------------------------------------------------------- /* * LGJAsynchSocketService.LockServerLib.FindMobileChannel MyBindedMobileChannel = new LGJAsynchSocketService.LockServerLib.FindMobileChannel(MyMobileID); * SocketServiceReadWriteChannel MyReadWriteSocketChannel; * * * try * { * MyReadWriteSocketChannel = this.MyManagerSocketLoginUser.MyLoginUserList.Find(new Predicate<LoginUser>(MyBindedMobileChannel.SelectMobileChannel)).MyReadWriteSocketChannel; * } * catch * { * MyReadWriteSocketChannel = null; * } */ FindMobileChannel MyFindMobileChannel = new FindMobileChannel(MyLockID); LoginUser MyLoginUser = this.MyManagerLoginMobileUser.MyLoginUserList.Find(new Predicate <LoginUser>(MyFindMobileChannel.BindedMobileChannel)); if (MyLoginUser == null) { //不存在或者不在线 this.DisplayResultInfor(1, string.Format("[{0}]所绑定的移动端通道不在线", MyLockID)); //this.DisplayResultInfor(2, string.Format(MyTimeMarkerStr + "|<-{0}", BaseMessageString)); this.DisplayResultInfor(1, string.Format(MyTimeMarkerStr + "[<-{0}]{1}", ClientSocketEndPointStr, BaseMessageString)); return; } else { //this.DisplayResultInfor(2, string.Format(MyTimeMarkerStr + "|<-{0}", BaseMessageString)); this.DisplayResultInfor(2, string.Format(MyTimeMarkerStr + "[<-{0}]{1}", ClientSocketEndPointStr, BaseMessageString)); } //下一步处理 SocketServiceReadWriteChannel MyMobileSocketChannel = MyLoginUser.MyReadWriteSocketChannel; //--再转发出去----------------------------------------------------------------------------------- if (MyMobileSocketChannel != null) { byte[] MySendMessageBytes = new byte[RecieveCount]; for (int i = 0; i < RecieveCount; i++) { MySendMessageBytes[i] = RecieveMessageBytes[i]; } StartAsynchSendMessage(MyMobileSocketChannel, MySendMessageBytes); //MyTimeMarkerStr = string.Format("{0:MM-dd HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); //MyTimeMarkerStr = string.Format("{0:HH:mm:ss}", DateTime.Now); //this.DisplayResultInfor(1, string.Format(MyTimeMarkerStr + " 接收云智能总线[{0}]响应命令:{1}", MyLockID, MyCommandMessage)); } else { this.DisplayResultInfor(1, string.Format(MyTimeMarkerStr + "[{0}]转发命令{1}出错!", MyLockID, MyCommandMessage)); } }
public void CommandDefineDispatchEx(LoginUser MeLoginUser, int InputRecieveCount) { /* * //byte[] MyReadBuffer = MeLoginUser.MyReadWriteSocketChannel.MyReadBuffers; * //string MessageString = null; * // MessageString = Encoding.UTF8.GetString(MyReadBuffer, 0, InputRecieveCount); * // string MyTimeMarker = string.Format("{0:HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); * // DisplayResultInfor(2, string.Format(MyTimeMarker + "[{0}]{1}]", MeLoginUser.MyReadWriteSocketChannel.MyTCPClient.Client.RemoteEndPoint, MessageString)); * // DisplayResultInfor(1, string.Format("[{0}]{1}", MeLoginUser.MyReadWriteSocketChannel.MyTCPClient.Client.RemoteEndPoint, MessageString)); * //return; */ if (InputRecieveCount < 5) { return; } byte[] MyReadBuffers = MeLoginUser.MyReadWriteSocketChannel.MyReadBuffers; SocketServiceReadWriteChannel InputSocketServiceReadWriteChannel = MeLoginUser.MyReadWriteSocketChannel; byte MessageFlagID = MyReadBuffers[1]; string BaseMessageString = null; BaseMessageString = Encoding.UTF8.GetString(MyReadBuffers, 3, InputRecieveCount - 3); //string MyTimeMarkerStr = string.Format("{0:MM-dd HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); string MyTimeMarkerStr = string.Format("{0:HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); DisplayResultInfor(2, string.Format(MyTimeMarkerStr + "[{0}]{1}", InputSocketServiceReadWriteChannel.MyTCPClient.Client.RemoteEndPoint, BaseMessageString)); if (MessageFlagID == 0 || MessageFlagID == 16)//第二个字节为0、16:异步Socket客户端;1:同步Socket客户端(非Web): { string CommandMessageStr = BaseMessageString.Substring(0, BaseMessageString.IndexOf("#")); switch (CommandMessageStr) { case "login": MobileAppServerLib.MobileLoginManager MyMobileLoginManager; MyMobileLoginManager = new MobileAppServerLib.MobileLoginManager(this, MeLoginUser, InputRecieveCount); MyMobileLoginManager.CompleteCommand(); break; case "ping": //DirectResponsePing(InputSocketServiceReadWriteChannel); DirectResponsePingEx(MeLoginUser); break; case "close": DirectCloseLoginUser(MeLoginUser); break; default: //直接转发给云锁服务器! byte[] MySendMessageBytes = new byte[InputRecieveCount]; for (int i = 0; i < InputRecieveCount; i++) { MySendMessageBytes[i] = MyReadBuffers[i]; } MyMobileServerClientAPI.SynchSendCommand(MySendMessageBytes); break; } } else { if (MessageFlagID == 100)//第二个字节为100:IM { MessageRountToEndpoit(MyReadBuffers, InputRecieveCount); } else//同步Socket客户端 { string CommandMessageStr = BaseMessageString.Substring(0, BaseMessageString.IndexOf("#")); int IndexStart = BaseMessageString.IndexOf("#") + 1; switch (CommandMessageStr) { case "authtication": string UserCertID = BaseMessageString.Substring(IndexStart, BaseMessageString.LastIndexOf("#") - IndexStart); FromDBAuthtication(UserCertID, InputSocketServiceReadWriteChannel); //通过客户LoginID号获得通道 break; case "getchannel": string CustomerID = BaseMessageString.Substring(IndexStart, BaseMessageString.LastIndexOf("#") - IndexStart); FromDBGetChannel(CustomerID, InputSocketServiceReadWriteChannel); //通过客户ID号获得通道[为TOMCAT定制] break; case "mobilelogin": string UserPassCertID = BaseMessageString.Substring(IndexStart, BaseMessageString.LastIndexOf("#") - IndexStart); MobileLoginAuthtication(UserPassCertID, InputSocketServiceReadWriteChannel); break; case "managerlogin": string managerPassCertID = BaseMessageString.Substring(IndexStart, BaseMessageString.LastIndexOf("#") - IndexStart); ManagerLoginAuthtication(managerPassCertID, InputSocketServiceReadWriteChannel); break; case "lockstatus": string LockStatusIDStr = BaseMessageString.Substring(IndexStart, BaseMessageString.LastIndexOf("#") - IndexStart); LockStatusManagerment(LockStatusIDStr, InputSocketServiceReadWriteChannel); break; case "customercreate": string customercreateStr = BaseMessageString.Substring(IndexStart, BaseMessageString.LastIndexOf("#") - IndexStart); CustomerCreateManagerment(customercreateStr, InputSocketServiceReadWriteChannel); break; case "getimage": string LockSnapID = BaseMessageString.Substring(IndexStart, BaseMessageString.LastIndexOf("#") - IndexStart); FromDBGetImage(LockSnapID, InputSocketServiceReadWriteChannel); break; case "getkeylist": break; case "getopenlist": break; case "getsnaplist": break; default: MobileAppServerLib.SynchTcpClientChannel MySynchTcpClientChannel; MySynchTcpClientChannel = new MobileAppServerLib.SynchTcpClientChannel(this, MeLoginUser, InputRecieveCount); MySynchTcpClientChannel.CompleteCommand(); break; } } } }
protected virtual void AsynchReadClientCallback(IAsyncResult InAsynchResult) { LoginUser MyLoginUser = (LoginUser)InAsynchResult.AsyncState; try { bool MyConnected = MyLoginUser.MyReadWriteSocketChannel.MyTCPClient.Connected; if (MyConnected)//-连接状态 { uint MyRecieveCount = (uint)MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.EndRead(InAsynchResult); int MyRecieveAvailable = MyLoginUser.MyReadWriteSocketChannel.MyTCPClient.Available; if (MyRecieveCount != 0) { //==正常Transmission Data状态====================================== ProcessMessageEntry(MyLoginUser, MyRecieveCount, MyRecieveAvailable);//子类实现具体方法 /* * LockServerLib.LongFileReceiveProc MyLongFileReceiveProc = MyManagerSocketLoginUser.FindLongFileReceiveProcList(ref MyReadWriteObject.MyTCPClient); * if (MyLongFileReceiveProc == null) * { * byte MessageTypeFirstFlag = MyReadWriteObject.MyReadBuffers[2]; * CommandDefineDispatch(MyReadWriteObject, MyReadWriteObject.MyReadBuffers, (int)MessageTypeFirstFlag, (int)MyRecieveCount);//命令状态 * } * else * { * if (MyRecieveAvailable==0 && MyLongFileReceiveProc.LoopReadCount == 0) * { * byte MessageTypeFirstFlag = MyReadWriteObject.MyReadBuffers[2]; * CommandDefineDispatch(MyReadWriteObject, MyReadWriteObject.MyReadBuffers, (int)MessageTypeFirstFlag, (int)MyRecieveCount);//命令、短文件传输状态 * } * else * { * MyLongFileReceiveProc.MyReadWriteChannel = MyReadWriteObject; * MyLongFileReceiveProc.MyAsynchSocketServiceBaseFrame = this; * LongFileReceiveLoopProcess(MyLongFileReceiveProc, MyReadWriteObject.MyReadBuffers, MyRecieveCount);//长文件传输状态 * * } * } */ //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- if (IsExit == false) { MyLoginUser.MyReadWriteSocketChannel.InitReadArray(); MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.BeginRead(MyLoginUser.MyReadWriteSocketChannel.MyReadBuffers, 0, MyLoginUser.MyReadWriteSocketChannel.MyReadBuffers.Length, AsynchReadClientCallback, MyLoginUser);//继续循环读取 } //=================================================================== } else { MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.EndRead(InAsynchResult);//后来完善加入 //--零字节调用-- //string MyTimeMarker = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString(); //string MyTimeMarker = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); //DisplayResultInfor(1, string.Format(MyTimeMarker + "客户端[{0}]已主动断开连接(11)", MyLoginUser.MyReadWriteSocketChannel.MyRemoteEndPointStr)); string MyTimeMarker = string.Format("{0:HH:mm:ss}", DateTime.Now); DisplayResultInfor(1, string.Format(MyTimeMarker + "[{0}]零字节调用已断开(13)", MyLoginUser.MyReadWriteSocketChannel.MyRemoteEndPointStr)); this.MyManagerSocketLoginUser.MyLoginUserList.Remove(MyLoginUser);//线程安全有待考虑 ClientCloseEventCallback(MyLoginUser.MobileID, MyLoginUser.LockID); DisplayResultInfor(4, ""); } } else //非连接状态:由客户端Socket各种因素如掉线、断网、非正常关机、无认证取消通道引起的Socket通讯链路不通! { MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.EndRead(InAsynchResult);//后来完善加入 //--> + "[" + DateTime.Now.Ticks.ToString() + "]" //string MyTimeMarker = DateTime.Now.ToString() + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); //string MyTimeMarker = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); string MyTimeMarker = string.Format("{0:HH:mm:ss}", DateTime.Now); DisplayResultInfor(1, string.Format(MyTimeMarker + "[{0}]已非正常断开连接(12)", MyLoginUser.SocketInfor)); //MyManagerSocketLoginUser.CRUDLoginUserList(ref MyReadWriteObject.MyTCPClient, 2); ClientCloseEventCallback(MyLoginUser.MobileID, MyLoginUser.LockID); this.MyManagerSocketLoginUser.MyLoginUserList.Remove(MyLoginUser); DisplayResultInfor(4, ""); } } catch (Exception ExceptionInfor) { //+ "[" + DateTime.Now.Ticks.ToString() + "]" //string MyTimeMarker = DateTime.Now.ToString() + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond) ; //string MyTimeMarker = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); string MyTimeMarker = string.Format("{0:HH:mm:ss}", DateTime.Now); if (ExceptionInfor.Message.IndexOf("无法从传输连接中读取数据") > -1) { MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.EndRead(InAsynchResult);//后来完善加入 DisplayResultInfor(1, string.Format(MyTimeMarker + "[{0}]已主动断开连接(10)", MyLoginUser.SocketInfor)); ClientCloseEventCallback(MyLoginUser.MobileID, MyLoginUser.LockID); this.MyManagerSocketLoginUser.MyLoginUserList.Remove(MyLoginUser);// DisplayResultInfor(4, ""); return; } //------------------------------------------------------------------------------- if (ExceptionInfor.Message.IndexOf("无法访问已释放的对象") > -1) { MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.EndRead(InAsynchResult);//后来完善加入 DisplayResultInfor(1, string.Format(MyTimeMarker + "服务器已自动断开客户端连接[已释放的对象:Client-SocketChannel]")); return; } else { //------继续读取:细节完善--------------------------------------------------------- if (IsExit == false) { MyLoginUser.MyReadWriteSocketChannel.InitReadArray(); MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.BeginRead(MyLoginUser.MyReadWriteSocketChannel.MyReadBuffers, 0, MyLoginUser.MyReadWriteSocketChannel.MyReadBuffers.Length, AsynchReadClientCallback, MyLoginUser);//继续循环读取 } DisplayResultInfor(1, string.Format(MyTimeMarker + "传输解析数据错误:[{0}]", ExceptionInfor)); } //------------------------------------------------------------------------------------------------------ } finally { //MyLoginUser.MyReadWriteSocketChannel.MyNetWorkStream.EndRead(InAsynchResult); } }
protected virtual void ProcessMessageEntry(LoginUser MyLoginUser, uint MyRecieveCount, int MyRecieveAvailable) { //虚函数,子类必须实现: }
private void AcceptTcpClientConnectCallback(IAsyncResult InputAsynchResult) { try { //这个函数在另外一个线程中执行 MyEventWaitAllDone.Set(); //侦听线程继续等待写一个客户端连接 TcpListener MyListener = (TcpListener)InputAsynchResult.AsyncState; TcpClient MyTCPClient = MyListener.EndAcceptTcpClient(InputAsynchResult); MyTCPClient.Client.IOControl(IOControlCode.KeepAliveValues, SetkeepalivInPARA(), null);// here set keep-alive //--------------------------------------------------------------------------------------------------------------------- //if (AroundDisplay == 1) ///{ //MyRefreshPrimeNotifyIconCallBackCallback.Invoke(MyTimeMarker.Substring(0,MyTimeMarker.IndexOf("[")) + "已接受客户连接:" + MyTCPClient.Client.RemoteEndPoint); //DisplayResultInfor(1, string.Format(MyTimeMarker + "已接受客户端[{0}]连接\r\n", MyTCPClient.Client.RemoteEndPoint)); //} //----------------------------------------------------------------------------------------------------------------------- //----回显信息-------------------------------------------------------------------------------------------------------- //string MyTimeMarker = DateTime.Now.ToString() + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond) + "[" + DateTime.Now.Ticks.ToString() + "]"; //string MyTimeMarker = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + ":" + string.Format("{0:D3}", DateTime.Now.Millisecond); string MyTimeMarker = string.Format("{0:HH:mm:ss}", DateTime.Now); //DisplayResultInfor(1, string.Format(MyTimeMarker + "已接受客户端[{0}]连接", MyTCPClient.Client.RemoteEndPoint)); if (MySocketServiceTypeID == SocketServiceTypeID.lockServer) { DisplayResultInfor(1, string.Format(MyTimeMarker + " I[{0}]连接", MyTCPClient.Client.RemoteEndPoint)); } if (MySocketServiceTypeID == SocketServiceTypeID.MobileAppServer) { DisplayResultInfor(1, string.Format(MyTimeMarker + " M[{0}]连接", MyTCPClient.Client.RemoteEndPoint)); } //---添加通道【涉及递归、无锁化设计、Proactor模式】--------------------------------------------------------------------------------------------------------- SocketServiceReadWriteChannel MyReadWriteChannel = new SocketServiceReadWriteChannel(MyTCPClient); //MyReadWriteChannel.MyNetWorkStream.BeginRead(MyReadWriteChannel.MyReadBuffers, 0, MyReadWriteChannel.MyReadBuffers.Length, AsynchReadClientCallback, MyReadWriteChannel); //MyManagerSocketLoginUser.CRUDLoginUserList(MyReadWriteChannel, 0); //LGJMessageEntity MyLGJMessageEntity = new LGJMessageEntity("create", 0, MyReadWriteChannel); //MyMessageEntityManager.AddMessageEntity(MyLGJMessageEntity); //增加会话状态记录 LoginID++; LoginUser MyLoginUser = new LoginUser(LoginID, ref MyReadWriteChannel); this.MyManagerSocketLoginUser.MyLoginUserList.Add(MyLoginUser); MyReadWriteChannel.MyNetWorkStream.BeginRead(MyReadWriteChannel.MyReadBuffers, 0, MyReadWriteChannel.MyReadBuffers.Length, AsynchReadClientCallback, MyLoginUser); //MyEventWaitAllDone.Set(); //新改进!继续等待写一个客户端连接 this.DisplayResultInfor(4, ""); //MySocketServiceList.Add(MyReadWriteObject); //CommandFromInOut = 1; //CommandDefine(MyReadWriteObject, "#login!"); //MySocketServiceList.Add(MyReadWriteObject); //CRUDLoginUserList(ref MyTCPClient, 0); //ClientListComboBox.Invoke(setComboBoxCallback, MyTCPClient.Client.RemoteEndPoint.ToString()); //OnReceiveCommand(new CommandMessageEventArgs(MyReadWriteObject,"#login!","",0)); } catch (Exception ExceptionInfor) { //DisplayResultInfor(1, "侦听器关闭[停止客户连接服务]" + "\r\n"); DisplayResultInfor(1, string.Format("服务器侦听错误[{0}][10030]", ExceptionInfor.Message)); //return; } }