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; } }
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); } }
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); }
public void Send(Message message) { byte[] bson = BSON.Serialize <Message>(message); Send(bson); }
public void OnClientConnected(object sender, EventArgs args) { Message readyMessage = new ReadyMessage(ObjectId.Empty); Send(BSON.Serialize <Message>(readyMessage)); }