//创建空数据包 public static WritePacket CreateNullPacket() { WritePacket wp = new WritePacket(); wp.WriteInt8(MsgOutsideDef.MsgNull); return(wp); }
public void WriteToSslStream(WritePacket packet) { _curwritePacket = packet; ArraySegment <byte> tmparr = packet.Stream.GetIOBuffer(); this._sslstream.Write(tmparr.Array, tmparr.Offset, packet.Stream.Length); }
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); }
public CmdChatText(short t, string msg, bool not) { cmdType = t; wp = PacketFunc.CreateBroadcastPacket(not); //广播是否包括自己 wp.WriteInt16(MsgInsideDef.MsgChatText); wp.WriteString(msg); }
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); }
//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); }
//创建单发数据包 public static WritePacket CreatePeerPacket(Int64 to) { WritePacket wp = new WritePacket(); wp.WriteInt8(MsgOutsideDef.MsgPeer); wp.WriteInt64(to); return(wp); }
IEnumerator ping() { while (keepPing && true) { WritePacket wp = PacketFunc.CreateNullPacket(); client.Send(wp.ToArray()); yield return(new WaitForSeconds(8)); } }
//创建群发数据包 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); }
/// <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); } }
/// <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); } }
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); }
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)); } }
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)); } }
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); } }
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); }
// 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("右键点击"); } }
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); } }
/// <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; } }
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; } }
public SSLWritePacket(WritePacket parent) { _parent = parent; }
private void OnWritePacket(int size) { WritePacket?.Invoke(this, size); }
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"); }