예제 #1
0
        private void BtnSendClientName_Click(object sender, RoutedEventArgs e)
        {
            string message = txtboxClientName.Text;

            txtboxInfo.Text = $"【发送客户端名称】 {message}\r\n" + txtboxInfo.Text;
            TSocketMessage msg = new TSocketMessage(Encoding.Unicode.GetBytes(message));

            ClientSocket.SendMsg(msg, MessageType.clientName);
        }
예제 #2
0
        public void RecriveClientName(TSocketMessage msg)
        {
            string clientName;

            using (MMO_MemoryStream ms = new MMO_MemoryStream(msg.MsgBuffer))
            {
                clientName = Encoding.Unicode.GetString(ms.ReadBytes((int)ms.Length));
                MessageBox.Show(clientName);
            }
        }
예제 #3
0
        /// <summary>
        /// 发送消息方法
        /// </summary>
        public int SendMsg(TSocketMessage msg, MessageType msgType)
        {
            int size = 0;

            try
            {
                if (!this.IsDispose)
                {
                    //封包
                    byte[] buffer = PHelper.PackData(msg, msgType);
                    size = this._Socket.Send(buffer);
                }
            }
            catch (System.ObjectDisposedException) { this.Close("链接已经被关闭"); }
            catch (System.Net.Sockets.SocketException) { this.Close("链接已经被关闭"); }
            msg.Dispose();
            return(size);
        }
예제 #4
0
        /// <summary>
        /// 当消息类型为Information时
        /// </summary>
        /// <param name="msg"></param>
        public void ReceiveInfo(TSocketMessage msg)
        {
            StoreHeap finalPath = new StoreHeap();

            using (MMO_MemoryStream ms = new MMO_MemoryStream(msg.MsgBuffer))
            {
                // 当未读取到字节流末尾时,将节点信息取出
                while (ms.Length != ms.Position)
                {
                    // 分别读取节点编号、节点X坐标、节点Y坐标、途径节点运动方向
                    Node node = new Node(ms.ReadShort(), ms.ReadShort(), ms.ReadShort(), ms.ReadShort());
                    finalPath.NodeList.Add(node);
                }
            }

            string result = "";

            result += finalPath.NodeList[0].Index.ToString() + "(直行)";

            for (int i = 1; i < finalPath.Count; i++)
            {
                string direction;
                if (finalPath.NodeList[i].Direction == 0)
                {
                    direction = "直行";
                }
                else if (finalPath.NodeList[i].Direction == 1)
                {
                    direction = "左转";
                }
                else if (finalPath.NodeList[i].Direction == 2)
                {
                    direction = "右转";
                }
                else
                {
                    direction = "停止";
                }
                result += " => " + finalPath.NodeList[i].Index.ToString() + $"({direction})";
            }

            MessageBox.Show(result);
        }
예제 #5
0
        /// <summary>
        /// 收到消息后
        /// </summary>
        /// <param name="rbuff"></param>
        public override void Receive(TSocketMessage msg)
        {
            switch (msg.MsgType)
            {
            case 0:
                ReceiveInfo(msg);
                break;

            case 1:
                ReceiveCommand(msg);
                break;

            default:
                break;
            }

            if (isServer)
            {
                //this.SendMsg(new TSocketMessage(Encoding.Unicode.GetBytes("Holle Client!")));
            }
        }
예제 #6
0
        /// <summary>
        /// 封包
        /// </summary>
        /// <param name="data">待处理的数据</param>
        /// <returns></returns>
        public byte[] PackData(TSocketMessage msg, MessageType msgType)
        {
            // 封包后返回的数据
            byte[] retBuffer = null;
            // 待封包的数据
            byte[] msgBuffer = msg.MsgBuffer;
            // 数据类型
            byte messageType = (byte)msgType;

            using (MMO_MemoryStream ms = new MMO_MemoryStream())
            {
                //封装包头
                ms.WriteShort(head1);
                ms.WriteShort(head2);
                //包协议
                if (msgBuffer != null)
                {
                    // 写入数据类型
                    ms.WriteByte(messageType);
                    // 写入包体长度信息
                    ms.WriteInt((int)(msgBuffer.Length + 2));
                    // 获取经异或加密后的数据
                    msgBuffer = SecurityUtil.Xor(msgBuffer);
                    // 获取 Crc 冗余校验码
                    ushort crc = Crc16.CalculateCrc16(msgBuffer);
                    // 写入 Crc 冗余校验码
                    ms.WriteUShort(crc);
                    // 写入 Xor 加密后的数据
                    ms.Write(msgBuffer, 0, msgBuffer.Length);
                }
                else
                {
                    ms.WriteInt(0);
                }
                // 获取处理后的完整数据包
                retBuffer = ms.ToArray();
            }
            return(retBuffer);
        }
예제 #7
0
        /// <summary>
        /// 当消息类型为Command时
        /// </summary>
        /// <param name="msg"></param>
        public void ReceiveCommand(TSocketMessage msg)
        {
            string message = Encoding.Unicode.GetString(msg.MsgBuffer);

            MessageBox.Show("收到命令!");
        }
예제 #8
0
 /// <summary>
 /// 对接收的信息进行处理
 /// </summary>
 /// <param name="msg">信息</param>
 public abstract void Receive(TSocketMessage msg);