public static NetMessage EncodeRequest(NetClient client) { if (client == null) throw new ArgumentNullException("client"); NetMessage msg = new NetMessage(); msg.m_type = NetMessageType.Discovery; msg.Write(client.Configuration.ApplicationIdentifier); return msg; }
public IPlayer(String dns, int port) { NetAppConfiguration myConfig = new NetAppConfiguration("MMO Mahjong"); m_log = new NetLog(); Init(); m_client = new NetClient(myConfig, m_log); m_client.Connect(dns, port); m_client.StatusChanged += new EventHandler<NetStatusEventArgs>(StatusChanged); Application.Idle += new EventHandler(ApplicationLoop); }
/// <summary> /// Initializes the network engine. /// </summary> /// <param name="strName">The name of the user</param> public SpiderClient(String strName) { spiderName = strName; spiderConfig = new NetAppConfiguration("ymfas",DEFAULT_PORT); spiderLog = new NetLog(); spiderLog.OutputFileName = "YMFAS Net Log (Port " + DEFAULT_PORT + ").html"; localSessionQueue = new Queue(50); messageQueue = new Queue(50); disconnectQueue = new Queue(50); spiderNet = new NetClient(spiderConfig,spiderLog); }
public Program() { NetAppConfiguration myConfig = new NetAppConfiguration("MMO Mahjong", 14242); NetLog log = new NetLog(); NetClient Client = new NetClient(myConfig, log); Client.Connect("localhost", 14242); // takes IP number or hostnameHow to detect connects/disconnects Client.StatusChanged += new EventHandler<NetStatusEventArgs>(StatusChanged);// to track connect/disconnect etc bool keepGoing = true; while (keepGoing) { Client.Heartbeat(); NetMessage msg; while ((msg = Client.ReadMessage()) != null) { string str = msg.ReadString(); // <- for example System.Console.WriteLine("You got a packet containing: " + str); Thread.Sleep(1); } } }
public void ClientClicked() { this.Window.Title = "Trying To Connect...."; //Since this is a client we dont have to specify a port here nac = new NetAppConfiguration("ZombieSim"); Log = new NetLog(); //Im Using this to show how to log files, So we pick that we want //to ignore nothing, Change this depending on what your //wanting to log for various debugging. Log.IgnoreTypes = NetLogEntryTypes.None; //Specify if you want to enable output to a file which we do. Log.IsOutputToFileEnabled = true; //If we output to a file we have to pick a filename to output it to. Log.OutputFileName = "Client.html"; //We Initiate the client here, it has not connected to the server yet. client = new NetClient(nac, Log); //We Want to Log Events that are fired from the client client.StatusChanged += new EventHandler<NetStatusEventArgs>(client_StatusChanged); //Finally we connect to the server, Specify the IP Address and //port if your wanting to connect to xxx.xxx.xxx.xxx //we would change this line to "192.168.1.1",12345 client.Connect("192.168.0.200", 12345); }
/// <summary> /// Connection heartbeat, normally called from NetClient.Heartbeat() or NetServer.Heartbeat() /// Calling it manually may interfere with proper throttling /// </summary> public void Heartbeat(double now) { m_frameLength = now - m_lastHeartbeat; m_lastHeartbeat = now; // resend timed out saved packets for (int i = 0; i < m_savedReliableMessages.Length; i++) { if (m_savedReliableMessages[i] != null && m_savedReliableMessages[i].Count > 0) { foreach (NetMessage msg in m_savedReliableMessages[i]) { if (now > msg.m_nextResendTime) { // it might have been lost; resend it if (msg.m_numResends >= Configuration.NumResendsBeforeFailing) { m_parent.Log.Error("Dropping outgoing packet " + msg + "; maximum number of resends reached!"); } else { if (MessageResent != null) { MessageResent(this, new NetMessageEventArgs(msg)); } //m_parent.Log.Verbose("Resending " + msg + " @ " + now); m_unsentMessages.Enqueue(msg); float nextResend = (float)now + (msg.m_numResends < 1 ? Configuration.ResendFirstUnacknowledgedDelay : Configuration.ResendSubsequentUnacknowledgedDelay); msg.m_nextResendTime = nextResend; msg.m_numResends++; Statistics.MessagesResent++; } m_tmpRemoveList.Add(msg); // to be removed } } if (m_tmpRemoveList.Count > 0) { foreach (NetMessage rm in m_tmpRemoveList) { m_savedReliableMessages[i].Remove(rm); // remove it; it will be readded upon sending } m_tmpRemoveList.Clear(); } } } // send all messages (possibly forcing acks to be sent) SendUnsentMessages((m_forceExplicitAckTime != 0.0 && now > m_forceExplicitAckTime), (m_frameLength > 0.1 ? 0.1 : m_frameLength)); // throttling don't like unlikely long frames if (now - m_lastHeardFromRemote > Configuration.ConnectionTimeOut) { NetHandshake.SendDisconnected(m_parent, "Connection timed out", this); SetStatus(NetConnectionStatus.Disconnected, "Connection timed out"); return; } if (m_status == NetConnectionStatus.Connecting) { // keep sending connect packets if (now - m_lastSentHandshake > NetConstants.SendHandshakeFrequency) { if (now - m_firstSentHandshake > NetConstants.ConnectAttemptTimeout) { m_parent.Log.Info("Failed to connect; no answer from remote host!"); SetStatus(NetConnectionStatus.Disconnected, "Connect attempt timeout"); } else { NetClient client = m_parent as NetClient; if (client != null) { int bytesSent = NetHandshake.SendConnect(client, m_remoteEndpoint, m_savedConnectCustomData); client.ServerConnection.Statistics.PacketsSent++; client.ServerConnection.Statistics.MessagesSent++; client.ServerConnection.Statistics.BytesSent += bytesSent; m_lastSentHandshake = now; } else { NetHandshake.SendConnectResponse(m_parent, this, m_remoteEndpoint); m_lastSentHandshake = now; } } } return; } m_ping.Heartbeat(now); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //PList = new PlayerList(); MainForm = new ChatForm(); AppConfig = new Configuration(); IsAppHidden = AppConfig.RunMinimized; // Create a configuration for the client Program.ChatApplicationBusiness.Config = new NetAppConfiguration("BBTcpTest"); Program.BBBotBusiness = new BBBotBusiness(Program.ChatApplicationBusiness); // enable encryption; this key was generated using the 'GenerateEncryptionKeys' application Config.EnableEncryption( "AQABwV1rQ1NDWzkkhUsYtMiRGjyxSn/zI13tros2RlXHlW6PA7Cvw2nOyMhDmweM+" + "T/+FWzssAWLh8qd+YHaMCCFskVdaZfRzquZlNOs9mX/vtxkLrXgBUaJQD/XOuBeJF" + "o3RfAKA4uhrEr7Bk1iB3zJQW1bSHK7KmmEWzMErk6iA7c=", null); Log = new NetLog(); Log.IgnoreTypes = NetLogEntryTypes.Verbose; // Verbose; Log.IsOutputToFileEnabled = false; Log.OutputFileName = "clientlog.html"; // uncomment this if you want to start multiple instances of this process on the same computer //Log.OutputFileName = "clientlog" + System.Diagnostics.Process.GetCurrentProcess().Id + ".html"; //Log.LogEvent += new EventHandler<NetLogEventArgs>(Log_LogEvent); Client = new NetClient(Config, Log); Client.StatusChanged += new EventHandler<NetStatusEventArgs>(Client_StatusChanged); Application.Idle += new EventHandler(ApplicationLoop); //MainForm.TypingStateChanged += new EventHandler<ChatForm.TypingEvent>(MainForm_TypingStateChanged); CreateNotificationIcon(); Application.Run(MainForm); if (Client != null) Client.Shutdown("Application exiting"); if (notifyIconNA != null) notifyIconNA.Visible = false; }
internal static int SendConnect(NetClient client, IPEndPoint remoteEndpoint, byte[] customData) { if (client.Configuration.UsesEncryption) client.Log.Debug("Sending Connect containing key: " + Convert.ToBase64String(client.ServerConnection.m_encryption.SymmetricEncryptionKeyBytes)); else client.Log.Debug("Sending Connect - Unencrypted connection!"); NetMessage msg = new NetMessage(NetMessageType.Handshake, 3); msg.Write((byte)NetHandshakeType.Connect); msg.WriteSendStamp(); // encrypt symmetric key using server public key if (client.Configuration.UsesEncryption) { byte[] encryptedKey = client.ServerConnection.m_encryption.EncryptRSA(client.ServerConnection.m_encryption.SymmetricEncryptionKeyBytes); msg.Write((ushort)encryptedKey.Length); msg.Write(encryptedKey); } else { // request no encryption msg.Write((ushort)0); } if (customData == null || customData.Length < 1) { msg.Write7BitEncodedUInt(0); } else { msg.Write7BitEncodedUInt((uint)customData.Length); msg.Write(customData); } return client.SendSingleMessageAtOnce(msg, null, remoteEndpoint); }