コード例 #1
0
ファイル: SocketManager.cs プロジェクト: ZeegZag/ExchangeBot
 private static void TryReconnect(ChildSocket child, WebSocket webSocket)
 {
     try
     {
         try
         {
             webSocket.CloseAsync(WebSocketCloseStatus.InternalServerError, "Request could not be processed",
                                  CancellationToken.None).Wait(3000);
         }
         catch (Exception exception)
         {
         }
         ClientWebSocket socket = new ClientWebSocket();
         socket.ConnectAsync(new Uri("wss://" + child.Name + "/ws"), CancellationToken.None).Wait();
         var childSocket = new ChildSocket(socket, IdCounter++, child.Name, child.Owner);
         child.Owner.Add(childSocket);
         child.Dispose(true);
         KeepListening(childSocket);
         Console.WriteLine("Reconnected");
     }
     catch (Exception e)
     {
         Console.WriteLine("Reconnect failed: " + e.Message);
     }
 }
コード例 #2
0
ファイル: SocketManager.cs プロジェクト: ZeegZag/ExchangeBot
        private static async Task KeepListening(ChildSocket child)
        {
            var webSocket = child.Socket;
            var buffer    = new Byte[8192];
            var segment   = new ArraySegment <byte>(buffer);

            try
            {
                var content = await ReadSocket(webSocket, segment);

                while (content != null)
                {
                    var req = JsonConvert.DeserializeObject <RequestModel>(content);
                    if (TaskQueue.TryRemove(req.RequestId, out var task))
                    {
                        task.SetResult(req);
                    }
                    Array.Clear(buffer, 0, buffer.Length);
                    content = await ReadSocket(webSocket, buffer);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            child.IsDisposed = true;
            //try to reconnect
            if (isLocked)
            {
                if (child.isLocked)
                {
                    TryReconnect(child, webSocket);
                }
                else
                {
                    lock (child.SyncWriteObject)
                    {
                        TryReconnect(child, webSocket);
                    }
                }
            }
            else
            {
                if (child.isLocked)
                {
                    lock (Locker)
                    {
                        TryReconnect(child, webSocket);
                    }
                }
                else
                {
                    lock (Locker)
                        lock (child.SyncWriteObject)
                        {
                            TryReconnect(child, webSocket);
                        }
                }
            }


            try
            {
                //await webSocket.CloseAsync(WebSocketCloseStatus.InternalServerError, "Request could not be processed", CancellationToken.None);
            }
            catch (Exception exception)
            {
            }
            //lock (Locker)
            //ChildSocket.FindByWebsocket(webSocket)?.Dispose(false);
        }
コード例 #3
0
ファイル: SocketManager.cs プロジェクト: ZeegZag/ExchangeBot
        public static void ConnectAll(bool first)
        {
            try
            {
                StatusText = "Connecting...";
                Console.WriteLine("Connecting...");
                FailedConnections.Clear();
                var allSocketIps = Borsa.Sockets.ToList();
                lock (Locker)
                {
                    isLocked   = true;
                    StatusText = "Started connecting...";
                    //dc all children
                    int j = 1;
                    foreach (var child in Children.ToList())
                    {
                        //Children.Remove(child);
                        StatusText = "Disconnecting old sockets... " + j++;
                        try
                        {
                            child.Socket.CloseAsync(WebSocketCloseStatus.Empty, String.Empty, CancellationToken.None).Wait();
                        }
                        catch (Exception e)
                        {
                        }
                        //child.Dispose(true);
                    }
                    //dc aligned sockets

                    foreach (var kvp in ChildrenByBorsa.ToArray())
                    {
                        foreach (var child in kvp.Value)
                        {
                            try
                            {
                                child.Socket.CloseAsync(WebSocketCloseStatus.Empty, String.Empty, CancellationToken.None).Wait();
                            }
                            catch (Exception e)
                            {
                            }
                            //child.Dispose(false);
                        }
                    }
                    if (first)
                    {
                        //connect all children again
                        for (var i = 0; i < allSocketIps.Count; i++)
                        {
                            StatusText = $"Connecting... ({i + 1}/{allSocketIps.Count})";
                            var             socketIp = allSocketIps[i];
                            ClientWebSocket socket   = new ClientWebSocket();
                            try
                            {
                                socket.ConnectAsync(new Uri("wss://" + socketIp + "/ws"), CancellationToken.None)
                                .Wait();
                                var childSocket = new ChildSocket(socket, IdCounter++, socketIp, Children);
                                Children.Add(childSocket);
                                KeepListening(childSocket);
                            }
                            catch (Exception e)
                            {
                                FailedConnections.Add(socketIp);
                            }
                        }
                    }

                    /*
                     * //align sockets again
                     * foreach (var kvp in ChildrenByBorsa.ToArray())
                     * {
                     *  var oldList = kvp.Value.ToList();
                     *  //align new sockets
                     *  var listOfAlignedChildren = Children.Take(oldList.Count).ToList();
                     *  var newList = new List<ChildSocket>();
                     *  foreach (var child in listOfAlignedChildren)
                     *  {
                     *      Children.Remove(child);
                     *      newList.Add(child);
                     *      child.Owner = newList;
                     *  }
                     *
                     *  ChildrenByBorsa[kvp.Key] = newList;
                     *
                     *  //dc old aligned sockets
                     *  foreach (var child in oldList)
                     *  {
                     *      try
                     *      {
                     *          //child.Socket.CloseAsync(WebSocketCloseStatus.Empty, String.Empty, CancellationToken.None);
                     *      }
                     *      catch (Exception e)
                     *      {
                     *      }
                     *      child.Dispose(true);
                     *  }
                     * }
                     */
                }

                isLocked = false;

                StatusText = $"Connected to {allSocketIps.Count - FailedConnections.Count} sockets";
                Console.WriteLine("Connected");
            }
            catch (Exception e)
            {
                StatusText = e.ToString();
            }
        }