Ejemplo n.º 1
0
		private static void PacketDecode(Socket socket, SocketToken Token, bool IsDecode) {
                        int iCheck = 0;
                        PacketBuffer cPacket = Token.ReceiveBuffer;
                        cPacket.Position = -1;
                        while (++cPacket.Position < cPacket.Length) {
                                        iCheck = VerifyPacket(cPacket);
                                        switch (iCheck) {
                                                case -1: //未知封包
							if (Token.IsStart) {
								if (cPacket[0] == 0x0b) {
									if (McpDecoder.McpDecode(socket, Token)) {
										continue;
									}
								}
								Token.IsStart = false;
							}
							Token.AddTemp();
                                                        break;
                                                case 0:  //完整封包
                                                        int iSize = cPacket[3] + 4;
                                                        Token.SetPackage(iSize);
                                                        DecodeFinance(socket, Token.StockEvent, IsDecode);
                                                        cPacket.Position += (iSize - 1);
							Token.IsStart = true;
                                                        break;
                                                case 1:  //末端斷包
                                                        while (++cPacket.Position < cPacket.Length)
                                                                Token.AddTemp();
                                                        break;
                                        }
                                
                        }
                }
Ejemplo n.º 2
0
		internal static event McpPacketHandler McpPacketProc;       //MCP封包事件

                /// <summary>
                ///   MCP命令封包解碼(解碼整個封包)
                /// </summary>
                /// <param name="socket">作用中的Socket類別</param>
                /// <param name="token">SocketToken類別</param>
		internal static void Decode(Socket socket, SocketToken token) {
                        PacketDecode(socket, token);

                        if (token.IsDataToTemp) {
                                token.Move();
                                PacketDecode(socket, token);
                                token.Reduction(4096, 512);
                        }
                }
Ejemplo n.º 3
0
		internal SocketAsyncEventArgsPool(int capacity) {
                        __cStackPool = new Stack<SocketAsyncEventArgs>(capacity + 8);

                        for (int i = 0; i < capacity; i++) {
                                SocketToken cToken = new SocketToken();
                                SocketAsyncEventArgs cEventArgs = new SocketAsyncEventArgs();
                                cEventArgs.UserToken = cToken;
                                __cStackPool.Push(cEventArgs);
                        }
                }
Ejemplo n.º 4
0
		private static DecodeFinance __cFinance = new DecodeFinance();  //解碼所有國際金融期貨

                /// <summary>
                ///    解碼函式(解碼所有封包)
                /// </summary>
                /// <param name="socket">作用中的Socket類別</param>
                /// <param name="token">SocketToken類別</param>
                /// <param name="isDecode">是否啟動解碼功能</param>
                public static void Decode(Socket socket, SocketToken token, bool isDecode) {
			token.IsStart = false;

			PacketDecode(socket, token, isDecode);
                        
                        if (token.IsDataToTemp) {
				token.IsStart = true;
				
				token.Move();
                                PacketDecode(socket, token, isDecode);
                                token.Reduction(2048, 512);
                        }
                }
Ejemplo n.º 5
0
                private static void PacketDecode(Socket socket, SocketToken Token) {
                        PacketBuffer cPacket = Token.ReceiveBuffer;

                        cPacket.Position = -1;
                        while (++cPacket.Position < cPacket.Length) {
                                //判斷是否是MCP標頭格式
                                if (cPacket[0] == 0x0b) {
					if (logger.IsDebugEnabled) logger.Debug("[McpDecoder.PacketDecode] Start decode MCP packet...");
                                        if (McpDecode(socket, Token)) {
						continue;
                                        }
					if (logger.IsDebugEnabled) logger.Debug("[McpDecoder.PacketDecode] End decode MCP packet...");
                                }
			
				Token.AddTemp();
			}
                }
Ejemplo n.º 6
0
                /// <summary>
                ///   MCP封包解碼(一次解讀一條MCP命令)
                /// </summary>
                /// <param name="socket">作用中的Socket類別</param>
                /// <param name="Token">SocketToken類別</param>
                /// <returns>返回值:true=解碼成功  false=解碼失敗</returns>
		internal static bool McpDecode(Socket socket, SocketToken Token) {
                        PacketBuffer cPacket = Token.ReceiveBuffer;
                        
                        if ((cPacket.Position + 4) >= cPacket.Length) { //如果封包沒有長度表示封包不完整(需要合併)
                                return false;
                        }

                        int iSize = ((cPacket[3] << 8) + cPacket[4]) + 6;
                        if ((cPacket.Position + iSize) > cPacket.Length) {
                                if (logger.IsWarnEnabled) logger.WarnFormat("[McpDecoder.McpDecode] MCP封包內Length值過大... CurrentIndex={0}, MCP_Length={1}, packetSize={2}", cPacket.Position, iSize, cPacket.Length);
                                return false;
                        }

                        //判斷是否是結束字元
                        if (cPacket[iSize - 1] == 0xe) {
                                byte bType = cPacket[1];
                                byte bCommand = cPacket[2];

                                if (bType == 0x03 && bCommand == 0x01) {  //是否為伺服器傳來的心跳包
					if (logger.IsDebugEnabled) logger.DebugFormat("[McpDecoder.McpDecode] Socket({0}) 收到伺服器送來的心跳包...", socket.Handle.ToInt32());
                                } else {
                                        if (McpPacketProc != null) {
                                                Token.SetPackage(iSize);
						if (logger.IsDebugEnabled) logger.DebugFormat("[McpDecoder.McpDecode] Socket({0}) Type={1}, Command={2}", socket.Handle.ToInt32(), bType, bCommand);
						if (logger.IsDebugEnabled) logger.DebugFormat("[McpDecoder.McpDecode] Packet Dump:{0}", Token.Package.ToString());

                                                McpPacketEvent cEvent = Token.McpPacketEvent;
                                                cEvent.ActiveSocket = socket;

                                                McpPacketProc(cEvent);
                                        }
                                }

                                cPacket.Position += (iSize - 1);
                                return true;
                        }
                        return false;
                }
Ejemplo n.º 7
0
 internal ReceiveEvent(Socket socket, SocketToken token) {
         __cSocket = socket;
         __cToken = token;
 }