/// <summary> /// Send a DCC Chat request to a remote user and wait for a connection /// using timeout period specified. /// </summary> /// <remarks> /// <para>If the user does not respond within the timeout period the DccChatSession /// will stop listening for a connection. The sesssion instance created then becomes /// invalid. This methods must be called again and a new instance created in order to /// initiate a try again. /// </para> /// <para> /// This method should be called from within a try/catch block /// in case there are socket errors. /// </para> /// </remarks> /// <param name="dccUserInfo">A collection of information about the remote user.</param> /// <param name="listenIPAddress">The IP address that will be sent to the remote user. It must /// be in dotted quad format (i.e. 192.168.0.2). If the client is behind a NAT system then /// this should be the address of that system and not the local host.</param> /// <param name="listenPort">The TCP/IP port to listen on</param> /// <param name="timeout">How long to wait for a response in milliseconds. /// A value of zero will disable the timeout.</param> public static DccChatSession Request(DccUserInfo dccUserInfo, string listenIPAddress, int listenPort, long timeout) { Debug.WriteLineIf(DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name + "] DccChatSession::Request()"); //Create session object DccChatSession session = new DccChatSession(dccUserInfo); session.listenPort = listenPort; //Start session Thread session.thread = new Thread(new ThreadStart(session.Listen)); session.thread.Name = session.ToString(); session.thread.Start(); //Send Chat request to remote user session.SendChatRequest(listenIPAddress, listenPort); //Start timeout thread if timeout > 0 if (timeout > 0) { Timer timer = new Timer( new TimerCallback(session.TimerExpired), session, timeout, 0); Debug.WriteLineIf(DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name + "] DccChatSession::Request timeout thread started"); } return(session); }
/// <summary> /// When another a remote user has sent a chat request, this /// method is called to accept the request and /// start a chat session with that user. /// </summary> /// <remarks> /// This method should be called from within a try/catch block /// because there are many things that could prevent this /// connection attempt from succeeding. /// </remarks> /// <param name="dccUserInfo">A collection of information about the remote user.</param> /// <returns>The DccChatSession instance for this session.</returns> public static DccChatSession Accept(DccUserInfo dccUserInfo) { Debug.WriteLineIf(DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name + "] DccChatSession::Accept()"); DccChatSession session = new DccChatSession(dccUserInfo); //Start session Thread session.thread = new Thread(new ThreadStart(session.Connect)); session.thread.Name = session.ToString(); session.thread.Start(); return(session); }
public void OnChatMessageReceived( DccChatSession session, string message ) { Console.WriteLine("Chat Message Received: " + message); if( message == "die" ) { //Close() closes the socket and stops the session Thread session.Close(); } else { //Use sendMessage() to send text to the remote user. Newlines //will automatically be appended to the message before sending. //Here, like the other examples, we simply echo back any text sent to us. session.SendMessage( message ); } }
/// <summary> /// Send a DCC Chat request to a remote user and wait for a connection /// using timeout period specified. /// </summary> /// <remarks> /// <para>If the user does not respond within the timeout period the DccChatSession /// will stop listening for a connection. The sesssion instance created then becomes /// invalid. This methods must be called again and a new instance created in order to /// initiate a try again. /// </para> /// <para> /// This method should be called from within a try/catch block /// in case there are socket errors. /// </para> /// </remarks> /// <param name="dccUserInfo">A collection of information about the remote user.</param> /// <param name="listenIPAddress">The IP address that will be sent to the remote user. It must /// be in dotted quad format (i.e. 192.168.0.2). If the client is behind a NAT system then /// this should be the address of that system and not the local host.</param> /// <param name="listenPort">The TCP/IP port to listen on</param> /// <param name="timeout">How long to wait for a response in milliseconds. /// A value of zero will disable the timeout.</param> public static DccChatSession Request( DccUserInfo dccUserInfo, string listenIPAddress, int listenPort, long timeout ) { Debug.WriteLineIf( DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name +"] DccChatSession::Request()"); //Create session object DccChatSession session = new DccChatSession( dccUserInfo ); session.listenPort = listenPort; //Start session Thread session.thread = new Thread(new ThreadStart( session.Listen ) ); session.thread.Name = session.ToString(); session.thread.Start(); //Send Chat request to remote user session.SendChatRequest( listenIPAddress, listenPort ); //Start timeout thread if timeout > 0 if( timeout > 0 ) { Timer timer = new Timer( new TimerCallback( session.TimerExpired ), session, timeout, 0); Debug.WriteLineIf( DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name +"] DccChatSession::Request timeout thread started"); } return session; }
/// <summary> /// When another a remote user has sent a chat request, this /// method is called to accept the request and /// start a chat session with that user. /// </summary> /// <remarks> /// This method should be called from within a try/catch block /// because there are many things that could prevent this /// connection attempt from succeeding. /// </remarks> /// <param name="dccUserInfo">A collection of information about the remote user.</param> /// <returns>The DccChatSession instance for this session.</returns> public static DccChatSession Accept( DccUserInfo dccUserInfo ) { Debug.WriteLineIf( DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name +"] DccChatSession::Accept()"); DccChatSession session = new DccChatSession( dccUserInfo ); //Start session Thread session.thread = new Thread(new ThreadStart( session.Connect ) ); session.thread.Name = session.ToString(); session.thread.Start(); return session; }
public void OnChatRequestTimeout( DccChatSession session ) { //When a session times out it is closed and cannot be reused. //We must create a new one. Console.WriteLine("Chat request timed out."); }
private void InitiateChat( string nick ) { //Initiating a chat is only slightly more complicated than accepting one. //First we must create a DccUserInfo instance which contains the information //on how to send the request, i.e. what connection to use and who should receive it. DccUserInfo userInfo = new DccUserInfo( connection, nick ); //Now we need to send our IP address and the port we will be listening on. Those //behind a NAT firewall should read the API docs for more information on what to use //as an IP address. The port can be any value above 1024. Thresher does not manage these //so it is up to us not to pick a port already in use. //Request() can take other arguments including a crypto protocol. See the API //docs for more information. chatSession = DccChatSession.Request( userInfo, "192.168.0.11", 50000, 30000 ); //Add delegates RegisterDelegates(); //At this point one of two things will happen: the remote user will accept //and OnChatSessionOpened() will be called or the user will not respond //and OnChatRequestTimeout() will be called instead. //The new session Thread is now running and waiting for a response... }
public void OnDccChatRequest( DccUserInfo dccUserInfo ) { //This event will be raised if someone is attempting to initiate a Chat session. //DccUserInfo extends UserInfo and contains some additional networking information //as well as the Connection instance used to send the request. //There are lots of things which can go wrong with DCC so the creation of any //type of DCC session should always be in a try/catch block. try { Console.WriteLine("Chat request from " + dccUserInfo.Nick ) ; //Untill we call accept on the session it will not be opened so we could ignore //it if we wanted to. The Accept parameters are the same ones kindly provided //by the ChatRequest event. chatSession = DccChatSession.Accept( dccUserInfo ); //Now add delegate to the session RegisterDelegates(); //All DCC sessions are direct connections to other users and do not pass through //the IRC server. The IRC server is only a means of sending session requests. DCC //sessions also run in their own separate Threads, one for each session instance. //When a session is closed the Thread ends. //Since we don't want to be notified of any more chat request we will //remove ourselves form the list. It is always important to remove yourself from //events when they are no longer needed. DccListener.DefaultInstance.OnDccChatRequest -= new DccChatRequestEventHandler( OnDccChatRequest ); //Now we wait for messages... } catch( Exception e ) { Console.WriteLine("Exception handling Chat request: " + e); } }
public void OnChatSessionOpened( DccChatSession session ) { Console.WriteLine("Chat session opened."); }
public void OnChatSessionClosed( DccChatSession session ) { Console.WriteLine("Chat session closed."); //When the chat session closes then close the IRC connection to. connection.Disconnect("No time for chat."); }