/// <summary> /// 登出遠端伺服器 /// </summary> public override void Logout() { StockDecoder.StockProc -= StockClient_onStock; StockDecoder.TimerProc -= StockClient_onTimer; StockDecoder.TimerProc -= GetTradeDateFromLogin; McpDecoder.McpPacketProc -= StockClient_onMcpPacket; try { if (__cSession != null) { __cSession.CloseProc -= StockClient_onClose; if (__cSession.Connected) { __cSession.Send(MitakePacket.ToBuffer(new Logout())); //送出登出訊息 } __cSession.Close(); } } catch(Exception __errExcep1) { if (logger.IsErrorEnabled) logger.ErrorFormat("[QuoteService.Logout] {0}\r\n{1}", __errExcep1.Message, __errExcep1.StackTrace); } try { if (__cSocket != null) { __cSocket.CloseProc -= StockClient_onClose; if (__cSocket.Connected) { __cSocket.Send(MitakePacket.ToBuffer(new Logout())); //送出登出訊息 } __cSocket.Close(); } } catch (Exception __errExcep2) { if (logger.IsErrorEnabled) logger.ErrorFormat("[QuoteService.Logout] {0}\r\n{1}", __errExcep2.Message, __errExcep2.StackTrace); } lock (__cTimer) { __bTimeFlag = false; __cTimer.Stop(); //關閉Timer } lock (__cReLoginTimer) { __bReLogin = false; __cReLoginTimer.Stop(); } __cSession = null; __cSocket = null; this.IsLogin = false; if (logger.IsInfoEnabled) logger.Info("[QuoteService.Logout] Service logout success..."); }
private bool RegisterServer(ZSocket socket, string serviceIP, int servicePort, byte command) { SessionRegister sessionRegister = new SessionRegister(command, __sUserID, 0, __sSessionKey); if (socket.Connect(serviceIP, servicePort)) { try { socket.Send(MitakePacket.ToBuffer(sessionRegister)); return true; } catch(Exception __errExcep) { if(logger.IsErrorEnabled) logger.ErrorFormat("[QuoteService.RegisterServer] Register service fail... command={0}\r\n{1}\r\n{2}", command, __errExcep.StackTrace, __errExcep.Message); } } return false; }
/// <summary> /// 登入遠端伺服器 /// </summary> /// <returns>返回值:true=登入程序已成功(等候onLogin事件通知), false=登入程序失敗</returns> public override bool Login() { if (this.IsLogin) { return true; } int iBufferSize = 1; byte[] bArray = new byte[64]; if (logger.IsInfoEnabled) logger.InfoFormat("[QuoteService.Login] Login signer service... service={0}({1}), userId={2}", this.RemoteIP, this.RemotePort, this.UserId); AuthenticationLogin authLogin = new AuthenticationLogin(this.UserId, this.Password, 0, 0); try { Socket __cLogin = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); __cLogin.Connect(new IPEndPoint(IPAddress.Parse(this.RemoteIP), this.RemotePort)); __cLogin.Send(MitakePacket.ToBuffer(authLogin)); iBufferSize = __cLogin.Receive(bArray); __cLogin.Close(); } catch (Exception __errExcep) { logger.ErrorFormat("{0}\r\n{1}", __errExcep.Message, __errExcep.StackTrace); return false; } if (iBufferSize == 0) { return false; } else { AuthenticationReturn authReturn = null; authReturn = MitakePacket.ToStructure<AuthenticationReturn>(bArray, iBufferSize); __sUserID = this.UserId; __sSessionKey = authReturn.SessionKey; string sServiceIP = authReturn.RemoteIP; int iPort = authReturn.RemotePort; if (logger.IsInfoEnabled) logger.InfoFormat("[QuoteService.Login] Register service... service={0}({1}), userId={2}, sessionKey:{3}", sServiceIP, iPort, __sUserID, __sSessionKey); StockDecoder.TimerProc += StockClient_onTimer; StockDecoder.StockProc += StockClient_onStock; McpDecoder.McpPacketProc += StockClient_onMcpPacket; if (__cSocket == null) { __cSocket = new ZSocket(); __cSocket.ReceiveProc += SocketClient_onReceive; __cSocket.CloseProc += StockClient_onClose; } if (!RegisterServer(__cSocket, sServiceIP, iPort, 0x06)) { return false; } if (__cSession == null) { __cSession = new ZSocket(); __cSession.ReceiveProc += SocketClient_onReceive; __cSession.CloseProc += StockClient_onClose; } if (!RegisterServer(__cSession, sServiceIP, iPort, 0x05)) { return false; } lock (__cTimer) { __bTimeFlag = true; __cTimer.Start(); } MitakeSymbolManager.DataSource = this.DataSource; MitakeSymbolManager.ExchangeName = this.ExchangeName; StockDecoder.TimerProc += GetTradeDateFromLogin; //先取得最後交易日期之後再處理股票代號表之類的運作 this.SendSubscribe(); //登入成功後就送出訂閱資訊 } return true; }