void RecvreceiveSock_OnReceived(CSerialDriver driver, string strMessage) { if (!Monitor.TryEnter(_keylock, TimeSpan.FromSeconds(1))) { SystemLogger.Log(Level.Exception, "Monitor.TryEnter Timeout 1 second.", receiveSock.Name + "_Driver"); } try { for (int i = 0; i < strMessage.Length; i++) { switch (strMessage[i]) { case (char)0x02: _sbPacket = new StringBuilder(); break; case (char)0x03: OnRecieveMessage.BeginInvoke(_sbPacket.ToString(), null, null); break; default: _sbPacket.Append(strMessage[i]); break; } } } catch (Exception ex) { SystemLogger.Log(Level.Exception, ex.Message, receiveSock.Name + "_Driver"); } finally { Monitor.Exit(_keylock); } }
private void _hwsClient_DataReceived(object sender, DataReceivedEventArgs e) { var strMsg = Encoding.UTF8.GetString(GZip.Decompress(e.Data)); if (strMsg.Contains("state.update") || strMsg.Contains("state.updateall") || strMsg.Contains("pong")) { return; } if (strMsg.Contains("status") && strMsg.Contains("success")) { var updateModel = JSONHelper.ConvertToObject <HotbitUpdateModel>(strMsg); if (updateModel.id == SUB_REQ_ID) { OnConnectionOpened?.Invoke(this, this.SubItemName); } else if (updateModel.id == SUB_AUTH_ID) { var authModel = this.SubModel as AuthSubscribeModel; authModel.reqList.ForEach(p => _hwsClient.Send(p.ToJson())); } return; } //_logger.Info($"recieve@{strMsg}@{Title}"); if (this.SubscribeType == "ASSET" && strMsg.Contains("result")) { strMsg = strMsg.Replace("error", "method").Replace("null", "\"asset.update\"").Replace("result", "params"); strMsg = strMsg.Replace("\"params\":", "\"params\":[").Replace("}}", "}}]"); strMsg = strMsg.Replace("}, ", "}},{"); } OnRecieveMessage?.Invoke(this, strMsg); }
/// <summary> /// Zajmuje sie sprawdzaniem czy cos jest na gniezdzie, wczytywaniem naglowka /// oraz calych pakietow /// </summary> private void go() { try { GaduPacketHeader header = new GaduPacketHeader(); // naglowek while (true) { if (!tcpClient.Connected || tcpClient.GetStream().DataAvailable == false || work == false) { // jezeli na gniezdzie nie ma 8 pelnych bajtow lub gniazdo nie jest podlaczone to Thread.Sleep(sleepTime); // czekamy na nowe pakiety continue; // i przechodzimy do nastepnej iteracji } header.read(tcpClient.GetStream(), 8); // czytamy naglowek if (OnPacket != null) { OnPacket.BeginInvoke(header, null, null); } switch (header.Type) { case GaduPacketConstans.GG_RECV_MSG: // ktos przyslal nam wiadomosc GaduPacketRecieveMessage msg = new GaduPacketRecieveMessage(); msg.read(tcpClient.GetStream(), header.Length); // wczytujemy wiadomosc if (OnRecieveMessage != null) // jezeli ktos podstawil swoja funkcje pod event { OnRecieveMessage.BeginInvoke(msg, null, null); // to ja wywolujemy podstawiajac jako argument pakiet z wiadomoscia } break; case GaduPacketConstans.GG_SEND_MSG_ACK: // odebralismy pakiet potwierdzajacy wiadomosc GaduPacketMessageAck ack = new GaduPacketMessageAck(); ack.read(tcpClient.GetStream(), header.Length); // wczytaj pakiet if (OnRecieveMessageAck != null) { OnRecieveMessageAck.BeginInvoke(ack, null, null); // wywolaj event } break; case GaduPacketConstans.GG_PONG: // otrzymalismy pakiet GG_PONG z servera if (OnPong != null) { OnPong.BeginInvoke(null, null); } break; case GaduPacketConstans.GG_LOGIN_OK: // otrzymalismy pakiet informujacy nas o tym ze logowanie sie powiodlo if (header.Length == 1) { tcpClient.GetStream().ReadByte(); // jeden bajt, ktory nie zawsze musi sie pojawic, przeznaczenie nie poznane } if (OnLoginOK != null) { OnLoginOK.BeginInvoke(null, null); // wywolaj event } break; case GaduPacketConstans.GG_NEED_EMAIL: // server chce abysmy uzupelnili e-mail w naszych danych if (OnLoginNeedEmail != null) { OnLoginNeedEmail.BeginInvoke(null, null); // wywolaj event } break; case GaduPacketConstans.GG_LOGIN_FAILED: // logowanie sie nie powiodlo if (OnLoginFailed != null) { OnLoginFailed.BeginInvoke(null, null); // wywolaj event } break; case GaduPacketConstans.GG_NOTIFY_REPLY77: // otrzymalismy pakiet mowiacy o tym ze ktos zmienil status GaduPacketNotifyReplay77 gaduPacketNotifyReplay77; while (header.Length > 0) // najprawdopodobnie bedzie to zlepek pakietow, a wiec bedziemy je wczytywac po kolei { gaduPacketNotifyReplay77 = new GaduPacketNotifyReplay77(); gaduPacketNotifyReplay77.read(tcpClient.GetStream(), header.Length); header.Length -= gaduPacketNotifyReplay77.getSize(); if (OnGaduNotifyReplay != null) { OnGaduNotifyReplay.BeginInvoke(gaduPacketNotifyReplay77, null, null); // dla danego pakiety wywolaj event } } break; case GaduPacketConstans.GG_STATUS77: // ktos zmienil status na gg GaduPacketStatus77 gaduPacketStatus77 = new GaduPacketStatus77(); gaduPacketStatus77.read(tcpClient.GetStream(), header.Length); // wczytujemy pakiet if (OnGaduStatus != null) { OnGaduStatus.BeginInvoke(gaduPacketStatus77, null, null); // wywolujemny event } break; case GaduPacketConstans.GG_USERLIST_REPLY: GaduPacketUserListRequest gaduPacketUserlistReplay = new GaduPacketUserListRequest(); gaduPacketUserlistReplay.read(tcpClient.GetStream(), header.Length); if (OnUserListReplay != null) { OnUserListReplay.BeginInvoke(gaduPacketUserlistReplay, null, null); } break; case GaduPacketConstans.GG_PUBDIR50_REPLY: GaduPacketPubDir50 gaduPacketPubDir50 = new GaduPacketPubDir50(); gaduPacketPubDir50.read(tcpClient.GetStream(), header.Length); if (OnPubDirReplay != null) { OnPubDirReplay.BeginInvoke(gaduPacketPubDir50, null, null); } break; case GaduPacketConstans.GG_DISCONNECTING: if (OnGaduDisconnecting != null) { OnGaduDisconnecting.BeginInvoke(null, null); } break; case GaduPacketConstans.GG_DISCONNECTING2: if (OnGaduDisconnecting != null) { OnGaduDisconnecting.BeginInvoke(null, null); } break; default: // jezeli otrzymalismy jakis pakiet, ktory jeszcze nie jest obslugiwany przez biblioteke byte[] packet = new byte[header.Length]; for (int i = 0; i < header.Length; i++) // to wczytaj tylko pakiet z gniazda i nic z nim nie rob { packet[i] = (byte)tcpClient.GetStream().ReadByte(); } if (OnUnhandledPacket != null) { OnUnhandledPacket.BeginInvoke(header, packet, null, null); } break; } } } catch (ThreadAbortException) { // nie zadreczajmy uzytkownika tym wyjatkiem, w 99% powodowany przez Thread.Abort() } catch (Exception e) { if (Gadu.GaduCriticalError() == false) { throw new GaduRecieverException(e.Message); } } }