예제 #1
0
        public void SendMessage(string msg)
        {
            NetCommonMsg proto = new NetCommonMsg();

            proto.msg = msg;
            NetCenter.Send(Client, proto);
        }
예제 #2
0
        public static int Dispatch(ByteReader reader, ReceiveMsgCallback callback)
        {
            if (callback == null)
            {
                return(0);
            }

            /*
             * 协议格式:
             *      short: 协议大小
             *      short: 协议号
             *      动态长度
             */
            int LastPos = 0;

            try
            {
                do
                {
                    int msg_len   = reader.ReadShort();
                    int msg_start = reader.GetPos();

                    int         id  = reader.ReadShort();
                    INetMessage msg = NetCenter.Recognize(id, reader);
                    if (msg == null)
                    {
                        break;
                    }

                    // 协议大小不对!
                    if (msg_len != (reader.GetPos() - msg_start))
                    {
                        break;
                    }

                    LastPos = reader.GetPos();

                    try
                    {
                        callback(id, msg);
                    }
                    catch (Exception)
                    { }
                }while (!reader.IsEnd());
            }
            catch (Exception)
            { }

            return(LastPos);
        }
예제 #3
0
        void TickReceive()
        {
            int cnt = 0;

            semaphore.WaitOne();
            cnt = ReceivedBuffer.Count;
            semaphore.Release();

            if (cnt == 0)
            {
                return;
            }

            semaphore.WaitOne();
            ByteReader reader  = new ByteReader(ReceivedBuffer.ToArray(), 0);
            int        LastPos = NetCenter.Dispatch(reader, Callback);

            if (LastPos > 0)
            {
                ReceivedBuffer.RemoveRange(0, LastPos);
            }
            semaphore.Release();
        }