private void ProcessMessage(QueuedMessage queuedMessage) { Protocol.IncomingMessageBuffer buffer = new Protocol.IncomingMessageBuffer(queuedMessage.messageData); Protocol.MessageType messageType = (Protocol.MessageType)buffer.NextByte; switch (messageType) { case Protocol.MessageType.ID_PING_OPEN_CONNECTIONS: Protocol.PingOpenConnections message = new Protocol.PingOpenConnections(); if (!message.Parse(buffer)) { return; } Protocol.UnconnectedPongOpenConnections response = new Protocol.UnconnectedPongOpenConnections(); response.PingId = message.PingId; Network.UdpReceiver.Instance.Broadcast(response, queuedMessage.source); break; default: Client client = ConnectionHandler.Instance.GetClient(queuedMessage.source); if (client != null) { client.ProcessMessage(messageType, buffer); } break; } }
protected void OnRawMessageReceiveEvent(byte[] packet, Protocol.MessageType messageType) { OnRawMessageReceive?.Invoke(this, new RawMessageEventArgs(packet, messageType)); }
/// <summary> /// Set the Header /// </summary> /// <param name="header"></param> public void SetHeader(byte[] header) { Size = BitConverter.ToUInt16(header, 0); Type = (Protocol.MessageType)BitConverter.ToUInt16(header, 2); }
public RawMessageEventArgs(byte[] packet, Protocol.MessageType messageType) { Packet = packet; MessageType = messageType; }
private static async Task <string> SendMessageAndGetResponseAsync(string message, Protocol.MessageType messageType, Protocol.MessageType answerType, string address) { var wrappedMessage = Protocol.WrapMessage(message, messageType); string response; using (var streamSocket = new StreamSocket()) { var hostname = new HostName(address); Logger.Log("Attempting to connect to server..."); await streamSocket.ConnectAsync(hostname, Protocol.DefaultPort); Logger.Log("Sending message..."); await Protocol.SendMessageToStreamAsync(streamSocket.OutputStream, wrappedMessage); Logger.Log("Reading response..."); response = await Protocol.ReadMessageFromStreamAsync(streamSocket.InputStream); } var serverAnswerJson = Protocol.parseRawMessage(response); if (Protocol.GetMessageType(serverAnswerJson) != answerType) { throw new Exception("Server did not respond with correct answer type for client message."); } return(Protocol.GetMessageContents(serverAnswerJson)); }