Exemplo n.º 1
0
        /// <summary>
        /// 将消息加入到消息队列中等待发送
        /// </summary>
        public static void Enqueue(Message message)
        {
            byte[] raw_bytes = PBConverter.Serialize <Message>(message);
            byte[] bytes     = _Pack(message.Type, raw_bytes);

            if (_curState == ClientState.Connected)
            {
                _messages.Enqueue(bytes);
            }
        }
Exemplo n.º 2
0
        public bool Send_KeepAliveRequest()
        {
            // 发送客户端的心跳包
            Message message = new Message
            {
                Type = BombplaneProto.Type.KeepaliveRequest
            };

            byte[] data = PBConverter.Serialize(message);
            return(Send_Data_ToServer(data));
        }
Exemplo n.º 3
0
        public LoginState LoginAction(string username, string password)
        {
            // 登录时需要进行的操作

            byte[]       recv_data     = new byte[BUF_SIZE];
            LoginRequest login_request = new LoginRequest
            {
                Username = ByteString.CopyFrom(username, Encoding.UTF8),
                Password = ByteString.CopyFrom(password, Encoding.UTF8)
            };
            Message message = new Message
            {
                Type         = BombplaneProto.Type.LoginRequest,
                Loginrequest = login_request
            };

            byte[] data = PBConverter.Serialize(message);

            Send_Data_ToServer(data);
            //System.Threading.Thread.Sleep(3000);
            LoginState state;

            Recv_Data_FromServer();
            //
            //socket.ReceiveFrom(recv_data, ref remote);
            //recv_data = this.state.buffer;

            Message recv_message = PBConverter.Deserialize <Message>(recv_data);

            if (recv_message.Type == BombplaneProto.Type.LoginResponse)
            {
                LoginResponse response = recv_message.Loginresponse;
                state = response.State;
                return(state);
            }
            else
            {
                // 收到的不是所需的消息类型,需要放到缓冲区
                queue_message.Append(recv_message);
            }
            state = LoginState.ServerError;
            Console.WriteLine("Login Action finished.");
            return(state);
        }
Exemplo n.º 4
0
        public bool Send_SingleCoordinate(int x, int y)
        {
            // 单坐标模式 将选择坐标发送到服务器
            Coordinate c = new Coordinate
            {
                X = x,
                Y = y
            };

            //Message message = new Message();
            //message.Type = BombplaneProto.Type.C
            // 使用PB序列化后得到的数据,准备发送到服务器
            byte[] data = PBConverter.Serialize <Coordinate>(c);
            //Coordinate temp = converter.Deserialize<Coordinate>(data);
            //Console.WriteLine("data from PB is " + temp.ToString());
            if (Send_Data_ToServer(data) == false)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 5
0
        private void Recv_Data_FromServer()
        {
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            while (true)
            {
                try
                {
                    byte[]  receiveBytes = receive_udp_client.Receive(ref remoteIpEndPoint);
                    Message recv_message = PBConverter.Deserialize <Message>(receiveBytes);

                    //string message = Encoding.Unicode.GetString(receiveBytes);

                    // 显示消息内容
                    //ShowMessageforView(receive_udp_client, string.Format("{0}[{1}]", remoteIpEndPoint, message));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }