public void HandleMessage(long sessionId, byte[] buffer, int offset, int length) { using (MemoryStream stream = new MemoryStream(buffer, offset, length)) { try { SimpleChat.MESSAGE_ID id = (SimpleChat.MESSAGE_ID)ProtoBuf.Serializer.DeserializeWithLengthPrefix <int>(stream, ProtoBuf.PrefixStyle.Fixed32); switch (id) { case SimpleChat.MESSAGE_ID.CMSG_HELLO: HandleMessage(sessionId, (SimpleChat.CMsgHello)_model.Deserialize(stream, null, typeof(SimpleChat.CMsgHello))); break; case SimpleChat.MESSAGE_ID.CMSG_CHAT: HandleMessage(sessionId, (SimpleChat.CMsgChat)_model.Deserialize(stream, null, typeof(SimpleChat.CMsgChat))); break; case SimpleChat.MESSAGE_ID.CMSG_BYE: HandleMessage(sessionId, (SimpleChat.CMsgBye)_model.Deserialize(stream, null, typeof(SimpleChat.CMsgBye))); break; } } catch (Exception) { } } }
void SendMessage(long sessionId, SimpleChat.MESSAGE_ID id, Object obj) { ChatClient client = null; _clients.TryGetValue(sessionId, out client); if (client != null) { client.SendObject(id, obj); } }
public void SendObject(SimpleChat.MESSAGE_ID id, Object obj) { lock (_sycStream) { _stream.Seek(0, SeekOrigin.Begin); ProtoBuf.Serializer.SerializeWithLengthPrefix <int>(_stream, (int)id, ProtoBuf.PrefixStyle.Fixed32); _model.Serialize(_stream, obj); Service.SendToSession(ID, _stream.GetBuffer(), 0, (int)_stream.Position, false); } }