/// <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); } finally { if (__cSession != null) { __cSession.Close(); } } 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); } finally { if (__cSocket != null) { __cSocket.Close(); } } 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..."); }
/// <summary> /// 傳送MCP命令給伺服器 /// </summary> /// <param name="mcpObject">MCP命令類別</param> internal void Send(object mcpObject) { if (__cSession != null) { if (__cSession.Connected) { try { __cSession.Send(MitakePacket.ToBuffer(mcpObject)); } catch (Exception __errExcep) { if (logger.IsErrorEnabled) logger.ErrorFormat("[QuoteService.Send] {0}\r\n{1}", __errExcep.Message, __errExcep.StackTrace); } } } }
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); if (__cSocket == null) { __cSocket = new ZSocket(); __cSocket.ReceiveProc += SocketClient_onReceive; __cSocket.CloseProc += StockClient_onClose; } if (!RegisterServer(__cSocket, sServiceIP, iPort, 0x06)) { __cSocket.Close(); //關閉 Stream Socket __cSocket = null; return false; } if (__cSession == null) { __cSession = new ZSocket(); __cSession.ReceiveProc += SocketClient_onReceive; __cSession.CloseProc += StockClient_onClose; } if (!RegisterServer(__cSession, sServiceIP, iPort, 0x05)) { //如果註冊失敗 __cSocket.Close(); //關閉 Stream Socket(一定要 Stream 與 Session 兩條通道都同時建立完成, 才算是連線成功) __cSocket = null; __cSession.Close(); //關閉 Session Socket __cSession = null; return false; } lock (__cTimer) { __bTimeFlag = true; __cTimer.Start(); } StockDecoder.TimerProc += StockClient_onTimer; StockDecoder.StockProc += StockClient_onStock; McpDecoder.McpPacketProc += StockClient_onMcpPacket; MitakeSymbolManager.DataSource = this.DataSource; MitakeSymbolManager.ExchangeName = this.ExchangeName; StockDecoder.TimerProc += GetTradeDateFromLogin; //先取得最後交易日期之後再處理股票代號表之類的運作 this.SendSubscribe(); } return true; }