// This demonstrates how to perform a graceful disconnect, where data sending/receiving // is allowed to complete before the client is disconnected. // In this demo the server begins the graceful disconnect process // It is also possible for a client to initiate a graceful disconnect using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Client { class Program { static void Main(string[] args) { // mnClientConnected return value constants const int C_NOT_CONNECTED = 0; const int C_CONNECTED = 1; const int C_NO_SEND = 2; const int C_NO_RECV = 3; const int C_NO_SEND_RECV = 4; // Connect to server int iReturn = 0; Console.WriteLine("DarkNet version: " + mn.GetVersion()); Int64 RecvPacket = mn.CreatePacket(); Int64 SendPacket = mn.CreatePacket(); mn.SetMemorySize(SendPacket,1024); mn.Start(1,0); mn.DisableUDP(0); mn.EnableGracefulDisconnect(0); iReturn = mn.Connect(0,"127.0.0.1",6565,"",0,5,true); switch(iReturn) { case(1): Console.WriteLine("Connected to server"); break; case(0): Console.WriteLine("Connection timed out"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); return; break; case(-1): Console.WriteLine("An unknown error occurred whilst trying to connect"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); return; break; case(-2): Console.WriteLine("The connection request was rejected because the server is full"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); return; break; } // Main loop int SendTimer = 0; int SendFreq = 2000; int iClientStatus; do { // Use less CPU Thread.Sleep(1); iClientStatus = mn.ClientConnected(0,0); switch(iClientStatus) { // Server has begun to gracefully disconnect us // No further data can be received but we can use // what has already been received, and is stored in // the packet queue case(C_NO_RECV): Console.WriteLine("mn.ClientConnected: " + iClientStatus); // Deal with data received and in packet queue while(mn.RecvTCP(0,RecvPacket,0) > 0) { Console.WriteLine("Final TCP data received"); } // Send final data // When mn.SendTCP returns data has been sent // and so cleanup after this will NOT cause the // send to fail mn.AddString(SendPacket,"Goodbye server, I will miss you!",0,true); mn.SendTCP(0,SendPacket,0,false,true); // Shutdown // No further data can be sent // mn.ClientConnected will use mn.DisconnectClient/mn.Finish // to clean up the instance and will return 0, completing // the disconnect process mn.ShutdownClient(0,0); Console.WriteLine("Received all data, sent final message to server and fully disconnected"); break; } if(iClientStatus == C_CONNECTED) { // TCP packets iReturn = mn.RecvTCP(0,RecvPacket,0); if(iReturn > 0) { Console.WriteLine("New TCP packet received"); } // Send messages every SendFreq ms if ((System.DateTime.Now.Millisecond + (System.DateTime.Now.Second * 1000) + (System.DateTime.Now.Minute * 60 * 1000)) - SendTimer > SendFreq) { string str = System.DateTime.Now.Date + ", " + System.DateTime.Now.TimeOfDay; mn.AddString(SendPacket,str,0,true); Console.WriteLine("String sent to server: " + str); // Send packet mn.SendTCP(0,SendPacket,0,false,true); // Update timer SendTimer = (System.DateTime.Now.Millisecond + (System.DateTime.Now.Second * 1000) + (System.DateTime.Now.Minute * 60 * 1000)); } } }while(iClientStatus != C_NOT_CONNECTED); // Cleanup and exit Console.WriteLine("Disconnected from server!"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); mn.Finish(-1); } } }