コード例 #1
0
        void OnUDPReceived(string endPointIp, byte[] data, int size)
        {
            int head = 0;

            while (head < size)
            {
                BytePacker packer   = new BytePacker(data);
                short      datasize = packer.ReadShort();
#if DISABLE_CHANNEL_VARINT
                short channelId = packer.ReadShort();
#else
                int   s         = 0;
                short channelId = VarintBitConverter.ToInt16(packer, out s);
#endif

                if (channelId == (short)PreservedChannelId.Beacon)
                {
                    if (acceptBeacon && !isConnecting && !IsConnected)
                    {
                        string beaconData = (string)DataSerializer.Deserialize <string>(packer);

                        if (BeaconAccept(beaconData))
                        {
                            Connect(endPointIp);
                        }
                    }
                }
                else if (!dataChannelMap.ContainsKey(channelId))
                {
                }
                else
                {
                    if (serverNode == null)
                    {
                        break;
                    }
                    if (endPointIp == serverNode.IP)
                    {
                        healthLostCount = 0;

                        IDataChannel channel   = dataChannelMap[channelId];
                        object       container = channel.FromStream(ref packer);

                        channel.Received(serverNode, container);
                    }
                }

                head += datasize + 4;
            }
        }
コード例 #2
0
        void OnUDPReceived(string endPointIp, byte[] data, int size)
        {
            int head = 0;

            while (head < size)
            {
                BytePacker packer   = new BytePacker(data);
                short      datasize = packer.ReadShort();
#if DISABLE_CHANNEL_VARINT
                short channelId = packer.ReadShort();
#else
                int   s         = 0;
                short channelId = VarintBitConverter.ToInt16(packer, out s);
#endif

                if (channelId == (short)PreservedChannelId.Beacon)
                {
                }
                else if (!dataChannelMap.ContainsKey(channelId))
                {
                }
                else
                {
                    if (nodeMap.ContainsKey(endPointIp))
                    {
                        ComNode node = nodeMap[endPointIp];

                        node.HealthLostCount = 0;

                        IDataChannel channel = dataChannelMap[channelId];

                        object container = channel.FromStream(ref packer);

                        channel.Received(node, container);
                    }
                }

                head += datasize + 4;
            }
        }
コード例 #3
0
ファイル: TCPConnection.cs プロジェクト: nakky/Snowball
        public async Task Start()
        {
            cancelToken.Token.Register(() => client.Close());
            int   resSize   = 0;
            short channelId = 0;

            bool isRent = false;

            byte[] buffer = null;

            do
            {
                try
                {
                    resSize = await nStream.ReadAsync(receiveBuffer, 0, 2, cancelToken.Token).ConfigureAwait(false);

                    if (resSize != 0)
                    {
                        receivePacker.Position = 0;
                        resSize = receivePacker.ReadShort();
#if DISABLE_CHANNEL_VARINT
                        await nStream.ReadAsync(receiveBuffer, 0, 2, cancelToken.Token).ConfigureAwait(false);

                        receivePacker.Position = 0;
                        channelId = receivePacker.ReadShort();
                        await nStream.ReadAsync(receiveBuffer, 0, resSize, cancelToken.Token).ConfigureAwait(false);
#else
                        int s = 0;
                        channelId = VarintBitConverter.ToInt16(nStream, out s);
                        await nStream.ReadAsync(receiveBuffer, 0, resSize, cancelToken.Token).ConfigureAwait(false);
#endif


                        buffer = arrayPool.Rent(resSize);
                        if (buffer != null)
                        {
                            isRent = true;
                        }
                        else
                        {
                            buffer = new byte[resSize];
                            isRent = false;
                        }

                        Array.Copy(receiveBuffer, buffer, resSize);

                        //Util.Log("TCP:" + resSize);
                    }
                }
                catch//(Exception e)
                {
                    //Util.Log("TCP:" + e.Message);
                    break;
                }

                if (resSize == 0)
                {
                    break;
                }

                if (cancelToken.IsCancellationRequested)
                {
                    break;
                }

                if (Global.SyncContext != null)
                {
                    Global.SyncContext.Post((state) => {
                        if (cancelToken.IsCancellationRequested)
                        {
                            return;
                        }
                        CallbackParam param = (CallbackParam)state;
                        if (OnReceive != null)
                        {
                            OnReceive(param.Ip, param.channelId, param.buffer, param.size);
                        }
                        if (isRent)
                        {
                            arrayPool.Return(buffer);
                        }
                    }, new CallbackParam(IP, channelId, buffer, resSize, isRent));
                }
                else
                {
                    if (OnReceive != null)
                    {
                        OnReceive(IP, channelId, receiveBuffer, resSize);
                    }
                }
            } while (client.Connected);

            Disconnect();
        }