示例#1
0
        public void OutPacket(WorldPack pack)
        {
            if (!connected())
            {
                return;
            }
            UInt16     size = (UInt16)(pack.size() + C2S_PACK_HEAD_SIZE);
            ByteBuffer data = new ByteBuffer(size);

            data.W((UInt16)(pack.size() + 4));
            data.W((UInt32)pack.opcode());
            data.W(pack);
            _tcpClient.Client.Send(data.contents());
        }
示例#2
0
        bool HandleSysPack(WorldPack pack)
        {
            Debug.Log("SystemPack Start");
            if (!pack.isSystem)
            {
                return(false);
            }
            short type  = 0;
            Int16 param = 0;

            pack.R(ref type);
            Debug.Log("SystemPack " + type.ToString());
            switch (type)
            {
            case SYS_PACKET_KEEP_ALIVE:
                pack.R(ref param);
                // Sys_Log("recv keep alive msg");
                byte      p1 = 1;
                WorldPack pk = new WorldPack();
                pk.W(SYS_PACKET_ALIVE_ACK);
                pk.W(param);
                pk.W(p1);
                gcOutPacket(pk, true);
                break;

            case SYS_PACKET_ALIVE_ACK:
                pack.R(ref param);
                // Sys_Log("recv alive ack msg");
                m_rtt = Time.time - (float)param / 1000.0f;
                break;

            case SYS_PACKET_SET_SEED:
            {
                pack.R(ref param);
                SetSeed(param);
                ConnectWorkFinish(true);
            }
            break;

            default:
                return(false);
            }
            return(true);
        }
示例#3
0
        private void _handlePong(WorldPack pack)
        {
            UInt32 rec_ping = 0;
            UInt32 now      = (UInt32)DateTime.UtcNow.Millisecond;

            pack.R(ref rec_ping);
            if (rec_ping != _ping)
            {
                Console.WriteLine("sending : we look for get ping: {0},but get ping: {1} on time: {2}"
                                  , _ping
                                  , rec_ping
                                  , now);
                _pinAck = true;
                return;
            }

            _latencey = (UInt32)(now - _lastPing);
            _pinAck   = true;
        }
示例#4
0
 private void addEvent(WorldPack Packet)
 {
     lock (_events)
     {
         bool missEvent = false;
         for (int i = 0; i < _missedEvents.Count; i++)
         {
             if (_missedEvents[i] == Packet.opcode())
             {
                 _missedEvents.RemoveAt(i);
                 Debug.LogWarning("ignore missed event: " + Packet.opcode() + ", missed events count: " + _missedEvents.Count);
                 missEvent = true;
                 break;
             }
         }
         if (!missEvent)
         {
             _events.Add(Packet);
         }
     }
 }
示例#5
0
        // once connected. we start to read data and ping.
        private void _onConnected(IAsyncResult result)
        {
            Debug.Log("_onConnected " + _tcpClient.Connected.ToString() + "  " + result.ToString());
            if (true == _tcpClient.Connected)
            {
                Console.WriteLine("{0} connect to server is successful.", _tcpClient.Client.ToString());
            }
            else
            {
                Console.WriteLine("Connecting error and failed.");
                WorldPack Packet = new WorldPack(Opcodes.SMSG_CONNECT_FAILED, 0);
                if (null != _packHandler)
                {
                    // push into a packet array.
                    addEvent(Packet);
                }
                _tcpClient.Close();
                _tcpClient = null;
                return;
            }

            // init stuff.
            _rpos = _wpos = 0;

            Debug.Log("_onConnected read first ");

            _netStream = new NetworkStream(_tcpClient.Client);
            _netStream.BeginRead(
                _read,
                (int)_wpos,
                (int)(_read.Length - _wpos),
                new AsyncCallback(_onRead),
                null);
            // 将下次收发的数据放到 _read _wpos的位置。并调用 onRead处理buffer。
            // init sending pings.
            // SendPing();

            Debug.Log("_onConnected end ");
        }
示例#6
0
        private void _handleAddChar(WorldPack pack)
        {
            UInt16 id   = 0;
            string name = "";
            bool   male = true;
            UInt16 race = 0;
            UInt16 lv   = 0;

            pack.R(ref id);
            pack.R(ref name);
            pack.R(ref male);
            pack.R(ref race);
            pack.R(ref lv);

            // add this char to your elder list.
            string str = String.Format(" Add char id = {0},name = {1}, is {2}, race is {3},lv is {4}",
                                       id,
                                       name,
                                       male?"男":"女",
                                       race,
                                       lv);

            Debug.Log(str);
        }
示例#7
0
 // on conncted.
 private void _handleAuthChallenge(WorldPack wp) // this should be in....
 {
     // send our client proof to server to complete authChallenge...now do nothing.
     Console.WriteLine("Connected...handleChallenge successfull");
     m_isLogin = true;
 }
示例#8
0
文件: Socket.cs 项目: mengtest/u3dmmo
        //event 3, do receive stuff for clients.
        // use tcpClients snync reading methods, read them into circular buffer, and make worldpack.
        // Callback function when receive byte data,Using tcpClient start read sync fucntion.
        private void _onRead(IAsyncResult result)
        {
            Debug.Log("_onRead " + result.ToString());
            // check if we have been gc or closed.
            if (!connected() || null == _netStream)
            {
                return;
            }

            int readByteThisTime = _netStream.EndRead(result);

            if (readByteThisTime <= 0)
            {
                Debug.LogError("NetErrorCode.NET_RECV_ERROR");
                Console.WriteLine("NetErrorCode.NET_RECV_ERROR");
                Disconnect();
                // TryToConnect();
                return;
            }
            Debug.Log("_onRead Receive beytes " + readByteThisTime.ToString());
            //Console.WriteLine("Receive beytes {0}",readByteThisTime);

            _wpos += (UInt16)readByteThisTime;
            // _wpos 为当前系统向_read中数据写到的长度。指向_read中数据的结尾处。
            // _rpos指向当前读数据开始的位置,只有在成功读出一个包的时候才改变它。
            // 缓存中当前处理实际数据大小为 _wpos-_rpos.
            // 循环处理多个包之后,缓存中的数据不足以一个包的时候,做自拷贝动作。
            // :将 _rpos --> _wpos的数据拷贝到_read开头。
            // _wpos and _rpos  all --rpos;

            while (true)
            {
                Debug.Log("_onRead(While)" + _wpos.ToString() + "  " + _rpos.ToString());
                Debug.Log("_onRead(While) _read=" + _read.ToString());
                for (int i = 0; i < 11; i++)
                {
                    Debug.Log("_onRead(While) _read:" + i.ToString() + "->" + _read[i].ToString());
                }

                int size = _wpos - _rpos;
                System.Diagnostics.Debug.Assert(size >= 0);
                if (size < 6)
                {
                    break;             /* no head in the apcket, let's wait.*/
                }

                // head: size(u16)+code(u32)
                UInt16 packetSize = BitConverter.ToUInt16(_read, (int)_rpos);
                if (packetSize < 4)
                {
                    Debug.LogError("packetSize<4!!"); break;
                }

                packetSize = (UInt16)(packetSize - 4);
                if (size < packetSize + S2C_PACK_HEAD_SIZE)
                {
                    break;                                          /*we have a fragmented apcket. wait fot the complete one before proceeding*/
                }
                Opcodes code = (Opcodes)BitConverter.ToUInt32(_read, (int)_rpos + MESSAGEHEAD_SIZE_LENGTH);

                WorldPack Packet = new WorldPack(code, packetSize);
                Packet.append(_read, _rpos + S2C_PACK_HEAD_SIZE, packetSize); // 将缓存中,从当前_rpos + 包头的数据给WorldPack.

                // 纪录发包either.
                // LogPacketPrintf(mSize, static_cast<uint16>(mOpcode), mSize ? Packet->contents() : NULL, 0, 0);
                _rpos += (UInt16)(packetSize + S2C_PACK_HEAD_SIZE);

                if (Packet.opcode() == Opcodes.SMSG_PONG)
                {
                    _handlePong(Packet);
                }
                else if (null != _packHandler)
                {
                    // push into a packet array.
                    addEvent(Packet);
                }
            }

            // copy the left data to the top.
            if (_wpos > _rpos)
            {
                Array.Copy(_read, _rpos, _read, 0, _wpos - _rpos);
            }

            _wpos -= _rpos; // _wpos == _read.getLenth(0);
            _rpos  = 0;
            Debug.Log("_onRead(While)  end");
            System.Diagnostics.Debug.Assert(_netStream != null);
            if (true == _tcpClient.Connected)
            {
                Debug.Log("_onRead to  _onRead");

                _netStream.BeginRead(
                    _read,
                    (int)_wpos,
                    (int)(_read.Length - _wpos),
                    new AsyncCallback(_onRead),
                    null);

                Debug.Log("_onRead to  _onRead2");
            }

            // do a mess packets read to test this code.
        }
示例#9
0
 public LoginSocket h;                                          // using for sending packet
 public void onPakcet(WorldPack packet)
 {
     _handleRPC(packet);
 }                                                              // handle packet
示例#10
0
        private void _handleRPC(WorldPack pack)
        {
            //receive
            if (false == enable)
            {
                Debug.LogWarning("RPC: we stoped rpc ,waiting for resume");
                packs.Add(pack);
                return;
            }

            UInt32 prams = 0;

            pack.R(ref prams);
            string methodName = "";

            pack.R(ref methodName);

            string des = methodName;

            des += "(";
            List <object> pramList = new List <object>();
            int           count    = 0;
            UInt32        type     = (prams >> (3 * count) & 7);

            while (type != 0)
            {
                switch (type)
                {
                case LUA_TBOOLEAN:
                {
                    Boolean value = false;
                    pack.R(ref value);
                    pramList.Add(value);
                    des += value.ToString();
                    des += " , ";
                }
                break;

                case LUA_TUSERDATA:
                {
                    Int64 value = 0;
                    pack.R(ref value);
                    pramList.Add(value);
                    des += "[Int64]";
                    des += value.ToString();
                    des += " , ";
                }
                break;

                //case LUA_TTHREAD:
                //    {
                //        UInt64 value = 0;
                //        pack.R(ref value);
                //        pramList.Add(value);
                //        des += value.ToString();
                //        des += " , ";
                //    }
                //    break;
                case LUA_TBNUMBER:
                {
                    Int32 value = 0;
                    pack.R(ref value);
                    pramList.Add(value);
                    des += "[Int32]";
                    des += value.ToString();
                    des += " , ";
                }
                break;

                case LUA_TSTRING:
                {
                    string value = "";
                    pack.R(ref value);
                    pramList.Add(value);
                    des += "[String]";
                    des += value.ToString();
                    des += " , ";
                }
                break;

                default:
                {
                    Debug.LogWarning("RPC: callClientMethod Got unknown data type methods");
                }
                break;
                }
                count++;
                type = (prams >> (3 * count) & 7);
            }
            des += " ) ";
            if (methodName != "a")
            {
                Log("S->  " + des);
            }
            _callClientMethod(methodName, pramList);
        }
示例#11
0
 private void _handleTable(WorldPack pack)
 {
     return;
 }
示例#12
0
        WorldPack rpcToPack(string methodName, params object[] par)
        {
            WorldPack pack  = new WorldPack(Opcodes.CMSG_GODMODE, (UInt16)(methodName.Length * 2 + 2 + 8000));
            UInt32    prams = 0;

            pack.W(prams);
            pack.W(methodName);
            int    count = 0;
            UInt32 type  = 0;

            foreach (object ob in par)
            {
                if (ob is System.Int32)
                {
                    pack.W((Int32)ob);
                    type = LUA_TBNUMBER;
                }
                else if (ob is System.Int16)
                {
                    Int16 temp = (Int16)ob;
                    pack.W((Int32)temp);
                    type = LUA_TBNUMBER;
                }
                else if (ob is System.UInt16)
                {
                    UInt16 temp = (UInt16)ob;
                    pack.W((Int32)temp);
                    type = LUA_TBNUMBER;
                }
                else if (ob is System.UInt32)
                {
                    pack.W((UInt32)ob);
                    type = LUA_TFUNCTION;
                }
                else if (ob is System.Int64)
                {
                    Int64 temp = (Int64)ob;
                    pack.W((Int64)temp);
                    type = LUA_TUSERDATA;
                }
                else if (ob is System.Boolean)
                {
                    pack.W((Boolean)ob);
                    type = LUA_TBOOLEAN;
                }
                else if (ob is System.String)
                {
                    pack.W((string)ob);
                    type = LUA_TSTRING;
                }
                else
                {
                    Debug.LogError("RPC: callServerMethod Got unknown data type methods");
                }

                prams = (prams | (type & 0xf) << 3 * count++);
            }
            pack.wpos(0);
            pack.W(prams);

            return(pack);
        }