void OnMessage(FreeNet.Packet packet) { // ex) PROTOCOL_ID protocol = (PROTOCOL_ID)packet.PopProtocolId(); //Console.WriteLine("------------------------------------------------------"); //Console.WriteLine("protocol id " + protocol); switch (protocol) { case PROTOCOL_ID.ECHO_REQ: { string text = packet.PopString(); Console.WriteLine(string.Format("text {0}", text)); var response = FreeNet.Packet.Create((short)PROTOCOL_ID.ECHO_ACK); response.Push(text); packet.Owner.Send(response); } break; default: { Console.WriteLine("Unknown protocol id " + protocol); } break; } }
public byte[] ToPacket(PROTOCOL_ID packetId, byte[] bodyData) { if (bodyData == null) { bodyData = Encoding.UTF8.GetBytes(Data); } var packetLen = (UInt16)(HeaderSize + bodyData.Length); var packet = new byte[packetLen]; FreeNet.FastBinaryWrite.UInt16(packet, 0, packetLen); FreeNet.FastBinaryWrite.UInt16(packet, 2, (UInt16)packetId); Buffer.BlockCopy(bodyData, 0, packet, HeaderSize, bodyData.Length); return(packet); }
void OnMessage(FreeNet.Packet packet) { // ex) PROTOCOL_ID protocol = (PROTOCOL_ID)packet.ProtocolId; //Console.WriteLine("------------------------------------------------------"); //Console.WriteLine("protocol id " + protocol); switch (protocol) { case PROTOCOL_ID.ECHO_REQ: { var requestPkt = new EchoPacket(); requestPkt.Decode(packet.BodyData); Console.WriteLine(string.Format("text {0}", requestPkt.Data)); var responsePkt = new EchoPacket(); var packetData = responsePkt.ToPacket(PROTOCOL_ID.ECHO_ACK, packet.BodyData); packet.Owner.Send(new ArraySegment <byte>(packetData, 0, packetData.Length)); } break; case PROTOCOL_ID.HEARTBEAT_UPDATE_NOTIFY: { Console.WriteLine($"heartbeat: {DateTime.Now}"); packet.Owner.LatestHeartbeatTime = (UInt64)DateTime.Now.Ticks; } break; default: { if (OnSystemPacket(packet) == false) { Console.WriteLine("Unknown protocol id " + protocol); } } break; } }