コード例 #1
0
        public void ReadCallback(IAsyncResult ar)
        {
            string content = string.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state     = (StateObject)ar.AsyncState;
            Socket      handler   = state.workSocket;
            int         bytesRead = 0;

            try
            {
                if (!((handler.Poll(1000, SelectMode.SelectRead) && (handler.Available == 0)) || !handler.Connected))
                {
                    // Read data from the client socket.
                    bytesRead = handler.EndReceive(ar);
                }
                else
                {
                    LogRecived?.Invoke(LogTypes.ClientDisconnected, string.Format("Client {0} is now disconected: ", handler.IpAdress()));
                    OnClientDisConnect?.Invoke(handler.IpAdress());
                }
            }
            catch (Exception ex)
            {
                LogRecived?.Invoke(LogTypes.Error, ex.ToString());
            }

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                //state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                //content = Encoding.Unicode.GetString(state.buffer, 0, bytesRead);

                for (int i = 0; i < bytesRead; i++)
                {
                    content += (char)state.buffer[i];
                }

                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);

                DataRecived?.Invoke(((IPEndPoint)(((StateObject)(ar.AsyncState)).workSocket.RemoteEndPoint)).Address.ToString(), content);
            }
        }
コード例 #2
0
 private void SocketServer_OnClientDisConnect(ClientSocket client)
 {
     ChatClientManager.RemoveClient(client);
     OnClientDisConnect?.Invoke(client);
 }
コード例 #3
0
 private void ClientSocketManager_AsyncOnClientDisConnect(ClientSocket client) =>
 ActionsConcurrentQueue.Enqueue(() => OnClientDisConnect.Invoke(client));