Пример #1
0
        public MessageEventArgs(byte[] msg, string endOfMessage)
        {
            try
            {
                string beginning = Encoding.UTF8.GetString(msg.Take(5).ToArray());


                if (beginning == "<BOF>" && endOfMessage == "<EOF>")
                {
                    byte[] data = new byte[msg.Length - 10];
                    Buffer.BlockCopy(msg, 5, data, 0, msg.Length - 10);
                    string viewMessageTest = Encoding.UTF8.GetString(data);
                    message = BSON.Deserialize <Message>(data);
                    string json = message.ToJson();
                }
                else
                {
                    message = new DestroyedBuffer();
                }
            }
            catch (Exception ex)
            {
                message = new DestroyedBuffer();
                throw ex;
            }
        }
Пример #2
0
        public void ClientMessageReceived(object sender, AndroidMessageEventArgs args)
        {
            if (!(args.message is DestroyedBuffer))
            {
                SocketHandle clientHandle = (SocketHandle)sender;
                UserMessage  userMessage  = args.message as UserMessage;
                ObjectId     userId       = userMessage.userid;

                AddClient(userId, clientHandle);

                Message response = null;
                Console.WriteLine("[Received {0}]  {1}", userMessage.username, args.message.GetType().ToString());

                if (args.message is SyncRequest)
                {
                    SyncRequest syncRequest = args.message as SyncRequest;
                    response = syncRequest.RetrieveData();
                }
                else if (args.message is CreateUser)
                {
                    CreateUser newUserMessage = args.message as CreateUser;
                    response = newUserMessage.Create();
                }
                else if (args.message is AddLinkFeed)
                {
                    AddLinkFeed linkFeed = args.message as AddLinkFeed;
                    response = linkFeed.AddLink();
                }
                else if (args.message is AddTextUpdate)
                {
                    AddTextUpdate textUpdate = args.message as AddTextUpdate;
                    response = textUpdate.AddLink();
                }
                else if (args.message is ChangePassword)
                {
                    ChangePassword changePassword = args.message as ChangePassword;
                    response = changePassword.Start();
                }
                else if (args.message is ModifyItem)
                {
                    ModifyItem modifyItem = args.message as ModifyItem;
                    response = modifyItem.Modify();
                }
                else if (args.message is DeleteUser)
                {
                    DeleteUser deleteUser = args.message as DeleteUser;
                    response = deleteUser.Delete();
                }

                Console.WriteLine("\t[Sent {0}]  {1}", response.GetType().ToString(), userMessage.username);
                byte[] bson = BSON.Serialize <Message>(response);
                Send(clientHandle, bson);
            }
        }
Пример #3
0
        public void WaitSend(object state)
        {
            allowSend.Reset();

            if (messageQueue.Count == 0)
            {
                allowSend.WaitOne();
            }

            Message message = messageQueue.Take();

            byte[] bson = BSON.Serialize <Message>(message);
            Send(bson);
        }
Пример #4
0
 public void Send(Message message)
 {
     byte[] bson = BSON.Serialize <Message>(message);
     Send(bson);
 }
Пример #5
0
        public void OnClientConnected(object sender, EventArgs args)
        {
            Message readyMessage = new ReadyMessage(ObjectId.Empty);

            Send(BSON.Serialize <Message>(readyMessage));
        }