public void SwitchToOpaqueMode(WebSocketBase webSocket) { Debug.Assert(webSocket != null, "'webSocket' MUST NOT be NULL."); Debug.Assert(_outputStream != null, "'m_OutputStream' MUST NOT be NULL."); Debug.Assert(_outputStream.InternalHttpContext != null, "'m_OutputStream.InternalHttpContext' MUST NOT be NULL."); Debug.Assert(_outputStream.InternalHttpContext.Response != null, "'m_OutputStream.InternalHttpContext.Response' MUST NOT be NULL."); Debug.Assert(_outputStream.InternalHttpContext.Response.SentHeaders, "Headers MUST have been sent at this point."); Debug.Assert(!_inOpaqueMode, "SwitchToOpaqueMode MUST NOT be called multiple times."); if (_inOpaqueMode) { throw new InvalidOperationException(); } _webSocket = webSocket; _inOpaqueMode = true; _readEventArgs = new HttpListenerAsyncEventArgs(webSocket, this); _readEventArgs.Completed += s_OnReadCompleted; _writeEventArgs = new HttpListenerAsyncEventArgs(webSocket, this); _writeEventArgs.Completed += s_OnWriteCompleted; if (NetEventSource.Log.IsEnabled()) { NetEventSource.Associate(this, webSocket); } }
/// <summary> /// Called when a redirection is requested. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnRedirect(WebSocketBase ws, MRoundRedirect message) { if (ProcessGameMessage(message) && Client.Remove(this)) { Client.JoinGame(message.Redirect.Id); } }
/// <summary> /// Called when a challenge is reloaded. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnChallengeReload(WebSocketBase ws, MReload message) { if (_disposing) { return; } if (challengeCon != null && challengeCon.IsConnected()) { //Disconnect challenge socket log.ConditionalDebug("Disconnecting challenge socket."); challengeCon.DisconnectAsync().Wait(); } if (challengeLocation != null) { Task <Round> roundTask = GetRound(challengeLocation); roundTask.Wait(); Round round = roundTask.Result; if (round != null) { if (gameCons.TryGetValue(round.Game.Url.Socket, out LilaGame gameCon)) { return; } JoinGame(round.Game); } } }
/// <summary> /// Called when seeks are reloaded. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnReloadSeeks(WebSocketBase ws, MReloadSeeks message) { LilaRequest lr = new LilaRequest(new Uri(LilaRoutes.LobbySeeks, UriKind.Relative), Culture); lr.Cookies.Add(lobbyCon.GetCookies()); lr.Get(LilaRequest.ContentType.Json).ContinueWith(SetHooks); }
public PimpWebSocket(WebSocketBase socket) { Socket = socket; socket.MessageReceived += socket_MessageReceived; socket.Error += OnErrorOccurred; socket.Closed += OnSocketClosed; }
public HttpListenerAsyncEventArgs(WebSocketBase webSocket, WebSocketHttpListenerDuplexStream stream) : base() { m_WebSocket = webSocket; m_CurrentStream = stream; InitializeOverlapped(); }
public void SwitchToOpaqueMode(WebSocketBase webSocket) { Contract.Assert(webSocket != null, "'webSocket' MUST NOT be NULL."); Contract.Assert(m_OutputStream != null, "'m_OutputStream' MUST NOT be NULL."); Contract.Assert(m_OutputStream.InternalHttpContext != null, "'m_OutputStream.InternalHttpContext' MUST NOT be NULL."); Contract.Assert(m_OutputStream.InternalHttpContext.Response != null, "'m_OutputStream.InternalHttpContext.Response' MUST NOT be NULL."); Contract.Assert(m_OutputStream.InternalHttpContext.Response.SentHeaders, "Headers MUST have been sent at this point."); Contract.Assert(!m_InOpaqueMode, "SwitchToOpaqueMode MUST NOT be called multiple times."); if (m_InOpaqueMode) { throw new InvalidOperationException(); } m_WebSocket = webSocket; m_InOpaqueMode = true; m_ReadEventArgs = new HttpListenerAsyncEventArgs(webSocket, this); m_ReadEventArgs.Completed += s_OnReadCompleted; m_WriteEventArgs = new HttpListenerAsyncEventArgs(webSocket, this); m_WriteEventArgs.Completed += s_OnWriteCompleted; if (WebSocketBase.LoggingEnabled) { Logging.Associate(Logging.WebSockets, this, webSocket); } }
/// <summary> /// Called when a redirection is requested. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnRedirect(WebSocketBase ws, MRoundRedirect message) { if (message.Redirect != null) { JoinGame(message.Redirect.Id); } }
/// <summary> /// Called when a new hook is created. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnNewHook(WebSocketBase ws, MNewHook message) { lock (hookLock) { hooks.Add(message.Hook); Events.FireEventAsync(Events._onNewHook, new HookEvent(this, message.Hook)); } }
private static void ThrowIfSessionHandleClosed(WebSocketBase webSocket) { if (webSocket.SessionHandle.IsClosed) { throw new WebSocketException(WebSocketError.InvalidState, SR.Format(SR.net_WebSockets_InvalidState_ClosedOrAborted, webSocket.GetType().FullName, webSocket.State)); } }
/// <summary> /// Called when game pings back. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnGamePong(WebSocketBase ws, MPong message) { TimeSpan lag = DateTime.Now.Subtract(_gamePing.LastSent); ClientLag = lag.TotalMilliseconds; Players = message.Players; Games = message.Games; }
/// <summary> /// Called when a game event is received. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnGameEvent(WebSocketBase ws, MGameMessages message) { for (int i = 0; i < message.Data.Count; i++) { if (socket != null) { socket.InvokeMessage(message.Data[i]); } } }
/// <summary> /// Called when the game ends. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnEnd(WebSocketBase ws, MEnd message) { ProcessGameMessage(message); if (Client != null) { Client.Remove(this); } OnGameEnd?.Invoke(this, new LilaGameEvent(Client, this)); }
/// <summary> /// Called when the game needs to be reloaded. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnReload(WebSocketBase ws, MReload message) { if (ProcessGameMessage(message)) { if (message.Data?.Type == "rematchOffer") { AcceptRematch(); } } }
/// <summary> /// Called when hooks are updated. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnHooks(WebSocketBase ws, MHooks message) { lock (hookLock) { hooks.Clear(); for (int i = 0; i < message.HookList.Length; i++) { hooks.Add(message.HookList[i]); Events.FireEventAsync(Events._onNewHook, new HookEvent(this, message.HookList[i])); } } }
public void SwitchToOpaqueMode(WebSocketBase webSocket) { Contract.Assert(webSocket != null, "'webSocket' MUST NOT be NULL."); Contract.Assert(!m_InOpaqueMode, "SwitchToOpaqueMode MUST NOT be called multiple times."); m_WebSocket = webSocket; m_InOpaqueMode = true; m_ReadEventArgs = new SocketAsyncEventArgs(); m_ReadEventArgs.UserToken = this; m_ReadEventArgs.Completed += s_OnReadCompleted; m_WriteEventArgs = new SocketAsyncEventArgs(); m_WriteEventArgs.UserToken = this; m_WriteEventArgs.Completed += s_OnWriteCompleted; }
internal static void WebSocketGetAction(WebSocketBase webSocket, ActionQueue actionQueue, Interop.WebSocket.Buffer[] dataBuffers, ref uint dataBufferCount, out Action action, out BufferType bufferType, out IntPtr actionContext) { Debug.Assert(webSocket != null, "'webSocket' MUST NOT be NULL or INVALID."); Debug.Assert(webSocket.SessionHandle != null && !webSocket.SessionHandle.IsInvalid, "'webSocket.SessionHandle' MUST NOT be NULL or INVALID."); Debug.Assert(dataBufferCount >= 0, "'dataBufferCount' MUST NOT be negative."); Debug.Assert((dataBuffers == null && dataBufferCount == 0) || (dataBuffers != null && dataBufferCount == dataBuffers.Length), "'dataBufferCount' MUST MATCH 'dataBuffers.Length'."); action = Action.NoAction; bufferType = BufferType.None; actionContext = IntPtr.Zero; IntPtr dummy; ThrowIfSessionHandleClosed(webSocket); int errorCode; try { errorCode = Interop.WebSocket.WebSocketGetAction(webSocket.SessionHandle, actionQueue, dataBuffers !, ref dataBufferCount, out action, out bufferType, out dummy, out actionContext); } catch (ObjectDisposedException innerException) { throw ConvertObjectDisposedException(webSocket, innerException); } ThrowOnError(errorCode); webSocket.ValidateNativeBuffers(action, bufferType, dataBuffers !, dataBufferCount); Debug.Assert(dataBufferCount >= 0); Debug.Assert((dataBufferCount == 0 && dataBuffers == null) || (dataBufferCount <= dataBuffers !.Length)); }
public void Stop() { if (wsb != null) { threadReConnect.Abort(); threadReConnect = null; ThreadRecordPriceDiff.Abort(); ThreadRecordPriceDiff = null; wsb.stop(); wsb = null; wss = null; } }
/// <summary> /// Initializes a new instance of the <see cref="LilaSocket"/> class. /// </summary> /// <param name="name">The name or identifier of the socket.</param> /// <param name="impl">The underlying implementation to use.</param> internal LilaSocket(string name, ResourceType impl) { switch (impl) { case ResourceType.Task: socket = new TaskSocket(name); break; case ResourceType.Thread: socket = new ThreadSocket(name); break; } socket.Cookies = new CookieCollection(); }
/// <summary> /// Called when tournaments are updated. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnTournaments(WebSocketBase ws, MTournaments message) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(message.Html); List <TournamentHtmlEntry> ids = new List <TournamentHtmlEntry>(); var docNode = doc.DocumentNode; if (docNode.ChildNodes.Count >= 2) { var tableEntries = docNode.ChildNodes[1].ChildNodes; for (int i = 0; i < tableEntries.Count; i++) { if (tableEntries[i].Name == "tr" && tableEntries[i].ChildNodes.Count >= 2 && tableEntries[i].ChildNodes[1].ChildNodes.Count >= 2) { var hrefNode = tableEntries[i].ChildNodes[1].ChildNodes[1]; string tournamentLocation = hrefNode.GetAttributeValue("href", null); if (tournamentLocation != null) { int sIndex = tournamentLocation.LastIndexOf('/'); if (sIndex != -1) { string tournamentId = tournamentLocation.Substring(sIndex + 1); string tournamentName = hrefNode.InnerText.Trim(); TournamentHtmlEntry entry = new TournamentHtmlEntry { Id = tournamentId, Name = tournamentName }; ids.Add(entry); } } } } } message.TournamentEntries = ids; Events.FireEventAsync(Events._onTournaments, new TournamentsEvent(this, ids)); }
internal static void WebSocketReceive(WebSocketBase webSocket) { Debug.Assert(webSocket != null, "'webSocket' MUST NOT be NULL or INVALID."); Debug.Assert(webSocket.SessionHandle != null && !webSocket.SessionHandle.IsInvalid, "'webSocket.SessionHandle' MUST NOT be NULL or INVALID."); ThrowIfSessionHandleClosed(webSocket); int errorCode; try { errorCode = Interop.WebSocket.WebSocketReceive(webSocket.SessionHandle, IntPtr.Zero, IntPtr.Zero); } catch (ObjectDisposedException innerException) { throw ConvertObjectDisposedException(webSocket, innerException); } ThrowOnError(errorCode); }
internal static void WebSocketSendWithoutBody(WebSocketBase webSocket, BufferType bufferType) { Contract.Assert(webSocket != null, "'webSocket' MUST NOT be NULL or INVALID."); Contract.Assert(webSocket.SessionHandle != null && !webSocket.SessionHandle.IsInvalid, "'webSocket.SessionHandle' MUST NOT be NULL or INVALID."); ThrowIfSessionHandleClosed(webSocket); int errorCode; try { errorCode = WebSocketSendWithoutBody_Raw(webSocket.SessionHandle, bufferType, IntPtr.Zero, IntPtr.Zero); } catch (ObjectDisposedException innerException) { throw ConvertObjectDisposedException(webSocket, innerException); } ThrowOnError(errorCode); }
public void SwitchToOpaqueMode(WebSocketBase webSocket) { Contract.Assert(webSocket != null, "'webSocket' MUST NOT be NULL."); Contract.Assert(!m_InOpaqueMode, "SwitchToOpaqueMode MUST NOT be called multiple times."); if (m_InOpaqueMode) { throw new InvalidOperationException(); } m_WebSocketConnection = BaseStream as WebSocketConnection; if (m_WebSocketConnection != null && m_IsFastPathAllowed) { if (WebSocketBase.LoggingEnabled) { Logging.Associate(Logging.WebSockets, this, m_WebSocketConnection); } m_WebSocketConnection.SwitchToOpaqueMode(webSocket); m_InOpaqueMode = true; } }
public void Start() { if (wsb == null) { wss = new BuissnesServiceImpl(); wsb = new WebSocketBase(SelectedUrl, wss); wsb.start(); //var dict = new Dictionary<string, string>(); //dict.Add("api_key", ConfigInfo.apiKey); //wsb.send("{\"event\":\"login\",\"parameters\":{\"api_key\":\"" + ConfigInfo.apiKey + "\",\"sign\":\"" + SignParams(dict) + "\"}}");//登录 //wsb.send("{'event':'addChannel','channel':'ok_sub_futureusd_btc_ticker_this_week'}"); //wsb.send("{'event':'addChannel','channel':'ok_sub_futureusd_btc_ticker_next_week'}"); //wsb.send("{'event':'addChannel','channel':'ok_sub_futureusd_btc_ticker_quarter'}"); ChangeChannel(WSChannelChangeType.Add, WSChannel.ThisWeekChannel); ChangeChannel(WSChannelChangeType.Add, WSChannel.NextWeekChannel); if (threadReConnect == null) { threadReConnect = new Thread(new ThreadStart(Reconnect)) { IsBackground = true }; threadReConnect.Start(); } if (ThreadRecordPriceDiff == null) { ThreadRecordPriceDiff = new Thread(new ThreadStart(LogPriceDiff)) { IsBackground = true }; ThreadRecordPriceDiff.Start(); } } }
/// <summary> /// Called when hooks need to be synchronized. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnHookSync(WebSocketBase ws, MHookSync message) { lock (hookLock) { string[] sync = message.GetSynchronized(); for (int i = hooks.Count - 1; i >= 0; i--) { bool remove = true; for (int j = 0; j < sync.Length; j++) { if (hooks[i].Id.Equals(sync[j])) { remove = false; break; } } if (remove) { hooks.RemoveAt(i); } } } }
internal static void WebSocketCompleteAction(WebSocketBase webSocket, IntPtr actionContext, int bytesTransferred) { Debug.Assert(webSocket != null, "'webSocket' MUST NOT be NULL or INVALID."); Debug.Assert(webSocket.SessionHandle != null && !webSocket.SessionHandle.IsInvalid, "'webSocket.SessionHandle' MUST NOT be NULL or INVALID."); Debug.Assert(actionContext != IntPtr.Zero, "'actionContext' MUST NOT be IntPtr.Zero."); Debug.Assert(bytesTransferred >= 0, "'bytesTransferred' MUST NOT be negative."); if (webSocket.SessionHandle.IsClosed) { return; } try { Interop.WebSocket.WebSocketCompleteAction(webSocket.SessionHandle, actionContext, (uint)bytesTransferred); } catch (ObjectDisposedException) { } }
/// <summary> /// Called when hooks are removed. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnHooksRemoved(WebSocketBase ws, MRemovedHooks message) { lock (hookLock) { string[] ids = message.GetRemoved(); for (int i = hooks.Count - 1; i >= 0; i--) { bool remove = false; for (int j = 0; j < ids.Length; j++) { if (hooks[i].Id.Equals(ids[j])) { remove = true; break; } } if (remove) { hooks.RemoveAt(i); } } } }
private static WebSocketException ConvertObjectDisposedException(WebSocketBase webSocket, ObjectDisposedException innerException) { return(new WebSocketException(WebSocketError.InvalidState, SR.Format(SR.net_WebSockets_InvalidState_ClosedOrAborted, webSocket.GetType().FullName, webSocket.State), innerException)); }
/// <summary> /// Called when challenges are updated. /// </summary> /// <param name="ws">The websocket.</param> /// <param name="message">The message.</param> private void OnChallenges(WebSocketBase ws, Messages.MChallenges message) { }
public HttpListenerAsyncEventArgs(WebSocketBase webSocket, WebSocketHttpListenerDuplexStream stream) : base() { _webSocket = webSocket; _currentStream = stream; }