예제 #1
0
        public static MsgRec Create(byte[] headbuff)
        {
            MsgRec    msg = new MsgRec();
            ByteArray ba  = new ByteArray(headbuff);

            //ba.ReadByte();
            //ba.ReadByte();
            ba.ReadByte();


            //int gamecode = ba.ReadInt();
            msg.moduleId   = ba.ReadByte();
            msg.subId      = ba.ReadByte();
            msg.succ       = ba.ReadByte();
            msg.bodyLength = ba.ReadInt();
//            Debug.LogError(msg.bodyLength);
            Debug.Log("<color=#ffff00ff>socket receive head: " + msg.moduleId + "-" + msg.subId + "-----" + msg.succ + "</color>");

            return(msg);
        }
예제 #2
0
            private void Run()
            {
                byte[] head = new byte[MsgRec.HEAD_SIZE];
                while (running)
                {
                    MsgRec rec      = null;
                    int    received = 0;
                    try
                    {
                        received = stream.Read(head, 0, head.Length);
                        //                        Debug.Log(received);
                    }
                    catch (Exception e)
                    {
                        socketMgr.ReportError(client, "receive head error: " + e.ToString());
                        running = false;
                        break;
                    }
                    if (received != head.Length)
                    {
                        socketMgr.ReportError(client, "receive head size error(maybe stream end):" + received + "-" + head.Length);
                        running = false;
                        break;
                    }
                    rec = MsgRec.Create(head);
                    if (rec.bodyLength > 0)
                    {
                        byte[] buff = new byte[rec.bodyLength];

                        try
                        {
                            Debug.Log("socket begin receive body length: " + buff.Length);
                            received = stream.Read(buff, 0, buff.Length);

                            //                            Debug.Log("socket receive body length: " + buff.Length);
                        }
                        catch (Exception e)
                        {
                            socketMgr.ReportError(client, "receive content error: " + e.Message + e.StackTrace);
                            running = false;
                            break;
                        }
                        if (received != buff.Length)
                        {
                            socketMgr.ReportError(client, "content receive size error(maybe stream end):" + received + "/" + buff.Length);
                            running = false;
                            break;
                        }

                        rec.content = buff;
                        rec.Deserialize();
                        lock (lockobj)
                        {
                            recQueue.Enqueue(rec);
                        }
                    }
                    else
                    {
                        lock (lockobj)
                        {
                            recQueue.Enqueue(rec);
                        }
                    }
                }
            }