Пример #1
0
        void OnGet_Friends_Request(chat.Message recv_msg)
        {
            GetFriendsResponse.Builder friends = new GetFriendsResponse.Builder();
            Users.instance().forall((User usr) =>
            {
                Friend friend = new Friend.Builder()
                {
                    Name   = pb.ByteString.CopyFromUtf8(usr.Username),
                    Online = usr.Login
                }.Build();

                friends.AddFriends(friend);
            });

            Message rsp_msg = new Message.Builder()
            {
                MsgType   = MSG.Get_Friends_Response,
                Sequence  = recv_msg.Sequence,
                SessionId = this.SessionId,
                Response  = new Response.Builder()
                {
                    Result       = true,
                    LastResponse = true,
                    GetFriends   = friends.Build()
                }.Build()
            }.Build();

            SendMessage(rsp_msg);
        }
Пример #2
0
        void OnLogout_Request(chat.Message recv_msg)
        {
            Message rsp_msg = new Message.Builder()
            {
                MsgType   = MSG.Logout_Response,
                Sequence  = recv_msg.Sequence,
                SessionId = this.SessionId,
                Response  = new Response.Builder()
                {
                    Result       = true,
                    LastResponse = true
                }.Build()
            }.Build();

            SendMessage(rsp_msg);

            Login = false;
            var friendNotification = BuildFriendNotification(false);

            Users.instance().forall((User user) =>
            {
                if (user != this && user.login)
                {
                    user.SendMessage(friendNotification);
                }
            });

            Users.instance().DelUser(this);
        }
Пример #3
0
        void OnLogin_Request(chat.Message recv_msg)
        {
            Users.instance().AddUser(this);

            Username = recv_msg.Request.Login.Username.ToStringUtf8();

            chat.Message login_rsp = new chat.Message.Builder()
            {
                MsgType   = MSG.Login_Response,
                Sequence  = recv_msg.Sequence,
                SessionId = this.SessionId,
                Response  = new Response.Builder()
                {
                    Result       = true,
                    LastResponse = true,
                    Login        = new LoginResponse.Builder()
                    {
                        Ttl = 10
                    }.Build()
                }.Build()
            }.Build();
            Login = true;
            SendMessage(login_rsp);

            var friendNotification = BuildFriendNotification(true);

            Users.instance().forall((User user) =>
            {
                if (user != this && user.login)
                {
                    user.SendMessage(friendNotification);
                }
            });
        }
Пример #4
0
        void OnSend_Message_Request(chat.Message recv_msg)
        {
            Message rsp_msg = new Message.Builder()
            {
                MsgType   = MSG.Send_Message_Response,
                Sequence  = recv_msg.Sequence,
                SessionId = this.SessionId,
                Response  = new Response.Builder()
                {
                    Result       = true,
                    LastResponse = true
                }.Build()
            }.Build();

            SendMessage(rsp_msg);

            Message text_msg = new Message.Builder()
            {
                MsgType      = MSG.Message_Notification,
                Sequence     = 0xffffffff,
                Notification = new Notification.Builder()
                {
                    Msg = new MessageNotification.Builder()
                    {
                        Sender    = Username,
                        Text      = recv_msg.Request.SendMessage.Text,
                        Timestamp = DateTime.Now.ToString()
                    }.Build()
                }.Build()
            }.Build();

            if (recv_msg.Request.SendMessage.HasReceiver)
            {
                string receiver = recv_msg.Request.SendMessage.Receiver.ToStringUtf8();

                Users.instance().forall((User usr) =>
                {
                    if (usr.Username.Equals(receiver))
                    {
                        usr.SendMessage(text_msg);
                    }
                });
            }
            else
            {
                Users.instance().forall((User user) =>
                {
                    if (user != this && user.login)
                    {
                        user.SendMessage(text_msg);
                    }
                });
            }
        }
Пример #5
0
        public void SendMessage(chat.Message msg)
        {
            int size = msg.SerializedSize;

            byte[] buf = new byte[size + 4];

            buf[0] = (byte)((size >> 24) & 0x000000ff);
            buf[1] = (byte)((size >> 16) & 0x000000ff);
            buf[2] = (byte)((size >> 8) & 0x000000ff);
            buf[3] = (byte)((size) & 0x000000ff);

            pb.CodedOutputStream cos = pb.CodedOutputStream.CreateInstance(buf, 4, size);
            msg.WriteTo(cos);

            SendMessage(buf);
        }
Пример #6
0
        void OnKeepalive_Request(chat.Message recv_msg)
        {
            Message rsp_msg = new Message.Builder()
            {
                MsgType   = MSG.Keepalive_Response,
                Sequence  = recv_msg.Sequence,
                SessionId = this.SessionId,
                Response  = new Response.Builder()
                {
                    Result       = true,
                    LastResponse = true
                }.Build()
            }.Build();

            SendMessage(rsp_msg);
        }
Пример #7
0
        void OnReadBody(IAsyncResult ar)
        {
            try
            {
                int len = connection_.Client.EndReceive(ar);

                body_read_len += len;
                if (body_read_len < body.Length)
                {
                    connection_.Client.BeginReceive(body, body_read_len, body.Length - body_read_len, 0, new AsyncCallback(OnReadBody), null);
                    return;
                }
                StartReadHead();
                try
                {
                    chat.Message recv_msg = chat.Message.ParseFrom(body);

                    Console.WriteLine("recv msg:{0}", recv_msg.ToString());
                    switch (recv_msg.MsgType)
                    {
                    case chat.MSG.Login_Request:
                    {
                        OnLogin_Request(recv_msg);
                    }
                    break;

                    case chat.MSG.Logout_Request:
                    {
                        OnLogout_Request(recv_msg);
                    }
                    break;

                    case chat.MSG.Keepalive_Request:
                    {
                        OnKeepalive_Request(recv_msg);
                    }
                    break;

                    case chat.MSG.Get_Friends_Request:
                    {
                        OnGet_Friends_Request(recv_msg);
                    }
                    break;

                    case chat.MSG.Send_Message_Request:
                    {
                        OnSend_Message_Request(recv_msg);
                    }
                    break;

                    default:
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("ParserMessage Exception:{0}", e.Message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("OnReadBody Exception:{0}", ex.Message);
            }
        }
Пример #8
0
        void OnReadBody(IAsyncResult ar)
        {
            try
            {
                int len = connection_.Client.EndReceive(ar);

                body_read_len += len;
                if (body_read_len < body.Length)
                {
                    connection_.Client.BeginReceive(
                        body, body_read_len, body.Length - body_read_len, 0,
                        new AsyncCallback(OnReadBody), null);
                    return;
                }
                StartReadHead();
                try
                {
                    chat.Message recv_msg = chat.Message.ParseFrom(body);

                    switch (recv_msg.MsgType)
                    {
                    case chat.MSG.Login_Request:
                    {
                        Username = recv_msg.Request.Login.Username.ToStringUtf8();

                        chat.Message login_rsp = new chat.Message.Builder()
                        {
                            MsgType   = MSG.Login_Response,
                            Sequence  = recv_msg.Sequence,
                            SessionId = this.SessionId,
                            Response  = new Response.Builder()
                            {
                                Result       = true,
                                LastResponse = true,
                                Login        = new LoginResponse.Builder()
                                {
                                    Ttl = 10
                                }.Build()
                            }.Build()
                        }.Build();
                        Login = true;

                        SendMessage(login_rsp);

                        var friendNotification = BuildFriendNotification(true);
                        Users.instance().forall((User otherUser) =>
                            {
                                if (otherUser != this && otherUser.login)
                                {
                                    otherUser.SendMessage(friendNotification);
                                }
                            });
                    }
                    break;

                    //case chat.MSG.GetScreen_Request:
                    //    {
                    //        lock (BMPScreen.IsLockGetJpgBytes)
                    //        {
                    //            if (this.SendScreen.AddSeconds(1) < DateTime.Now)
                    //            {
                    //                chat.Message screenresponse = new chat.Message.Builder()
                    //                {
                    //                    MsgType = chat.MSG.GetScreen_Response,
                    //                    Sequence = recv_msg.Sequence,
                    //                    Response = new Response.Builder()
                    //                    {
                    //                        Result = true,
                    //                        LastResponse = true,
                    //                        Screenfile = new GetScreenResponse.Builder()
                    //                        {
                    //                            FileBytes = pb.ByteString.CopyFrom(BMPScreen.JpgBytes)
                    //                        }.Build()
                    //                    }.Build()
                    //                }.Build();
                    //                SendMessage(screenresponse);

                    //                this.SendScreen = DateTime.Now;
                    //            }
                    //            //Console.WriteLine(this.Username);
                    //        }

                    //    }
                    //    break;
                    case chat.MSG.Logout_Request:
                    {
                        Message rsp_msg = new Message.Builder()
                        {
                            MsgType   = MSG.Logout_Response,
                            Sequence  = recv_msg.Sequence,
                            SessionId = this.SessionId,
                            Response  = new Response.Builder()
                            {
                                Result       = true,
                                LastResponse = true
                            }.Build()
                        }.Build();

                        SendMessage(rsp_msg);

                        Login = false;
                        var friendNotification = BuildFriendNotification(false);
                        Users.instance().forall((User otherUser) =>
                            {
                                if (otherUser != this && otherUser.login)
                                {
                                    otherUser.SendMessage(friendNotification);
                                }
                            });
                    }
                    break;

                    case chat.MSG.Keepalive_Request:
                    {
                        Message rsp_msg = new Message.Builder()
                        {
                            MsgType   = MSG.Keepalive_Response,
                            Sequence  = recv_msg.Sequence,
                            SessionId = this.SessionId,
                            Response  = new Response.Builder()
                            {
                                Result       = true,
                                LastResponse = true
                            }.Build()
                        }.Build();

                        SendMessage(rsp_msg);
                    }
                    break;

                    case chat.MSG.Get_Friends_Request:
                    {
                        GetFriendsResponse.Builder friends = new GetFriendsResponse.Builder();
                        Users.instance().forall((User usr) =>
                            {
                                Friend friend = new Friend.Builder()
                                {
                                    Name   = pb.ByteString.CopyFromUtf8(usr.Username),
                                    Online = usr.Login
                                }.Build();

                                friends.AddFriends(friend);
                            });

                        Message rsp_msg = new Message.Builder()
                        {
                            MsgType   = MSG.Get_Friends_Response,
                            Sequence  = recv_msg.Sequence,
                            SessionId = this.SessionId,
                            Response  = new Response.Builder()
                            {
                                Result       = true,
                                LastResponse = true,
                                GetFriends   = friends.Build()
                            }.Build()
                        }.Build();

                        SendMessage(rsp_msg);
                    }
                    break;

                    case chat.MSG.Send_Message_Request:
                    {
                        Message rsp_msg = new Message.Builder()
                        {
                            MsgType   = MSG.Send_Message_Response,
                            Sequence  = recv_msg.Sequence,
                            SessionId = this.SessionId,
                            Response  = new Response.Builder()
                            {
                                Result       = true,
                                LastResponse = true
                            }.Build()
                        }.Build();

                        SendMessage(rsp_msg);

                        Message text_msg = new Message.Builder()
                        {
                            MsgType      = MSG.Message_Notification,
                            Sequence     = 0xffffffff,
                            Notification = new Notification.Builder()
                            {
                                Msg = new MessageNotification.Builder()
                                {
                                    Sender    = pb.ByteString.CopyFromUtf8(Username),
                                    Text      = recv_msg.Request.SendMessage.Text,
                                    Timestamp = DateTime.Now.ToString()
                                }.Build()
                            }.Build()
                        }.Build();

                        if (recv_msg.Request.SendMessage.HasReceiver)
                        {
                            string receiver = recv_msg.Request.SendMessage.Receiver.ToStringUtf8();

                            Users.instance().forall((User usr) =>
                                {
                                    if (usr.Username.Equals(receiver))
                                    {
                                        usr.SendMessage(text_msg);
                                    }
                                });
                        }
                        else
                        {
                            Users.instance().forall((User usr) =>
                                {
                                    usr.SendMessage(text_msg);
                                });
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("ParserMessage Exception:{0}", e.Message);
                }
            }
            catch (SocketException sockectex)
            {
                Console.WriteLine("OnReadBody SockectException:{0}", sockectex.Message);
                Users.instance().DelUser(this);
            }
            catch (Exception ex)
            {
                Console.WriteLine("OnReadBody Exception:{0}", ex.Message);
            }
        }