/// <summary> /// Creates a new instance of a <see cref="NetworkListener"/>. /// </summary> /// <param name="port">The port of server.</param> /// <param name="clientConnectedHandler">The client connected handler callback implementation.</param> /// <param name="messageReceivedHandler">The callback of message received handler implementation.</param> /// <param name="clientDisconnectedHandler">The callback of client disconnected handler implementation.</param> /// <param name="maxMessageBuffer">The number max of connected clients, the default value is 1000.</param> public NetworkListener(ushort port, ClientConnectedHandler clientConnectedHandler, MessageReceivedHandler messageReceivedHandler, ClientDisconnectedHandler clientDisconnectedHandler, ushort maxMessageBuffer) { _clientConnectedHandler = clientConnectedHandler; _messageReceivedHandler = messageReceivedHandler; _clientDisconnectedHandler = clientDisconnectedHandler; _maxMessageBuffer = maxMessageBuffer; }
/// <summary> /// /// </summary> /// <param name="maxConn"></param> /// <param name="receiveBufferSize"></param> /// <param name="getIp"></param> public void setListener(int maxConn, int receiveBufferSize, ClientConnectedHandler getIp) { ServerState = WorkStatus.ServerInitialing; connCount = 0; MaxClient = maxConn; Clients = new SocketPool(); SemaphoreAcceptedClients = new Semaphore(ConnectionCount, MaxClient); SocketBuffer = new BufferManager(receiveBufferSize * maxConn, receiveBufferSize); CommProtocol = new CommunicationProtocol(); ClientConnected = getIp; }
/// <summary>Called when a client has connected to the server.</summary> /// <param name="client">The client that has connected.</param> /// <returns>True if the connection is to be accepted.</returns> protected virtual bool OnClientConnected(IClient client) { this.Info(client, Cell_Core.ClientConnected); ClientConnectedHandler clientConnected = this.ClientConnected; if (clientConnected != null) { clientConnected(client); } return(true); }
/// <summary> /// Called when a client has connected to the server. /// </summary> /// <param name="client">The client that has connected.</param> /// <returns>True if the connection is to be accepted.</returns> protected virtual bool OnClientConnected(ClientBase client) { Info(client, Resources.Connected); ClientConnectedHandler handler = ClientConnected; if (handler != null) { handler(client); } return(true); }
public Client(TcpClient client, IEventDispatcher eventDispatcher, Parser.ParserMethod parseMethod, ClientConnectedHandler onConnect = null) { _client = client; Connected = true; _client.NoDelay = true; _eventDispatcher = eventDispatcher; _parseMethod = parseMethod; _networkInfos = _client.Client.RemoteEndPoint.ToString(); if (onConnect != null) onConnect.BeginInvoke(this, null, null); new Task(Receive).Start(); }
/// <summary> /// 连接处理 /// </summary> protected virtual bool OnClientConnected(IClient client) { ClientConnectedHandler handler = ClientConnected; if (handler != null) { handler(client); } //if (ClientConnected != null) //{ // ClientConnected(this, new DataEventArgs<string>(client.ClientAddress.ToString())); //} return(true); }
/// <summary> /// Creates a new instance of a <see cref="TcpNetworkListener"/>. /// </summary> /// <param name="port">The port of server.</param> /// <param name="clientConnectedHandler">The client connected handler callback implementation.</param> public TcpNetworkListener(ushort port, ClientConnectedHandler clientConnectedHandler) : base(port, clientConnectedHandler) { try { _clientConnectedHandler = clientConnectedHandler; _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _listener.Bind(new IPEndPoint(IPAddress.Any, port)); _listener.Listen(100); _listener.BeginAccept(new AsyncCallback(OnAccept), null); Console.WriteLine($"Starting the server network listener on port: {port}."); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}."); } }
public Client(string ip, int port, IEventDispatcher eventDispatcher, Parser.ParserMethod parseMethod, ClientConnectedHandler onConnect = null) { try { _client = new TcpClient(ip, port); Connected = true; _client.NoDelay = true; _eventDispatcher = eventDispatcher; _parseMethod = parseMethod; _networkInfos = _client.Client.RemoteEndPoint.ToString(); if (onConnect != null) onConnect.BeginInvoke(this, null, null); new Task(Receive).Start(); } catch (SocketException se) { Connected = false; } }
/// <summary> /// Creates a new instance of a <see cref="TcpNetworkListener"/>. /// </summary> /// <param name="port">The port of server.</param> /// <param name="clientConnectedHandler">The client connected handler callback implementation.</param> /// <param name="messageReceivedHandler">The callback of message received handler implementation.</param> /// <param name="clientDisconnectedHandler">The callback of client disconnected handler implementation.</param> /// <param name="maxMessageBuffer">The number max of connected clients, the default value is 1000.</param> public UdpNetworkListener(ushort port, ClientConnectedHandler clientConnectedHandler, MessageReceivedHandler messageReceivedHandler, ClientDisconnectedHandler clientDisconnectedHandler, ushort maxMessageBuffer) : base(port, clientConnectedHandler, messageReceivedHandler, clientDisconnectedHandler, maxMessageBuffer) { try { _kingUdpClients = new Dictionary <EndPoint, UDPClient>(); _listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); _listener.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port)); EndPoint endPointFrom = new IPEndPoint(IPAddress.Any, 0); byte[] array = new byte[_maxMessageBuffer]; _listener.BeginReceiveFrom(array, 0, _maxMessageBuffer, SocketFlags.None, ref endPointFrom, new AsyncCallback(ReceiveDataCallback), array); Console.WriteLine($"Starting the UDP network listener on port: {port}."); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}."); } }
/// <summary> /// Initializes this instance with the provided values. /// </summary> /// <param name="port"></param> /// <param name="max_users"></param> private void Initialize(int port, int max_users) { server = null; this.port = port; this.max_users = max_users; this.battlefields = new ArrayList(); try { server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); local_end_point = new IPEndPoint(IPAddress.Any, port); server.Bind(local_end_point); server.Listen(1); ClientConnected += new ClientConnectedHandler(ChrysopraseDaemon_ClientConnected); } catch (SocketException s_ex) { Console.WriteLine(s_ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Creates a new instance of a <see cref="WebSocketNetworkListener"/>. /// </summary> /// <param name="port">The port of server.</param> /// <param name="listenerType">The listener type of client connection.</param> /// <param name="clientConnectedHandler">The client connected handler callback implementation.</param> /// <param name="messageReceivedHandler">The callback of message received handler implementation.</param> /// <param name="clientDisconnectedHandler">The callback of client disconnected handler implementation.</param> /// <param name="maxMessageBuffer">The number max of connected clients, the default value is 1000.</param> public WebSocketNetworkListener(NetworkListenerType listenerType, ushort port, ClientConnectedHandler clientConnectedHandler, MessageReceivedHandler messageReceivedHandler, ClientDisconnectedHandler clientDisconnectedHandler, ushort maxMessageBuffer) : base(port, clientConnectedHandler, messageReceivedHandler, clientDisconnectedHandler, maxMessageBuffer) { try { //var host = Dns.GetHostEntry(Dns.GetHostName()); //var hostIp = host.AddressList.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork).ToString(); _listenerType = listenerType; _httpListener = new HttpListener(); _httpListener.Prefixes.Add($"http://localhost:{port}/"); _httpListener.Prefixes.Add($"http://127.0.0.1:{port}/"); //_httpListener.Prefixes.Add($"http://{hostIp}:{port}/"); _httpListener.Start(); Console.WriteLine($"Starting the WebSocket network listener on port: {port}."); WaitForConnections(); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}."); } }
/// <summary> /// Creates a new instance of a <see cref="NetworkListener"/>. /// </summary> /// <param name="port">The port of server.</param> /// <param name="clientConnectedHandler">The client connected handler callback implementation.</param> public NetworkListener(ushort port, ClientConnectedHandler clientConnectedHandler) { }
public static NetworkListener CreateForType(NetworkListenerType listenerType, ushort port, ClientConnectedHandler clientConnectedHandler) { if (listenerType == NetworkListenerType.TCP) { return(new TcpNetworkListener(port, clientConnectedHandler)); } return(new UdpNetworkListener(port, clientConnectedHandler)); }