/// <summary> /// 初始化 /// </summary> public static void Init() { if (isInited == true) { return; } //Add an incoming packet handler using default SendReceiveOptions. // Multiple handlers for the same packet type will be executed in the order they are added. //注册接收到消息的处理器 //PacketType用于标志哪种类型的消息,客户端和服务器端协议商定,标志消息类型,可以自定义 //事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalIncomingPacketHandler <string>(PacketType, PacketHandlerAction); //客户端建立连接事件 //事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalConnectionEstablishHandler(ConnectionEstablishDelegate); //关闭连接事件 //事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalConnectionCloseHandler(ConnectionShutdownDelegate); //未处理的信息包处理 //事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler(UnmanagedPacketHandlerDelgatePointer); isInited = true; }
/// <summary> /// 开启监听 /// </summary> public static void Start() { //Add an incoming packet handler using default SendReceiveOptions. // Multiple handlers for the same packet type will be executed in the order they are added. //注册接收到消息的处理器 //PacketType用于标志哪种类型的消息,客户端和服务器端协议商定,标志消息类型,可以自定义 //事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalIncomingPacketHandler <string>(PacketType, PacketHandlerAction); //连接建立监听 事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalConnectionEstablishHandler(ConnectionEstablishDelegate); //连接关闭监听 事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalConnectionCloseHandler(ConnectionShutdownDelegate); //未处理的信息包处理 事件是在多线程中回调的,不能实现确定回调的线程 NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler(UnmanagedPacketHandlerDelgatePointer); //检查连接的保活的定时器时间间隔 单位秒 默认是30s //Connection.ConnectionKeepAlivePollIntervalSecs = 180; //在serverPort上开始监听消息,并返回监听的列表 List <ConnectionListenerBase> listenerList = Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, serverPort)); Console.WriteLine("服务器监听下列地址:"); foreach (var listenerItem in listenerList) { Console.WriteLine(listenerItem.LocalListenEndPoint); } }
public static void RunExample() { //Ensure we use the null serializer for unmanaged connections SendReceiveOptions options = new SendReceiveOptions <NullSerializer>(); //Setup listening for incoming unmanaged UDP broadcasts UDPConnectionListener listener = new UDPConnectionListener(options, ApplicationLayerProtocolStatus.Disabled, UDPOptions.None); Connection.StartListening(listener, new IPEndPoint(IPAddress.Any, 10000)); //Add a packet handler for unmanaged connections NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((packetHeader, connection, incomingBytes) => { Console.WriteLine("Received {0} bytes from {1}", incomingBytes.Length, connection.ConnectionInfo.RemoteEndPoint); }); //Generate some test data to broadcast byte[] dataToSend = new byte[] { 1, 2, 3, 4 }; //Create an unmanaged packet manually and broadcast the test data //In future this part of the API could potentially be improved to make it clearer using (Packet sendPacket = new Packet("Unmanaged", dataToSend, options)) UDPConnection.SendObject <byte[]>(sendPacket, new IPEndPoint(IPAddress.Broadcast, 10000), options, ApplicationLayerProtocolStatus.Disabled); Console.WriteLine("Client done!"); Console.ReadKey(); }
private static void RegisterPacketHandler() { NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { //TODO Packet Handler } ); NetworkComms.AppendGlobalIncomingPacketHandler <string>("AuthDBConnection", (header, connection, content) => { connection.SendObject("AuthDBConnectionACK"); AuthConnection.connection = connection; AuthConnection.RegisterPackets(); } ); NetworkComms.AppendGlobalIncomingPacketHandler <string>("AgentDBConnection", (header, connection, content) => { connection.SendObject("AgentDBConnectionACK"); AgentConnection.connection = connection; AgentConnection.RegisterPackets(); } ); //NetworkComms.AppendGlobalIncomingPacketHandler<string[]>("UserLogin", (header, connection, content) => //{ // AuthPackets.LoginPacket(content[0], content[1], content[2]); //}); }
private static void RegisterPacketHandler() { NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { PacketParser.ParseUnmanagedPacket(array, connection); } ); }
private static void RegisterPacketHandler() { NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { PacketParser.ParseUnmanagedPacket(array, connection); } ); NetworkComms.AppendGlobalIncomingPacketHandler <string>("WorldAgentConnection", (header, connection, content) => { connection.SendObject("WorldAgentConnectionACK"); WorldServerConnection.connection = connection; WorldServerConnection.RegisterPackets(); }); }
private static void RegisterPacketHandler() { NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { PacketParser.ParseUnmanagedPacket(array, connection); } ); //NetworkComms.AppendGlobalIncomingPacketHandler<string>("AgentAuthConnection",( // (header, connection, incomingObject) => // { // connection.SendObject("AgentAuthConnectionACK"); // AgentConnection.connection = connection; // AgentConnection.RegisterPackets(); // } )); }
/// <summary> /// Run the AdvancedSend example. /// </summary> public static void RunExample() { Console.WriteLine("Unmanaged Connection Example ...\n"); //***************************************************************// // Start of interesting stuff // //***************************************************************// Console.WriteLine("\nNOTE: From this point on make sure both clients are configured in the same way if you want the example to work."); Console.WriteLine("\nIMPORTANT!! - Many of the features offered by NetworkComms.Net rely on managed connections, " + "i.e. those which enable the custom ApplicationLayerProtocol. If you use unmanaged connections, i.e. where the custom " + "application protocol has been disabled, you must take into account TCP packet fragmentation and concatenation, " + "correctly handling it, for all circumstances."); //Choose between unmanaged TCP or UDP SelectConnectionType(); //Add a packet handler for dealing with incoming unmanaged data NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { Console.WriteLine("\nReceived unmanaged byte[] from " + connection.ToString()); for (int i = 0; i < array.Length; i++) { Console.WriteLine(i.ToString() + " - " + array[i].ToString()); } }); //Create suitable send receive options for use with unmanaged connections SendReceiveOptions optionsToUse = new SendReceiveOptions <NullSerializer>(); //Get the local IPEndPoints we intend to listen on //The port provided is '0' meaning select a random port. List <IPEndPoint> localIPEndPoints = (from current in HostInfo.IP.FilteredLocalAddresses() select new IPEndPoint(current, 0)).ToList(); //Create suitable listeners depending on the desired connectionType List <ConnectionListenerBase> listeners; if (connectionTypeToUse == ConnectionType.TCP) { //For each localIPEndPoint get a TCP listener //We need to set the ApplicationLayerProtocolStatus to Disabled listeners = (from current in localIPEndPoints select(ConnectionListenerBase) new TCPConnectionListener(optionsToUse, ApplicationLayerProtocolStatus.Disabled)).ToList(); } else { //We need to set the ApplicationLayerProtocolStatus to Disabled listeners = (from current in localIPEndPoints select(ConnectionListenerBase) new UDPConnectionListener(optionsToUse, ApplicationLayerProtocolStatus.Disabled, UDPConnection.DefaultUDPOptions)).ToList(); } //Start listening Connection.StartListening(listeners, localIPEndPoints, true); //***************************************************************// // End of interesting stuff // //***************************************************************// Console.WriteLine("Listening for incoming byte[] on:"); List <EndPoint> localListeningEndPoints = Connection.ExistingLocalListenEndPoints(connectionTypeToUse); foreach (IPEndPoint localEndPoint in localListeningEndPoints) { Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); } Console.WriteLine("\nPress any key if you want to send data from this client. Press q to quit."); while (true) { //Wait for user to press something before sending anything from this end var keyContinue = Console.ReadKey(true); if (keyContinue.Key == ConsoleKey.Q) { break; } //Create the send object based on user input byteDataToSend = CreateSendArray(); //Get remote endpoint address //Expecting user to enter IP address as 192.168.0.1:4000 ConnectionInfo connectionInfo = ExampleHelper.GetServerDetails(ApplicationLayerProtocolStatus.Disabled); //***************************************************************// // Start of interesting stuff // //***************************************************************// Connection connectionToUse; //Create the connection if (connectionTypeToUse == ConnectionType.TCP) { connectionToUse = TCPConnection.GetConnection(connectionInfo, optionsToUse); } else { connectionToUse = UDPConnection.GetConnection(connectionInfo, UDPOptions.None, optionsToUse); } //Send the object connectionToUse.SendUnmanagedBytes(byteDataToSend); //***************************************************************// // End of interesting stuff // //***************************************************************// Console.WriteLine("\nSend complete. Press 'q' to quit or any other key to send something else."); } //***************************************************************// // Start of interesting stuff // //***************************************************************// //Make sure you call shutdown when finished to clean up. NetworkComms.Shutdown(); //***************************************************************// // End of interesting stuff // //***************************************************************// }