public void OnRecievedData( IAsyncResult ar ) { Socket sock = (Socket)ar.AsyncState; try { int nBytesRead = 0; int nBytesRec = sock.EndReceive( ar ); if( nBytesRec > 0 ) { do { // build the flap header flap_header fh = new flap_header(); fh.asterisk = (char)m_byBuff[nBytesRead+0]; fh.frametype = (byte)m_byBuff[nBytesRead+1]; byte [] byteTemp = new byte[2]; byteTemp[1] = m_byBuff[nBytesRead+2]; byteTemp[0] = m_byBuff[nBytesRead+3]; fh.seqno = BitConverter.ToInt16(byteTemp,0); byteTemp[1] = m_byBuff[nBytesRead+4]; byteTemp[0] = m_byBuff[nBytesRead+5]; fh.datalen = BitConverter.ToInt16(byteTemp,0); #region do-while-loop // we're talking to the authentication server if (!m_bAuthenticated) { if (fh.frametype == 4) { byteTemp = new byte[fh.datalen]; Array.Copy(m_byBuff,nBytesRead+6,byteTemp,0,fh.datalen); Hashtable loginInfo = GetTLVHash(byteTemp,fh.datalen); // authentication error if (loginInfo["8"] != null) { TLV tlv = (TLV)loginInfo[8]; if (OnError != null) OnError("Error Code ("+Encoding.ASCII.GetString(tlv.byteData,0,2)+")"); m_socket.Shutdown(SocketShutdown.Both); m_socket.Close(); m_bTCPConnected = false; } // success! else if (loginInfo["6"] != null) { // set the BOS info TLV tlv = (TLV)loginInfo["5"]; string strTemp = Encoding.ASCII.GetString(tlv.byteData,0,tlv.length); string [] strData = Regex.Split(strTemp,"(:)"); m_strBOSServer = strData[0]; m_strBOSPort = strData[2]; // set the auth cookie TLV cookie = (TLV)loginInfo["6"]; m_authCookie = new byte[cookie.length]; Array.Copy(cookie.byteData,0,m_authCookie,0,cookie.length); // shut down and connect to BOS m_bAuthenticated = true; m_bTCPConnected = false; m_socket.Shutdown(SocketShutdown.Both); m_socket.Close(); Connect(); } } } else { // SNAC data is always on flap channel 2 if (fh.frametype == 2) { byte [] rawData = new byte[fh.datalen]; Array.Copy(m_byBuff,nBytesRead+6,rawData,0,fh.datalen); // build the snac SNAC snac = new SNAC(); snac.family = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData,0)); snac.subtype = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData,2)); snac.flags = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData,4)); snac.requestid = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(rawData,6)); // get the snac data byte [] snacData = new byte[fh.datalen-10]; Array.Copy(rawData,10,snacData,0,fh.datalen-10); // send it on its way DispatchSNACData(snac,snacData); } } #endregion nBytesRead += fh.datalen + FLAP_HEADER_LENGTH; } while (nBytesRead < nBytesRec); SetupRecieveCallback (sock); } else if (!m_bDCOnPurpose) HandleReconnect(); // looks like we disconnect, so reconnect } catch( Exception ex ) { // looks like the connection dropped if (!m_bDCOnPurpose) HandleReconnect(); } }
public void OnRecievedData(IAsyncResult ar) { Socket sock = (Socket)ar.AsyncState; try { int nBytesRead = 0; int nBytesRec = sock.EndReceive(ar); if (nBytesRec > 0) { do { // build the flap header flap_header fh = new flap_header(); fh.asterisk = (char)m_byBuff[nBytesRead + 0]; fh.frametype = (byte)m_byBuff[nBytesRead + 1]; byte [] byteTemp = new byte[2]; byteTemp[1] = m_byBuff[nBytesRead + 2]; byteTemp[0] = m_byBuff[nBytesRead + 3]; fh.seqno = BitConverter.ToInt16(byteTemp, 0); byteTemp[1] = m_byBuff[nBytesRead + 4]; byteTemp[0] = m_byBuff[nBytesRead + 5]; fh.datalen = BitConverter.ToInt16(byteTemp, 0); #region do-while-loop // we're talking to the authentication server if (!m_bAuthenticated) { if (fh.frametype == 4) { byteTemp = new byte[fh.datalen]; Array.Copy(m_byBuff, nBytesRead + 6, byteTemp, 0, fh.datalen); Hashtable loginInfo = GetTLVHash(byteTemp, fh.datalen); // authentication error if (loginInfo["8"] != null) { TLV tlv = (TLV)loginInfo[8]; if (OnError != null) { OnError("Error Code (" + Encoding.ASCII.GetString(tlv.byteData, 0, 2) + ")"); } m_socket.Shutdown(SocketShutdown.Both); m_socket.Close(); m_bTCPConnected = false; } // success! else if (loginInfo["6"] != null) { // set the BOS info TLV tlv = (TLV)loginInfo["5"]; string strTemp = Encoding.ASCII.GetString(tlv.byteData, 0, tlv.length); string [] strData = Regex.Split(strTemp, "(:)"); m_strBOSServer = strData[0]; m_strBOSPort = strData[2]; // set the auth cookie TLV cookie = (TLV)loginInfo["6"]; m_authCookie = new byte[cookie.length]; Array.Copy(cookie.byteData, 0, m_authCookie, 0, cookie.length); // shut down and connect to BOS m_bAuthenticated = true; m_bTCPConnected = false; m_socket.Shutdown(SocketShutdown.Both); m_socket.Close(); Connect(); } } } else { // SNAC data is always on flap channel 2 if (fh.frametype == 2) { byte [] rawData = new byte[fh.datalen]; Array.Copy(m_byBuff, nBytesRead + 6, rawData, 0, fh.datalen); // build the snac SNAC snac = new SNAC(); snac.family = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData, 0)); snac.subtype = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData, 2)); snac.flags = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData, 4)); snac.requestid = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(rawData, 6)); // get the snac data byte [] snacData = new byte[fh.datalen - 10]; Array.Copy(rawData, 10, snacData, 0, fh.datalen - 10); // send it on its way DispatchSNACData(snac, snacData); } } #endregion nBytesRead += fh.datalen + FLAP_HEADER_LENGTH; } while (nBytesRead < nBytesRec); SetupRecieveCallback(sock); } else if (!m_bDCOnPurpose) { HandleReconnect(); // looks like we disconnect, so reconnect } } catch (Exception ex) { // looks like the connection dropped if (!m_bDCOnPurpose) { HandleReconnect(); } } }