private void MQ_Connect(ServerLink sl, NC_ResultCallBackConnect onResult, params AMFObjectProperty[] amfProperties) { MQ_Close(); netConnectionState = NetConnectionState.Connecting; serverLink = sl; bool success = MQInternal_MakeConnection(amfProperties); if (success) { netConnectionState = NetConnectionState.Connected; } if (success) { success = false; // Now we have to wait for the "onConnect" event to be send by the wowza server, before we can say the // connection was successful. We do this for a max of 7 seconds, if no "onConnect" then // disconnect and say failed DateTime timeStamp = DateTime.Now.AddMilliseconds(onConnectWaitTimeoutMS); while (!success && (timeStamp - DateTime.Now).TotalMilliseconds > 0) { if (netConnectionState == NetConnectionState.Connected) { if (MQInternal_IsConnected) { if (MQInternal_DataInSocket) { RTMPPacket packet = null; ReadPacket(out packet); // When we get an Invoke while connecting this is the "connect" event! // where we have been waiting for. if (packet.PacketType == PacketType.Invoke) { success = true; } HandleClientPacket(packet); } } else { // There is no connection anymore break; } } // don't use cpu 100% if (!success) { Thread.Sleep(10); } } //while // Close connection when failed! if (!success || (DateTime.Now - timeStamp).TotalMilliseconds > 0) { #if SSL if (sslStream != null) { try { sslStream.Close(); sslStream.Dispose(); } catch { } } sslStream = null; #endif if (tcpSocket != null) { try { tcpSocket.Shutdown(SocketShutdown.Both); tcpSocket.Close(); } catch { } } tcpSocket = null; // temporary before we set the state to disconnected! netConnectionState = NetConnectionState.Connecting; dtNetConnectionKeepAlive = DateTime.Now; } } if (onResult != null) { // this is delegate given when connecting (used by Mediaplayer class) // makes event OnConnect not needed DoNC_ResultCallBackConnectEvent(onResult, success); } // Now we do a "global" onConnect if (success && OnConnect != null) { MQ_RTMPMessage message = new MQ_RTMPMessage(); message.MethodCall = MethodCall.OnEventCallUserCode; message.Params = new object[] { OnConnect, this }; AddOnEventUserCallCodeToPump(message); } if (!success) { // Set state netConnectionState = NetConnectionState.Disconnected; } }
private void MQ_Close() { lock (lockVAR) { // delete all streams if there were any bool wasConnected = (tcpSocket != null && netConnectionState == NetConnectionState.Connected); if (MQInternal_IsConnected) { foreach (NetStream netStream in netStreamsList) { if (netStream.Stream_ID >= 0) { MQ_SendDeleteStream(netStream.Stream_ID); // this will generate result packets but we're cclosing the // connect. Really we should wait to get all result before disconnecting } } //foreach } #if SSL if (sslStream != null) { try { sslStream.Close(); sslStream.Dispose(); } catch { } } sslStream = null; #endif if (tcpSocket != null) { try { tcpSocket.Shutdown(SocketShutdown.Both); tcpSocket.Close(); } catch { } } tcpSocket = null; netConnectionState = NetConnectionState.Disconnected; dtNetConnectionKeepAlive = DateTime.MaxValue; receiveTimeoutMS = RTMPConst.TIMEOUT_RECEIVE; InChunkSize = RTMPConst.RTMP_DEFAULT_CHUNKSIZE; outChunkSize = RTMPConst.RTMP_DEFAULT_CHUNKSIZE; bwCheckCounter = 0; clientBW = 2500000; clientBW2 = 2; serverBW = 2500000; bytesReadTotal = 0; lastSentBytesRead = 0; numInvokes = 0; for (int i = 0; i < RTMPConst.RTMP_CHANNELS; i++) { vecChannelsIn[i] = null; vecChannelsOut[i] = null; channelTimestamp[i] = 0; } //for methodCallDictionary.Clear(); dMethodLookup.Clear(); if (wasConnected) { foreach (NetStream netStream in netStreamsList) { netStream.internal_NetStreamDisconnectNotify(); // } //foreach if (OnDisconnect != null) { MQ_RTMPMessage message = new MQ_RTMPMessage(); message.MethodCall = MethodCall.OnEventCallUserCode; message.Params = new object[] { OnDisconnect, this }; AddOnEventUserCallCodeToPump(message); } } // reset is for the next connect DisconnectEventSend = false; } //lock }
public void SetConnectionState(NetConnectionState state) { ConnectionState = state; }