Пример #1
0
    //创建空数据包
    public static WritePacket CreateNullPacket()
    {
        WritePacket wp = new WritePacket();

        wp.WriteInt8(MsgOutsideDef.MsgNull);
        return(wp);
    }
Пример #2
0
        public void WriteToSslStream(WritePacket packet)
        {
            _curwritePacket = packet;
            ArraySegment <byte> tmparr = packet.Stream.GetIOBuffer();

            this._sslstream.Write(tmparr.Array, tmparr.Offset, packet.Stream.Length);
        }
Пример #3
0
    public CmdInputAction(short t, long aid, byte action, byte not, long to = 0)
    {
        cmdType = t;
        switch (not)
        {
        case MsgBroadcastType.Peer:
            wp = PacketFunc.CreatePeerPacket(to);
            break;

        case MsgBroadcastType.Broadcast:
            wp = PacketFunc.CreateBroadcastPacket(true);
            break;

        case MsgBroadcastType.BroadcastNot:
            wp = PacketFunc.CreateBroadcastPacket(false);
            break;

        default:
            wp = PacketFunc.CreateBroadcastPacket(true);
            break;
        }
        wp.WriteInt16(MsgInsideDef.MsgInputAction);
        wp.WriteInt8(action);
        wp.WriteInt64(aid);
    }
Пример #4
0
 public CmdChatText(short t, string msg, bool not)
 {
     cmdType = t;
     wp      = PacketFunc.CreateBroadcastPacket(not); //广播是否包括自己
     wp.WriteInt16(MsgInsideDef.MsgChatText);
     wp.WriteString(msg);
 }
Пример #5
0
        private async Task HandleTCPReceive_RT(SocketReceiveEventArgs args)
        {
            switch (args.ReadPacket.OpCode)
            {
            case 0:
                string guid     = args.ReadPacket.ReadString();
                string roomId   = args.ReadPacket.ReadString();
                string clientId = args.ReadPacket.ReadString();
                if (tempUsersDick.TryGetValue(guid, out NetSocket netSocket))
                {
                    tempUsersDick.Remove(guid);
                    await AddClient(roomId, clientId, new NetSocket(netSocket.MainSocket, NetManager.BUFFER_LENGTH));

                    WritePacket wr = new WritePacket(NetManager.RT_PACKET_OPCODE1);
                    netSocket.Send(wr);
                }
                else
                {
                }
                break;

            default:
                break;
            }
            await Task.FromResult(true);
        }
Пример #6
0
    //fire2按钮
    public void OnFire2Btn()
    {
        var         cmd = new CmdInputAction(MsgInsideDef.MsgInputAction, agent.GetConnectID(), InputType.Fire2, MsgBroadcastType.Broadcast);
        WritePacket wp  = cmd.Packet();

        wp.WriteFloat32(5.8f);
        cmd.Send(agent);
    }
Пример #7
0
    //创建单发数据包
    public static WritePacket CreatePeerPacket(Int64 to)
    {
        WritePacket wp = new WritePacket();

        wp.WriteInt8(MsgOutsideDef.MsgPeer);
        wp.WriteInt64(to);
        return(wp);
    }
Пример #8
0
 IEnumerator ping()
 {
     while (keepPing && true)
     {
         WritePacket wp = PacketFunc.CreateNullPacket();
         client.Send(wp.ToArray());
         yield return(new WaitForSeconds(8));
     }
 }
Пример #9
0
    //创建群发数据包
    public static WritePacket CreateBroadcastPacket(bool notself)
    {
        WritePacket wp = new WritePacket();

        wp.WriteInt8(MsgOutsideDef.MsgBroadcast);
        wp.WriteInt64(0);
        wp.WriteInt8(notself ? (byte)1 : (byte)0);
        return(wp);
    }
Пример #10
0
        /// <summary>
        /// 触发写入包的发送失败事件
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        public void ChannelWriteErr(IContext context, object msg)
        {
            WritePacket packet = msg as WritePacket;

            if (packet != null)
            {
                packet.EmitErr(context);
            }
        }
Пример #11
0
        /// <summary>
        /// 触发写入包的发送成功事件
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        public void ChannelWriteFinish(IContext context, object msg)
        {
            WritePacket packet = msg as WritePacket;

            if (packet != null)
            {
                packet.EmitSuccess(context);
            }
        }
Пример #12
0
        private async Task HandleTCPRT(NetSocket netSocket)
        {
            Guid guid = Guid.NewGuid();

            tempUsersDick.Add(guid.ToString(), netSocket);
            // TODO Complete TCP Connection
            WritePacket wr = new WritePacket(NetManager.RT_PACKET_OPCODE0);

            wr.AddString(guid.ToString());
            netSocket.Send(wr);
            await Task.FromResult(true);
        }
Пример #13
0
        public async Task <bool> Send(string id, WritePacket wr)
        {
            NetSocket client = await FindSocket(id);

            if (client != null)
            {
                client.Send(wr);
                return(await Task.FromResult(true));
            }
            else
            {
                return(await Task.FromResult(false));
            }
        }
Пример #14
0
 public async Task <bool> Send(WritePacket wr)
 {
     if (netSockets.Count > 0)
     {
         foreach (var item in netSockets)
         {
             item.Value.Send(wr);
         }
         return(await Task.FromResult(true));
     }
     else
     {
         return(await Task.FromResult(false));
     }
 }
Пример #15
0
    public void OnStartGame()
    {
        if (!isStart && agent.GetIds().Count >= MinPlayerCount)
        {
            var         cmd = new CmdInputAction(MsgInsideDef.MsgInputAction, agent.GetConnectID(), InputType.ButtonA, MsgBroadcastType.BroadcastNot);
            WritePacket wp  = cmd.Packet();
            cmd.Send(agent);

            Debug.Log("Click startGame");
        }
        else
        {
            Debug.Log("Player Count: " + agent.GetIds().Count);
        }
    }
Пример #16
0
 public override void ChannelWrite(IContext context, object msg)
 {
     if (EnsureAuthenticated())
     {
         if (!(msg is SSLWritePacket))
         {
             //判断是否非SSL包,是则进行SSL封包,并将当前包当作
             WritePacket wp = msg as WritePacket;
             if (wp != null)
             {
                 _agent.WriteToSslStream(wp);
                 return;
             }
         }
     }
     context.FirePreWrite(msg);
 }
Пример #17
0
    // Update is called once per frame
    void Update()
    {
        if (isStart && Input.GetMouseButtonDown(0))
        {
            Debug.Log("鼠标左键点击");
            var         cmd = new CmdInputAction(MsgInsideDef.MsgInputAction, agent.GetConnectID(), InputType.ButtonC, MsgBroadcastType.Broadcast);
            WritePacket wp  = cmd.Packet();
            wp.WriteInt32(isMain?1:0);
            cmd.Send(agent);
        }

        if (Input.GetMouseButtonDown(1))
        {
            blue.GetComponent <Rigidbody>().AddForce(0, 500, 0);
            Debug.Log("右键点击");
        }
    }
Пример #18
0
 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     _curwritePacket = null;
     Write(buffer, offset, count);
     if ((_state & SSLHandlerState.Authenticating) != 0 && _needRead)
     {
         //如果是在握手阶段则使用异步,需要读取才结束
         _AsyncWrite            = new SSLAsyncResult <int>();
         _AsyncWrite.Callback   = callback;
         _AsyncWrite.AsyncState = state;
         return(_AsyncWrite);
     }
     else
     {
         SSLSyncResult <int> result = new SSLSyncResult <int>();
         result.AsyncState = state;
         return(result);
     }
 }
Пример #19
0
    /// <summary>
    /// 动作回调
    /// </summary>

    public void OnAction(long id, byte action, ReadPacket rp)
    {
        switch (action)
        {
        case InputType.ButtonA:
            var         cmd = new CmdInputAction(MsgInsideDef.MsgInputAction, agent.GetConnectID(), InputType.ButtonD, MsgBroadcastType.Peer, id);
            WritePacket wp  = cmd.Packet();
            cmd.Send(agent);
            isStart = true;
            startGame.gameObject.SetActive(false);
            break;

        case InputType.ButtonC:
            Debug.Log("ButtonC");
            int isM = rp.ReadInt32();
            if (isM == 1)
            {
                blue.GetComponent <Rigidbody>().AddForce(0, 500, 0);
            }
            else
            {
                red.GetComponent <Rigidbody>().AddForce(0, 500, 0);
            }
            break;

        case InputType.ButtonD:
            isStart = true;
            isMain  = true;
            startGame.gameObject.SetActive(false);
            break;

        case InputType.Fire2:
            Debug.Log("收到动作命令, ID: " + id + " 动作: " + action + " 附加数据: " + rp.ReadFloat32());
            break;

        default:
            Debug.Log("收到动作命令, ID: " + id + " 动作: " + action);
            break;
        }
    }
Пример #20
0
        private static void HandleTCPReceive(SocketReceiveEventArgs args)
        {
            switch (args.ReadPacket.OpCode)
            {
            case 0:
                string guid = args.ReadPacket.ReadString();
                Console.WriteLine("Received : {0}", guid);
                WritePacket wr = new WritePacket(0);
                wr.AddString(guid);
                wr.AddString(matchId);
                wr.AddString(Id);
                tcpSocket.Send(wr);
                break;

            case 1:
                Console.WriteLine("You Can Play Now");
                break;

            default:
                break;
            }
        }
Пример #21
0
 public SSLWritePacket(WritePacket parent)
 {
     _parent = parent;
 }
Пример #22
0
 private void OnWritePacket(int size)
 {
     WritePacket?.Invoke(this, size);
 }
Пример #23
0
 void Start()
 {
     devinfo["motor"]   = new WritePacket(0x09, 0xaa, "motor", "short");
     devinfo["led"]     = new WritePacket(0x03, 0xab, "led", "byte");
     devinfo["marquee"] = new WritePacket(0x05, 0xac, "marquee", "byte");
 }