/// <summary> /// Fills the object with values from the data byte. /// </summary> /// <param name="b">The byte value.</param> protected override void FromByte(byte b) { IncomingMessageType incomingMessageType = IncomingMessageType.SmsDeliver; if (incomingMessageType == IncomingMessageType.SmsDeliver) { this.moreMessages = (b & 4) == 0; this.StatusReportRequested = (b & 32) > 0; this.userDataHeaderPresent = (b & 64) > 0; this.replyPathExists = (b & 128) > 0; return; } else { throw new ArgumentException("Not an SMS-DELIVER message."); } }
/// <summary> /// Fills the object with values from the data byte. /// </summary> /// <param name="b">The byte value.</param> protected override void FromByte(byte b) { IncomingMessageType incomingMessageType = IncomingMessageType.SmsStatusReport; if ((b & 2) > 0) { incomingMessageType = IncomingMessageType.SmsStatusReport; } if (incomingMessageType == IncomingMessageType.SmsStatusReport) { this.userDataHeaderPresent = (b & 4) > 0; this.moreMessages = (b & 8) == 0; this.qualifier = (b & 16) > 0; return; } else { throw new ArgumentException("Not an SMS-STATUS-REPORT message."); } }
/// <summary> /// Decodes an incoming SMS PDU stream. /// </summary> /// <param name="pdu">The PDU string to decode.</param> /// <param name="includesSmscData">Specify true if the PDU data contains an SMSC header, otherwise false.</param> /// <param name="actualLength">The size of the PDU data in bytes, not counting the SMSC header. Set to -1 if unknown.</param> /// <returns>An <see cref="T:GSMCommunication.PDUDecoder.IncomingSmsPdu" /> object representing the decoded message.</returns> public static IncomingSmsPdu Decode(string pdu, bool includesSmscData, int actualLength) { if (pdu != string.Empty) { int num = 0; if (includesSmscData) { int num1 = num; num = num1 + 1; byte num2 = BcdWorker.GetByte(pdu, num1); if (num2 > 0) { num = num + num2; } } int num3 = num; IncomingMessageType messageType = IncomingSmsPdu.GetMessageType(BcdWorker.GetByte(pdu, num3)); IncomingMessageType incomingMessageType = messageType; switch (incomingMessageType) { case IncomingMessageType.SmsDeliver: { return(new SmsDeliverPdu(pdu, includesSmscData, actualLength)); } case IncomingMessageType.SmsStatusReport: { return(new SmsStatusReportPdu(pdu, includesSmscData, actualLength)); } } throw new NotSupportedException(string.Concat("Message type ", messageType.ToString(), " recognized, but not supported by the SMS decoder.")); } else { throw new ArgumentException("pdu must not be an empty string."); } }
public static bool TryDeserializeIncoming(string message, out IncomingMessageType messageType, out ArraySegment <JsonElement> args) => TryDeserialize(message, out messageType, out args);
public static string Serialize(IncomingMessageType messageType, params object[] args) => Serialize(messageType.ToString(), args);
internal NetQueueItem(Connection connection, IncomingMessageType type, IncomingMessage incomingMessage = null) { Connection = connection; Type = type; Message = incomingMessage; }
/// <summary> /// Executes the router to find an appropriate processor for the incoming message /// </summary> /// <param name="type">The type of incoming message from Slack.</param> /// <param name="context">The <see cref="HttpContext"/> for the current request.</param> /// <param name="httpClient">The <see cref="HttpClient"/> instance to use for any network calls (to promote reuse)</param> /// <returns>A task that represents the execution of the router.</returns> /// <exception cref="ArgumentNullException">Throws when <paramref name="context"/> or <paramref name="httpClient"/> is <c>null</c>.</exception> public async Task ProcessAsync(IncomingMessageType type, HttpContext context, HttpClient httpClient) { _ = context ?? throw new ArgumentNullException(nameof(context)); _ = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); // Ensure it's a POST coming from Slack if (context.Request.Method != "POST") { context.Response.StatusCode = 404; return; } AppResponse response = null; var isRespondable = false; try { switch (type) { case IncomingMessageType.Command: isRespondable = true; response = await ProcessCommandAsync(context); break; case IncomingMessageType.Action: isRespondable = true; response = await ProcessActionAsync(context); break; case IncomingMessageType.Event: await ProcessEventAsync(context); break; } } catch (Exception e) { var message = $"An exception occurred while processing this {type}"; _logger.LogError(e, message, null); if (isRespondable) { if (response == null) { response = new AppResponse(); } response.ImmediateMessage = SlackMessage.CreateErrorMessage($"message: {e.Message}"); response.ResponseUrlMessages.Clear(); context.Response.StatusCode = 200; } else { context.Response.StatusCode = 500; } } if (!isRespondable) { return; } if (response.ImmediateMessage != null) { // Respond immediately with a message // Per Slack docs, this keeps the original message in the channel // See https://api.slack.com/slash-commands#delayed_responses_and_multiple_responses context.Response.ContentType = "application/json"; await context.Response.WriteAsync(response.ImmediateMessage.ToJson()); } if (response.ResponseUrlMessages.Count > 0 && !string.IsNullOrEmpty(response.response_url)) { foreach (var message in response.ResponseUrlMessages) { // Send the message back to the response_url // Per Slack docs, this replaces the original message in the channel // See https://api.slack.com/slash-commands#delayed_responses_and_multiple_responses var requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(response.response_url)) { Content = new StringContent(message.ToJson(), Encoding.UTF8, "application/json") }; try { var result = await httpClient.SendAsync(requestMessage); if (!result.IsSuccessStatusCode) { throw new Exception($"Failed to post to Slack: Status Code \"{result.StatusCode}\" ({(int)result.StatusCode})."); } } catch (Exception e) { // Something happened posting back to the response_url, so log the error _logger.LogError(e, "An exception occurred while sending a message to the response_url", null); } } } }
static void Main(string[] args) { NetPeerConfiguration config = new NetPeerConfiguration("projectorigin"); config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest); config.Port = 14242; // Create and start server NetServer server = new NetServer(config); server.Start(); Console.WriteLine("Game Server starts"); //Schedule initial sending of position updates double nextSendUpdates = NetTime.Now; GameStatus gameStatus = GameStatus.Initial; int clientDoneCounter = 0; //Generate a map and wait client to connect //InternalMap map = GenerateMap(); int mapSeed = 1000; Random rand = new Random(); mapSeed = rand.Next(); //Run until escape is pressed while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape) { NetIncomingMessage msg; while ((msg = server.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryRequest: // // Server received a discovery request from a client; send a discovery response (with no extra data attached) // NetOutgoingMessage com = server.CreateMessage(); //byte[] mapData = Serializer<InternalMap>.SerializeObject(map); switch (server.ConnectionsCount) { case 0: com.Write((int)PlayerId.Green); com.Write((int)mapSeed); //Write map seed break; case 1: com.Write((int)PlayerId.Red); com.Write((int)mapSeed); //Write map seed gameStatus = GameStatus.Receive; break; } server.SendDiscoveryResponse(com, msg.SenderEndpoint); Console.WriteLine("Connect to: " + msg.SenderEndpoint); break; case NetIncomingMessageType.Data: if (gameStatus == GameStatus.Receive) { // // The client sent input to the server // IncomingMessageType imt = (IncomingMessageType)msg.ReadByte(); if (imt == IncomingMessageType.DataPlayerInfo) { int wayPointCounter = msg.ReadInt32(); List <PlayerInfo> wayPoints = new List <PlayerInfo>(); for (int i = 1; i <= wayPointCounter; ++i) { Vector3 tempPos; tempPos.X = msg.ReadFloat(); tempPos.Y = msg.ReadFloat(); tempPos.Z = msg.ReadFloat(); float tempOrientation; tempOrientation = msg.ReadFloat(); PlayerInfo wayPoint = new PlayerInfo(tempPos, tempOrientation); wayPoints.Add(wayPoint); } playerInfoDict[msg.SenderEndpoint] = wayPoints; Console.WriteLine("Receive message from" + msg.SenderEndpoint); clientDoneCounter++; if (clientDoneCounter == 2) { gameStatus = GameStatus.Sending; } } } break; } // // send position updates 30 times per second // if (gameStatus == GameStatus.Sending) { // Yes, it's time to send position updates // for each player... foreach (NetConnection player in server.Connections) { if (!playerInfoDict.ContainsKey(player.RemoteEndpoint)) { playerInfoDict[player.RemoteEndpoint] = new List <PlayerInfo>(); } } foreach (NetConnection player in server.Connections) { //send information about every other player(not including self) foreach (NetConnection otherPlayer in server.Connections) { if (player.RemoteEndpoint == otherPlayer.RemoteEndpoint) { continue; } // send position update about 'otherPlayer' to 'player' NetOutgoingMessage om = server.CreateMessage(); om.Write((byte)OutgoingMessageType.DataOtherPlayerInfo); om.Write(playerInfoDict[otherPlayer.RemoteEndpoint].Count); foreach (PlayerInfo playerInfo in playerInfoDict[otherPlayer.RemoteEndpoint]) { om.Write(playerInfo.position.X); om.Write(playerInfo.position.Y); om.Write(playerInfo.position.Z); om.Write(playerInfo.orientation); } //send message server.SendMessage(om, player, NetDeliveryMethod.ReliableOrdered); } } gameStatus = GameStatus.Receive; clientDoneCounter = 0; } } } server.Shutdown("server exiting"); }
public IncomingMessageEventArgs(IncomingMessageType message) { this.message = message; }
public void IncomingMessage(IncomingMessageType message) { OnMessageReceived(new IncomingMessageEventArgs(message)); }
internal InnolasMessageEventArgs(string _data, IncomingMessageType _errorLevel) { this.data = _data; this.errorLevel = _errorLevel; }
/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { KeyboardState keyboard = Keyboard.GetState(); if (keyboard.IsKeyDown(Keys.Enter) && prevKeyboardState.IsKeyUp(Keys.Enter) && this.game.GetGameStatus() == Shooter.GameStatus.Simulation && this.game.GetGameStatus() != Shooter.GameStatus.Sending && this.game.GetGameStatus() != Shooter.GameStatus.Receive) { this.game.SetGameStatus(Shooter.GameStatus.Sending); } this.prevKeyboardState = keyboard; if (this.game.GetGameStatus() == Shooter.GameStatus.Sending) { if (this.game.bMapIsReady) { // // If there's input; send it to server // NetOutgoingMessage om = client.CreateMessage(); om.Write((byte)OutgoingMessageType.DataPlayerInfo); om.Write(this.game.path.GetWayPoints().Count); foreach (WayPoint point in this.game.path.GetWayPoints()) { Vector3 tempPos = point.CenterPos;//this.game.gamePlayer.GetPlayerPosition(); float tempOri = this.game.gamePlayer.GetPlayerOrientation(); om.Write(tempPos.X); om.Write(tempPos.Y); om.Write(tempPos.Z); om.Write(tempOri); } client.SendMessage(om, NetDeliveryMethod.ReliableOrdered); } this.game.SetGameStatus(Shooter.GameStatus.Receive); } if (this.game.GetGameStatus() == Shooter.GameStatus.Receive || this.game.GetGameStatus() == Shooter.GameStatus.MainMenu) { //read message NetIncomingMessage msg; while ((msg = client.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryResponse: //just connect to first server discovered client.Connect(msg.SenderEndpoint); playerId = (Project_Origin.Player.PlayerId)(msg.ReadInt32()); int mapSeed = msg.ReadInt32(); this.game.bMapIsReady = true; this.game.BuildGameComponents(mapSeed); this.game.gamePlayer.SetPlayId(); break; case NetIncomingMessageType.Data: //server sent a position update IncomingMessageType imt = (IncomingMessageType)msg.ReadByte(); if (imt == IncomingMessageType.DataOtherPlayerInfo) { opponentWaypoint.Clear(); int wayPointCounter = msg.ReadInt32(); for (int i = 1; i <= wayPointCounter; ++i) { otherPlayerInfo.position.X = msg.ReadFloat(); otherPlayerInfo.position.Y = msg.ReadFloat(); otherPlayerInfo.position.Z = msg.ReadFloat(); otherPlayerInfo.orientation = msg.ReadFloat(); Console.WriteLine("{0} {1} {2}", otherPlayerInfo.position.X, otherPlayerInfo.position.Y, otherPlayerInfo.position.Z); opponentWaypoint.Add(new WayPoint(this.game.GraphicsDevice, new Vector3(otherPlayerInfo.position.X, otherPlayerInfo.position.Y, otherPlayerInfo.position.Z))); } } this.game.gamePlayer.Path.OpponentWayPoints = opponentWaypoint; this.game.SetGameStatus(Shooter.GameStatus.Start); this.game.gamePlayer.StartToMove(); break; } } } base.Update(gameTime); }