Example #1
0
        public static Head ByteToHead(byte[] byt)
        {
            if (byt.Length != LENGTH)
            {
                return(null);
            }

            Head head = new Head();
            int  i = 0, j = 0;

            byte[] len = new byte[4];
            len[i++] = byt[j++];
            len[i++] = byt[j++];
            len[i++] = byt[j++];
            len[i++] = byt[j++];
            Array.Reverse(len);
            head.length = BitConverter.ToInt32(len, 0);

            i = 0;
            byte[] seqeId = new byte[8];
            seqeId[i++] = byt[j++];
            seqeId[i++] = byt[j++];
            seqeId[i++] = byt[j++];
            seqeId[i++] = byt[j++];
            seqeId[i++] = byt[j++];
            seqeId[i++] = byt[j++];
            seqeId[i++] = byt[j++];
            seqeId[i++] = byt[j++];
            Array.Reverse(seqeId);
            head.sequencedId = BitConverter.ToInt64(seqeId, 0);

            i = 0;
            byte[] cod = new byte[2];
            cod[i++] = byt[j++];
            cod[i++] = byt[j++];
            Array.Reverse(cod);
            head.code = BitConverter.ToInt16(cod, 0);

            i = 0;
            byte[] ver = new byte[4];
            ver[i++] = byt[j++];
            ver[i++] = byt[j++];
            ver[i++] = byt[j++];
            ver[i++] = byt[j++];
            Array.Reverse(ver);
            head.version = BitConverter.ToInt32(ver, 0);

            i = 0;
            byte[] term = new byte[4];
            term[i++]     = byt[j++];
            term[i++]     = byt[j++];
            term[i++]     = byt[j++];
            term[i++]     = byt[j++];
            head.terminal = Encoding.UTF8.GetString(term);

/*
 *          i = 0;
 *          byte[] req = new byte[4];
 *          req[i++] = byt[j++];
 *          req[i++] = byt[j++];
 *          req[i++] = byt[j++];
 *          req[i++] = byt[j++];
 *          Array.Reverse(req);
 *          head.requestId = BitConverter.ToInt32(req, 0);
 */
            return(head);
        }
Example #2
0
        public static void ThreadFun(object obj)
        {
            SocketWorker socketWorker = (SocketWorker)obj;


            Socket socket = socketWorker.sock;

            byte[] coin = Encoding.UTF8.GetBytes("{\"symbol\":\"XDFYX / CNY\"}");
            Head   hed  = new Head();

            hed.length      = 26 + coin.Length;
            hed.sequencedId = 0L;
            hed.code        = 20021;
            hed.version     = 1;
            hed.terminal    = "1001";
            hed.requestId   = 0;

            byte[] sedh = hed.headToByteArray();

            int hdlength = sedh.Length + coin.Length;

            byte[] senddata = new byte[hdlength];

            Buffer.BlockCopy(sedh, 0, senddata, 0, sedh.Length);  //这种方法仅适用于字节数组
            Buffer.BlockCopy(coin, 0, senddata, sedh.Length, coin.Length);

            int sendlen = socket.Send(senddata, senddata.Length, SocketFlags.None);

            try
            {
                for (; socket.Connected;)
                {
                    int headBuf    = Head.LENGTH;
                    int headLeft   = Head.LENGTH;
                    int headOffset = 0;

                    int    readLen    = 0;
                    byte[] HeadBuffer = new byte[Head.LENGTH];//数据长度
                    while (socket.Connected && headLeft > 0)
                    {
                        readLen = socket.Receive(HeadBuffer, headOffset, headLeft, SocketFlags.None);
                        if (readLen <= 0)
                        {
                            break;
                        }
                        headOffset += readLen;
                        headLeft   -= readLen;
                    }

                    Head head = Head.ByteToHead(HeadBuffer);
                    if (head == null)
                    {
                        throw new Exception("解析包头信息异常");
                    }
                    //接收包体数据
                    int bodyBuf    = head.length - Head.LENGTH;
                    int bodyLeft   = bodyBuf;
                    int bodyOffset = 0;

                    byte[] bodyBuffer = new byte[head.length - Head.LENGTH];

                    while (socket.Connected && bodyLeft > 0)
                    {
                        readLen = socket.Receive(bodyBuffer, bodyOffset, bodyLeft, SocketFlags.None);//读取socket数据
                        if (readLen <= 0)
                        {
                            break;
                        }
                        bodyLeft   -= readLen;
                        bodyOffset += readLen;
                    }
                    //读取信息后 解析
                    string message = Encoding.UTF8.GetString(bodyBuffer);
                    socketWorker.op.printmsg(message);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
Example #3
0
        public static void ThreadFun(object obj)
        {
            SocketWorker socketWorker = (SocketWorker)obj;
            Socket       socket       = socketWorker.sock;

            try
            {
                byte[] coin = Encoding.UTF8.GetBytes(socketWorker.subJson);
                Head   hed  = new Head();

                hed.length      = 26 + coin.Length;
                hed.sequencedId = 0L;
                hed.code        = 20021;
                hed.version     = 1;
                hed.terminal    = "1001";
                hed.requestId   = 0;

                byte[] sedh     = hed.headToByteArray();
                int    hdlength = sedh.Length + coin.Length;
                byte[] senddata = new byte[hdlength];
                // 包头 包体 合并
                Buffer.BlockCopy(sedh, 0, senddata, 0, sedh.Length);  //这种方法仅适用于字节数组
                Buffer.BlockCopy(coin, 0, senddata, sedh.Length, coin.Length);

                // 发送请求信息
                socket.Send(senddata, senddata.Length, SocketFlags.None);
                // 接收交易信息
                for (; socket.Connected && !socketWorker.tradeStop;)
                {
                    int headBuf    = Head.LENGTH;
                    int headLeft   = Head.LENGTH;
                    int headOffset = 0;

                    int    readLen    = 0;
                    byte[] HeadBuffer = new byte[Head.LENGTH];//数据长度
                    while (socket.Connected && headLeft > 0)
                    {
                        readLen = socket.Receive(HeadBuffer, headOffset, headLeft, SocketFlags.None);
                        if (readLen <= 0)
                        {
                            break;
                        }
                        headOffset += readLen;
                        headLeft   -= readLen;
                    }

                    Head head = Head.ByteToHead(HeadBuffer);
                    if (head == null)
                    {
                        throw new Exception("解析包头信息异常");
                    }
                    //接收包体数据
                    int bodyBuf    = head.length - Head.LENGTH;
                    int bodyLeft   = bodyBuf;
                    int bodyOffset = 0;

                    byte[] bodyBuffer = new byte[head.length - Head.LENGTH];

                    while (socket.Connected && bodyLeft > 0)
                    {
                        readLen = socket.Receive(bodyBuffer, bodyOffset, bodyLeft, SocketFlags.None);//读取socket数据
                        if (readLen <= 0)
                        {
                            break;
                        }
                        bodyLeft   -= readLen;
                        bodyOffset += readLen;
                    }
                    //读取信息后 解析
                    string json = Encoding.UTF8.GetString(bodyBuffer);
                    if (head.code == 20023 || head.code == 20024)
                    {
                        socketWorker.sybase.OnMarket(head.code, json);
                    }
                }
            }
            catch (ThreadAbortException tae)
            {
                try {
                    socketWorker.sybase.OnError(EnumExceptionCode.行情线程被迫终止, "【SocketWorker】 行情线程被强制关闭,线程即将退出! \r\n 异常信息:" + tae.ToString());
                    return;
                }
                catch { }
            }
            catch (Exception ex)
            {
                socketWorker.sybase.OnError(EnumExceptionCode.行情异常, "【SocketWorker】 异常信息:" + ex.ToString());
                return;
            }
        }