Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Hide socket exception message / stop trying make new connect when there too many failure #234

Merged
merged 2 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions PoGo.NecroBot.CLI/BotDataSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace PoGo.NecroBot.CLI
{
public class BotDataSocketClient
{
private static Queue<object> events = new Queue<object>();
private const int POLLING_INTERVAL = 15000;
private static Queue<EncounteredEvent> events = new Queue<EncounteredEvent>();
private const int POLLING_INTERVAL = 10000;
public static void Listen(IEvent evt, Session session)
{
dynamic eve = evt;
Expand Down Expand Up @@ -57,6 +57,9 @@ private static string Serialize(dynamic evt)
return json;
}

private static int retries = 0;
static List<EncounteredEvent> processing = new List<EncounteredEvent>();

public static async Task Start(Session session, CancellationToken cancellationToken)
{

Expand All @@ -70,41 +73,66 @@ public static async Task Start(Session session, CancellationToken cancellationTo

using (var ws = new WebSocketSharp.WebSocket(socketURL))
{
ws.Log.Level = WebSocketSharp.LogLevel.Error;
ws.Log.Level = WebSocketSharp.LogLevel.Fatal;
ws.Log.Output = (logData, message) =>
{
//silenly, no log exception message to screen that scare people :)
};

//ws.OnMessage += (sender, e) =>
// Console.WriteLine("New message from controller: " + e.Data);

while (true)
{
try
{
if (retries++ == 5) //failed to make connection to server times contiuing, temporary stop for 10 mins.
{
session.EventDispatcher.Send(new WarnEvent()
{
Message = "Couldn't establish the connection to necro socket server, Bot will re-connect after 10 mins"
});

await Task.Delay(10 * 1000 * 60);
}

ws.Connect();
Logger.Write("Pokemon spawn point data service connection established.");
while (ws.ReadyState == WebSocketSharp.WebSocketState.Open)
if (ws.ReadyState == WebSocketSharp.WebSocketState.Open)
{
lock (events)
Logger.Write("Pokemon spawn point data service connection established.");
retries = 0;

while (ws.IsAlive)
{

while (events.Count > 0)
lock (events)
{
if (ws.ReadyState == WebSocketSharp.WebSocketState.Open)
while (events.Count > 0)
{
var item = events.Dequeue();
processing.Add(events.Dequeue());
}
}

while (processing.Count > 0)
{
if (ws.IsAlive)
{
var item = processing.FirstOrDefault();
var data = Serialize(item);
ws.Send($"42[\"pokemon\",{data}]");
processing.Remove(item);
await Task.Delay(processing.Count > 0 ? 3000 : 0);
}
}
}
await Task.Delay(POLLING_INTERVAL);
ws.Ping();

}
}
catch (IOException)
{
session.EventDispatcher.Send(new ErrorEvent
session.EventDispatcher.Send(new WarnEvent
{
Message = "The connection to the data sharing location server was lost."
Message = "Disconnect to necro socket. New connection will be established when service available..."
});
}
catch (Exception)
Expand Down
2 changes: 2 additions & 0 deletions PoGo.NecroBot.Logic/Event/EncounteredEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class EncounteredEvent : IEvent
public double ExpireTimestamp { get; set; }
public string SpawnPointId{ get; set; }
public string EncounterId { get; internal set; }
public string Move1 { get; set; }
public string Move2 { get; set; }
}
}
6 changes: 4 additions & 2 deletions PoGo.NecroBot.Logic/Tasks/CatchPokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ public static async Task Execute(ISession session,
Expires = expiredDate.ToUniversalTime(),
ExpireTimestamp = unixTimeStamp,
SpawnPointId = _spawnPointId,
EncounterId = _encounterId.ToString()
});
EncounterId = _encounterId.ToString(),
Move1 = PokemonInfo.GetPokemonMove1(encounteredPokemon).ToString(),
Move2 = PokemonInfo.GetPokemonMove2(encounteredPokemon).ToString(),
});

CatchPokemonResponse caughtPokemonResponse = null;
var lastThrow = CatchPokemonResponse.Types.CatchStatus.CatchSuccess; // Initializing lastThrow
Expand Down